Skip to the content
Documentation

Virtual host example

No ratings yet

Apache virtual hosts for an Exponential 6 installation with PHP-FPM. Replace example.com, /var/www/example.com (the installation root, where index.php is), the PHP-FPM socket and the certificate paths. The modules rewrite, proxy, proxy_fcgi and ssl must be loaded (Debian: a2enmod rewrite proxy_fcgi ssl).

A minimal virtual host with .htaccess

This one lets Apache read the rules from .htaccess, which ./console exp:setup:htaccess --apply copies from .htaccess_root. It passes httpd -t (Apache 2.4):

<VirtualHost *:443>
    ServerName example.com
    DocumentRoot /var/www/example.com
    DirectoryIndex index.php

    SSLEngine on
    SSLCertificateFile    /etc/ssl/example.com/fullchain.pem
    SSLCertificateKeyFile /etc/ssl/example.com/privkey.pem

    <Directory /var/www/example.com>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

Add a *:80 virtual host that redirects to HTTPS, and get the certificate from your usual tool. The socket is the one of the PHP-FPM pool that serves the site: /run/php-fpm/www.sock is the stock pool on Red Hat style systems; Debian uses /run/php/php<version>-fpm.sock, and Plesk one pool per domain.

The faster virtual host without .htaccess

For production, write the rules of .htaccess_root into the virtual host and set AllowOverride None: Apache then no longer looks for .htaccess files in every directory on every request. In server context each pattern starts with ^/. The complete, tested file, with HTTPS, HTTP/2, the security headers and the redirect from plain HTTP, is the Apache virtual host example in the help center. Compare its rules with .htaccess_root after every upgrade and merge new ones.

After a change

apachectl configtest
systemctl reload httpd       # apache2 on Debian and Ubuntu
curl -s -o /dev/null -w '%{http_code}\n' https://example.com/site/          # 200
curl -s -o /dev/null -w '%{http_code}\n' https://example.com/.git/config    # 404

After a PHP change, reload the PHP-FPM that serves the site. On hosts with several PHP versions that is not always the service called php-fpm (on Plesk it is plesk-php<XY>-fpm); php bin/php/console exp:velocity deploy finds it by itself.

Other servers: Virtual host setup (Velocity and nginx). What the rules do: htaccess example.

Contributors

Comments

Add a comment

Comments are read by a moderator before they appear.