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.
1 2 3 4 |
<FilesMatch ".+\.ph(ar|p|tml)$"> SetHandler None SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost" </FilesMatch> |
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.
1 2 3 4 |
<FilesMatch ".+\.ph(ar|p|tml)$"> SetHandler None SetHandler "proxy:fcgi://127.0.0.1:9000" </FilesMatch> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<Directory "/var/www/lupecode.com/70"> SetHandler None SetHandler "proxy:unix:/run/php/php7.0-fpm.sock|fcgi://localhost" </Directory> <Directory "/var/www/lupecode.com/71"> SetHandler None SetHandler "proxy:unix:/run/php/php7.1-fpm.sock|fcgi://localhost" </Directory> <Directory "/var/www/lupecode.com/72"> SetHandler None SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost" </Directory> <Directory "/var/www/lupecode.com/73"> SetHandler None SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost" </Directory> |
That’s real. You can see it working below. In the folders I have just an index.php
file with phpinfo(INFO_GENERAL);
.
Update 2020:
Added PHP 7.4 to this list.