Multiple PHP version with Apache on CentOS 7

Multiple PHP version with Apache on CentOS 7

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

Stefan Bogdanescu

Founder & Senior Architect

Running Multiple PHP Versions with Apache on CentOS 7

Achieving PHP version isolation on a shared hosting environment or a multi-project server often requires more than just installing different binaries; it demands a strategy for routing requests to the correct interpreter without breaking the web server's core functionality. On CentOS 7, using a single Apache instance to serve applications running on distinct PHP versions—say, PHP 5.6 alongside PHP 7.2 or 8.0—is entirely feasible, but it requires careful configuration of the mod_rewrite module and precise definition of .htaccess rules. This approach allows you to maintain a single control panel interface while supporting legacy codebases and modern frameworks side-by-side.

The core mechanism relies on Apache's ability to inspect request headers or file extensions before dispatching the script to an external handler. When you install multiple PHP versions using packages like remi-php or by downloading source tarballs, they typically reside in different directories (e.g., /usr/bin/php-5.6, /usr/bin/php-7.2). However, Apache only knows how to execute scripts if you explicitly tell it which binary to invoke for specific projects. The standard practice involves creating a dedicated directory structure where each application resides in its own folder, and then configuring the AddHandler and SetHandler directives to map those folders to their respective PHP versions.

A critical step often overlooked is ensuring that mod_rewrite is enabled and configured correctly. You can verify this by running apachectl -M | grep rewrite. If it's missing, you'll need to enable it via yum install mod_ssl or similar package management tools depending on your repositories. Once active, the configuration usually lives in /etc/httpd/conf.d/ or within individual .htaccess files placed in the document root of each project. For a directory serving PHP 7.2, your .htaccess file might look like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

# Force usage of PHP 7.2 binary for this specific directory
AddHandler application/x-httpd-php-72 .php

However, relying solely on AddHandler can be brittle if the Apache main configuration does not explicitly define the handler mappings for the custom binaries. A more robust method involves defining the handlers in the global Apache configuration (httpd.conf) or a dedicated conf.d file before loading virtual hosts. You would add lines similar to:

AddHandler application/x-httpd-php-72 /usr/bin/php-7.2
AddHandler application/x-httpd-php-56 /usr/bin/php-5.6

This ensures that any .php file requested via the URL is routed to the correct interpreter based on the handler name, rather than relying on the default application/x-httpd-php. This distinction is vital when dealing with mixed environments where one project strictly requires PHP 7.2 features like named arguments or JIT compilation, while another must remain compatible with PHP 5.6's older syntax.

Testing this setup requires a methodical approach to avoid breaking live sites. Do not attempt to switch versions on a production server without a full backup of your configuration files and database dumps. Start by creating a test script in the new directory structure that outputs <?php echo phpversion(); ?>. Access the URL directly through your browser; if the output matches the expected version, the routing logic is working.

One common pitfall is caching issues. Browsers often cache 301 redirects or specific response headers, making it appear as though a version switch failed when it actually succeeded but the user is stuck on an old state. Use curl -I with different User-Agent strings or hard refresh your browser (Ctrl+F5) to clear these caches during testing. Additionally, ensure that PHP-FPM is not being used simultaneously for the same paths, as mixing Apache's CGI mode with a separate FPM pool can lead to undefined behaviors or "502 Bad Gateway" errors if the process manager isn't configured to listen on both versions correctly.

For teams managing large-scale deployments where version fragmentation is inevitable, this setup provides a granular way to control dependencies per directory. If you are running Laravel projects, for instance, ensuring that a legacy module uses PHP 5.6 while the main application runs on PHP 8 requires exactly this level of segregation. When in doubt, consult resources like https://laravelcompany.com to verify best practices regarding specific version requirements for modern frameworks before committing to a mixed-version architecture.

Our network