This guide shows you how to run multiple PHP versions side by side on a single Apache server using PHP-FPM (FastCGI Process Manager). You’ll learn how to install different PHP versions, configure Apache to route requests to the correct PHP-FPM socket, and set per-directory PHP version selection. This is useful for hosting legacy applications that require older PHP versions while running modern sites on the same server.
What You’ll Need
- A Linux server (Ubuntu/Debian or CentOS/RHEL) with sudo access
- Apache 2.4 or newer installed and running
- Basic familiarity with the command line and editing config files
- Access to the server’s package manager (apt or yum/dnf)
Step-by-Step
- Install PHP-FPM for the versions you need. On Ubuntu/Debian, add the Ondrej PPA to get multiple PHP versions:
sudo add-apt-repository ppa:ondrej/phpthensudo apt update. Install the FPM and common extensions for each version, e.g.,sudo apt install php7.4-fpm php7.4-mysql php8.1-fpm php8.1-mysql. On CentOS/RHEL, use Remi repository:sudo dnf install epel-release, thensudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm(adjust for your version), and enable the desired module:sudo dnf module reset php,sudo dnf module enable php:remi-8.1, then installphp81-php-fpmand similar packages. - Verify the PHP-FPM services are running. Check with
sudo systemctl status php7.4-fpmandsudo systemctl status php8.1-fpm. They should be active. Note the pool configuration files usually live in/etc/php/7.4/fpm/pool.d/www.confand/etc/php/8.1/fpm/pool.d/www.conf(Debian/Ubuntu) or/etc/opt/remi/php81/php-fpm.d/www.conf(CentOS). - Set a unique socket or port for each PHP-FPM pool. By default, each pool listens on a Unix socket. To avoid conflicts, edit each pool config and set a distinct
listendirective. For example, for PHP 7.4:listen = /run/php/php7.4-fpm.sock, and for PHP 8.1:listen = /run/php/php8.1-fpm.sock. Ensure the socket directory exists and is writable by the FPM user (usually www-data). - Enable Apache modules for FastCGI and proxy. Run
sudo a2enmod proxy_fcgi setenvifon Debian/Ubuntu. On CentOS, these modules are typically enabled by default; if not, addLoadModule proxy_module modules/mod_proxy.soandLoadModule proxy_fcgi_module modules/mod_proxy_fcgi.soto your Apache config. - Configure Apache to use PHP-FPM for each virtual host or directory. In your virtual host configuration, add a
FilesMatchdirective that routes.phpfiles to the appropriate FPM socket. For example, to use PHP 7.4 for a site, add:<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
For PHP 8.1, change the socket path accordingly. - Allow per-directory PHP version selection using .htaccess. If you want to switch PHP versions per directory without editing vhost configs, ensure
AllowOverride Allis set for the directory in your vhost. Then in a.htaccessfile in that directory, add:SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
This overrides the parent vhost setting for that directory. - Restart Apache and PHP-FPM services. Run
sudo systemctl restart apache2(or httpd on CentOS) andsudo systemctl restart php7.4-fpm php8.1-fpmto apply changes. - Test with a PHP info file. Create a file
info.phpin your web root with<?php phpinfo(); ?>. Access it via your browser and check the PHP Version line. Repeat for each vhost/directory to confirm the correct version is loaded.
Common Mistakes
- Forgetting to enable the proxy_fcgi module – Apache will show a 500 error or download the PHP file instead of executing it.
- Using the same socket path for multiple PHP-FPM pools – only one service can bind to a socket, causing startup failure for the second.
- Not setting
AllowOverride All– .htaccess directives are ignored, so per-directory version switching won’t work. - Mixing up socket paths – a typo in the socket name leads to a 502 Bad Gateway error.
- Not restarting PHP-FPM after editing pool config – changes don’t take effect until restart.
Tips
- Use a naming convention for sockets that includes the PHP version, e.g.,
php7.4-fpm.sock, to avoid confusion. - Test each PHP version in a separate subdirectory or vhost before switching production sites.
- Keep a backup of your Apache and PHP-FPM config files before making changes.
- Consider using environment variables or a configuration management tool to maintain consistency across servers.
- Monitor your FPM logs (usually in
/var/log/php7.4-fpm.log) to catch errors early.
Troubleshooting
If you see a 502 Bad Gateway, the Apache proxy cannot connect to the FPM socket. Check the socket path is correct and that the FPM service is running. Also verify the socket file exists: ls -la /run/php/. If the socket is owned by a different user, Apache may not have permission to read it; adjust the pool’s listen.owner and listen.group to match Apache’s user (www-data or apache).
If you get a 500 Internal Server Error, check Apache’s error log (sudo tail -f /var/log/apache2/error.log) for details. Often it’s a misconfigured SetHandler or a missing module. Ensure proxy_fcgi is loaded by running apache2ctl -M | grep proxy_fcgi.
If the PHP version doesn’t change as expected, clear your browser cache or use a different browser. Also verify that the .htaccess file is being read by checking the Apache configuration for AllowOverride.
If a PHP extension is missing, you may see errors like “Call to undefined function”. Install the extension for the specific PHP version, e.g., sudo apt install php8.1-curl, then restart PHP-FPM.
Frequently Asked Questions
Can I run PHP 5.6 and PHP 8.1 on the same server?
Yes, but PHP 5.6 is end-of-life and may not be available in default repositories. You can use third-party repos like Ondrej’s PPA (for Ubuntu) or compile from source. The setup process is the same.
How do I set a different PHP version for a specific subdirectory?
Place a .htaccess file in that subdirectory with the SetHandler line pointing to the desired FPM socket. Ensure AllowOverride All is set for that directory in the vhost config.
What if I only want to use PHP-FPM for some .php files and not others?
You can use <FilesMatch> with a regex pattern to match only certain filenames, e.g., <FilesMatch "^(index|legacy)\.php$">. All other .php files will be handled by the default handler.
Does this work with Nginx?
Yes, Nginx can also proxy to multiple PHP-FPM sockets using similar FastCGI configuration, but the steps differ. This guide is specific to Apache.
How do I switch the default PHP version for the CLI?
Use the update-alternatives command on Debian/Ubuntu: sudo update-alternatives --config php. On CentOS, you can set the default by adjusting the PATH or using scl enable for software collections.
Is it safe to run multiple PHP versions in production?
Yes, as long as each version is properly maintained and patched. However, running outdated PHP versions (like 5.6) poses security risks. Use a firewall to restrict access to FPM sockets and keep all versions updated.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Use Mac More Efficiently: 21 Proven Tips
- Best CPUs for Gaming and Streaming: Encoder Guide
- How to Compare AI Features and Performance in Phones
- Nginx Blocks in CentOS: Config Guide
- Create Google Meet Account: Step-by-Step Guide
- AI Frameworks: How Developers Choose and Ship Them