Run Multiple Versions of PHP in a Single Apache Vhost

When upgrading to the next release of PHP it’s nice to be able to test it in place.At one of the companies I worked for, I’ve had a server admin tell my team that they could not run more than one version of PHP on the same server. You can though, maybe the admin didn’t know. Maybe the admin didn’t want to. I cannot say.

This does require using PHP-FPM. If you have followed my post to set up HTTP2, TLSv1.3, Push, and ALPN then you have it. This guide assumes you have PHP-FPM running.

The code to have a directory or a vhost use a different version of PHP is the same. For a directory, you just put it in the .htaccess file, or in a Directory tag in the vhost directive. Some servers are set to not use .htaccess files, so I will show both here.

I did not come up with this on my own. I absconded with the handler that is in /etc/apache2/conf-available/php7.3-fpm.conf and added SetHandler None to unset the previous handler. You should check your conf-available folder. Older versions of Apache may need something like this.

Now you can throw this .htaccess file into a folder and you are good to go. You can also change the version, and have another folder running a different version.

Here’s the vhost config version for the last 4 versions of PHP.

That’s real. You can see it working below. In the folders I have just an index.php file with phpinfo(INFO_GENERAL);.

https://lupecode.com/70/

https://lupecode.com/71/

https://lupecode.com/72/

https://lupecode.com/73/

Update 2020:

Added PHP 7.4 to this list.

https://lupecode.com/74/