Hosting & Deployment

Apache Virtual Hosts for Multiple PHP Sites

Configure Apache virtual hosts with isolated document roots, per-site logs, PHP handling, and safe reload procedures.

Apache name-based virtual hosts allow one server address to host multiple domains while keeping each site's document root and logs separate. A correct virtual-host configuration is important for both isolation and troubleshooting: an incorrect default host can serve the wrong website, and shared writable directories can allow one compromised site to affect another.

Define explicit host boundaries

A basic virtual host might be:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /srv/www/example/public

    <Directory /srv/www/example/public>
        AllowOverride None
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example-error.log
    CustomLog ${APACHE_LOG_DIR}/example-access.log combined
</VirtualHost>

Use a separate configuration and document root for each domain. Enable only the required modules and avoid broad directory permissions. If the application relies on .htaccess, configure AllowOverride narrowly rather than enabling overrides across the entire filesystem.

Enable, inspect, and reload safely

On Debian-style systems, virtual-host files are commonly enabled with a2ensite; other distributions use different include directories. Validate the configuration before reloading:

apachectl configtest
apachectl -S
systemctl reload apache2

The service may be named httpd on other systems. apachectl -S helps identify which virtual host is selected for each name and which host is the default.

PHP integration and isolation

PHP may run through PHP-FPM, a module, or a control-panel-managed handler. Prefer a supported per-site PHP-FPM pool when isolation is required. Do not mix handler directives from unrelated hosting configurations. Verify the actual PHP version and loaded extensions through a protected diagnostic process, then remove any temporary phpinfo() page.

Troubleshooting

Check DNS first, then the listener, virtual-host mapping, document-root permissions, and application logs. A browser cache or CDN can obscure changes, so test with a direct host header when appropriate. Keep TLS virtual hosts aligned with the HTTP host names and redirect policy. On cPanel, do not edit generated Apache configuration files directly; use WHM or supported include mechanisms so rebuilds do not erase manual changes.

Share this page Help other developers discover this resource.