Running Two PHP Versions on the Same Server Using Apache
Running legacy codebases alongside modern applications is a common scenario during migration or in shared hosting environments. You likely have PHP versions installed side-by-side, perhaps via a package manager like apt or yum, but you need to serve specific requests to a specific version without touching global configuration files that might affect other services.
The standard approach of using .htaccess directives like AddHandler application/x-httpd-php7 .php often fails when attempting to route traffic between different PHP versions on the same Apache instance. This happens because Apache's handler mapping logic usually prioritizes the first match found in its configuration order, and simply changing the MIME type or handler name in a directory-level file rarely overrides the global default or the FastCGI process manager setup effectively.
To solve this, you must configure Apache to use multiple PHP-FPM pools or Sockets, each corresponding to a specific PHP version. This method leverages Apache's ProxyPass directive to forward requests to the correct backend process.
Configuring Multiple PHP-FPM Pools
The most robust way to achieve version-specific routing is to ensure both PHP versions have their own Process Manager (FPM) pools configured with distinct Sockets.
Verify FPM Configuration: Ensure that both
php5.6-fpmandphp7.0-fpmare running and listening on different sockets.- Check
/etc/php/5.6/fpm/pool.d/www.conffor the socket path (e.g.,/var/run/php5-fpm.sock). - Check
/etc/php/7.0/fpm/pool.d/www.conffor its socket path (e.g.,/var/run/php7-fpm.sock).
- Check
Modify Apache Virtual Hosts: Instead of relying on
.htaccess, apply the routing logic directly in your Apache Virtual Host configuration files (typically found in/etc/apache2/sites-available/).For the PHP 5.6 Project:
Create a virtual host or edit the existing one for the legacy project. Configure theProxyPassdirective to forward all PHP requests to the version 5.6 socket.<VirtualHost *:80> ServerName legacy.example.com DocumentRoot /var/www/legacy # Proxy everything except static assets to the PHP 5.6 pool ProxyPassMatch ^/(.+\.(php|php3|phtml))$ fcgi://unix:/var/run/php5-fpm.sock/$1 # Or simply proxy the whole host if no other backend exists: # ProxyPassMatch ^/(.+\.(php|php3|phtml))$ fcgi://unix:/var/run/php5-fpm.sock/$1 # Ensure static files are served directly <IfModule mod_rewrite.c> RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ http://localhost/legacy/$1 [L] </IfModule> </VirtualHost>For the PHP 7.0 Project:
Similarly, configure the second virtual host to point to the PHP 7.0 socket.<VirtualHost *:80> ServerName modern.example.com DocumentRoot /var/www/modern # Proxy PHP requests to the version 7.0 pool ProxyPassMatch ^/(.+\.(php|php3|phtml))$ fcgi://unix:/var/run/php7-fpm.sock/$1 </VirtualHost>
Why .htaccess Often Fails Here
The AddHandler directive you attempted changes the MIME type or handler associated with the .php extension. However, if Apache is already configured to use a default PHP-FPM handler globally (often via mod_proxy_fcgi and a generic DirectoryIndex or global LoadModule), local overrides can be ignored due to precedence rules or because the specific FPM socket isn't being recognized as a valid target for that MIME type. Furthermore, Apache's processing model resolves handlers before entering .htaccess, making it difficult to dynamically switch between two distinct FPM sockets based solely on an extension filter within a directory context without explicit proxy configuration.
Implementation Considerations and Pitfalls
When implementing this dual-version setup, be mindful of the following:
- Static Assets: Ensure your proxy rules only target PHP files. Static assets (images, CSS, JS) should bypass the FPM socket entirely to avoid unnecessary overhead or errors if the static file handler isn't configured for the specific socket.
- Session Management: If both projects share a session storage mechanism, ensure they are not interfering with each other. Using separate database tables or filesystem directories for sessions is often necessary when running different PHP versions simultaneously.
- Error Logs: Monitor
access.loganderror.logcarefully. Since the routing happens at the Apache level, you might see 404 errors if theProxyPassMatchpattern doesn't align perfectly with your document root structure or if the socket path is incorrect. - Global Defaults: If you have a catch-all configuration for PHP in your main
httpd.confor a global site config, it might intercept requests before they reach your specific Virtual Host definitions. Ensure your VHost settings are applied after any global defaults or use specific<Directory>blocks to isolate the behavior.
By explicitly mapping sockets in Apache's primary configuration, you bypass the limitations of .htaccess and achieve a clean separation between your legacy PHP 5.6 application and your modern PHP 7.0 stack. This approach is scalable and allows for a gradual migration path without requiring a full server restart or downtime for either project.
