How to select PHP version 5 and 7 per virtualhost in Apache 2.4 on Debian?

How to select PHP version 5 and 7 per virtualhost in Apache 2.4 on Debian?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Isolating PHP Versions per VirtualHost in Apache 2.4 on Debian

Running multiple versions of PHP side-by-side on a single Apache installation is a common requirement during migration phases. You might have legacy applications relying on deprecated functions that only exist in PHP 5, while new projects benefit from the performance and security improvements of PHP 7 or 8. On Debian 9 with Apache 2.4, achieving this isolation requires specific configuration directives that tell the web server which interpreter to load for a given domain.

The core mechanism lies in the SetHandler directive combined with AddHandler. By default, Apache uses the cgi-fcgi or proxy_fcgi handler mapped to a generic script filename like index.php, but more importantly, it relies on the global LoadModule and PHPIniDir settings which often point to a single PHP-FPM pool. To separate versions per virtual host, you must override the default handler mapping within each <VirtualHost> block.

Configuring the PHP-FPM Sockets

First, ensure that both PHP 5 and PHP 7 installations are present on the system. If you installed them via apt, they will likely be located in /usr/sbin/php-fpm5 and /usr/sbin/php-fpm7. The critical step is verifying that the FastCGI sockets exist for each version.

Typically, these are created automatically when the PHP-FPM services start:

  • PHP 5: /var/run/php5-fpm.sock (or similar depending on distro setup)
  • PHP 7: /run/php7-fpm.sock

You can verify these exist by running ls -l /var/run/php*. If they are missing, start the respective services:

service php5-fpm start
service php7-fpm start

Modifying the VirtualHost Configuration

Once the sockets are active, you need to instruct Apache to pass requests for specific domains to the correct PHP process manager. This is done by modifying your site configuration files, usually located in /etc/apache2/sites-available/.

For a virtual host running on PHP 7, add the following inside the <VirtualHost> block:

<VirtualHost *:80>
    ServerAdmin webmaster@mywebsite.com
    ServerName mywebsite.com
    DocumentRoot /var/www/mywebsite.com

    # Direct requests to the PHP 7 FastCGI socket
    SetHandler "proxy:unix:/run/php7-fpm.sock|fcgi://localhost"
    
    # Ensure index.php is handled correctly if not via mod_rewrite
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php7-fpm.sock|fcgi://localhost"
    </FilesMatch>

    # Optional: Prevent .htaccess from overriding this if you want strict control
    AllowOverride None 
</VirtualHost>

For the legacy site running on PHP 5, modify the directive to point to the PHP 5 socket:

<VirtualHost *:80>
    ServerAdmin webmaster@mywebsite2.com
    ServerName mywebsite2.com
    DocumentRoot /var/www/mywebsite2.com

    # Direct requests to the PHP 5 FastCGI socket
    SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost"
    
    <FilesMatch \.php$>
        SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost"
    </FilesMatch>

    AllowOverride None 
</VirtualHost>

Handling the Global Apache Configuration

A common pitfall occurs when LoadModule php7_module or PHPIniDir is defined globally in /etc/apache2/mods-enabled/php7.conf. If you have a global directive like SetHandler proxy:unix:/run/php7-fpm.sock|fcgi://localhost in the main configuration, it might override your per-site settings.

To prevent this, ensure that the version-specific socket mapping is strictly contained within the <VirtualHost> blocks or use the AddHandler directive if you are using a specific script handler like application/x-httpd-php. However, the modern and recommended approach on Debian is to rely solely on SetHandler within the VirtualHost context to override any global defaults.

Verification and Troubleshooting

After updating the configuration files, reload Apache to apply changes without dropping connections:

sudo apache2ctl configtest
sudo systemctl reload apache2

If configtest returns errors, check for syntax issues or duplicate handler definitions. A frequent mistake is assuming that installing both versions automatically creates two distinct pools; you must explicitly ensure the Listen directives in /etc/apache2/mods-enabled/php5-fpm.conf and php7-fpm.conf are not conflicting with your Apache port bindings.

This approach allows for granular control, enabling you to migrate sites one by one without a forced server-wide upgrade. It preserves compatibility for older codebases while taking advantage of newer language features where appropriate.

Our network