# Main site (one domain). Needs: a TLS certificate for it, mod_rewrite, mod_ssl, mod_headers, and PHP-FPM (mod_proxy_fcgi) or mod_php.
#   a2enmod rewrite ssl headers proxy_fcgi setenvif && a2enconf php8.3-fpm
#   cp deploy/apache-app.conf /etc/apache2/sites-available/billing.conf   # edit every line marked EDIT
#   a2ensite billing && apachectl configtest && systemctl reload apache2
# The router tunnel listener is a SEPARATE file: deploy/apache-tunnel.conf

<VirtualHost *:80>
    # EDIT
    ServerName billing.example.com
    # M-Pesa callbacks and the captive portal need HTTPS.
    RewriteEngine On
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    # Same token rule as the HTTPS vhost (and its own CustomLog, so Debian's other_vhosts_access.log is not used).
    SetEnvIf Request_URI "^/(provision/|api/router_backup)" nolog
    CustomLog ${APACHE_LOG_DIR}/billing-access.log combined env=!nolog
</VirtualHost>

<VirtualHost *:443>
    # EDIT
    ServerName billing.example.com
    # EDIT (only public/ is ever web-visible)
    DocumentRoot /var/www/billing/public

    SSLEngine on
    # EDIT (your certificate)
    SSLCertificateFile    /etc/letsencrypt/live/billing.example.com/fullchain.pem
    # EDIT
    SSLCertificateKeyFile /etc/letsencrypt/live/billing.example.com/privkey.pem
    Header always set Strict-Transport-Security "max-age=31536000"
    Header always set X-Content-Type-Options "nosniff"

    <Directory /var/www/billing/public>
        # public/.htaccess holds the portal and /provision/<token>.rsc rewrites
        AllowOverride All
        Options -Indexes
        Require all granted
    </Directory>

    # PHP-FPM. Remove this block if you use mod_php. Match your PHP version's socket (ls /run/php/).
    <FilesMatch "\.php$">
        SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost"
    </FilesMatch>

    # Never serve dotfiles or SQL/log files even if one is dropped into public/ by mistake.
    <FilesMatch "^\.|\.(sql|log|bak|env)$">
        Require all denied
    </FilesMatch>

    # Router provisioning tokens travel in the URL: keep them out of the access log.
    # A CustomLog here also stops Debian's global other_vhosts_access.log from recording this vhost.
    SetEnvIf Request_URI "^/(provision/|api/router_backup)" nolog
    ErrorLog  ${APACHE_LOG_DIR}/billing-error.log
    CustomLog ${APACHE_LOG_DIR}/billing-access.log combined env=!nolog
</VirtualHost>
