Have you tried running various
PHP websites on a single
VPS server and thought to yourself, "Why does each project need a different version of
PHP?" I have. But once you understand how to run
multiple versions of PHP on a single VPS, it's much easier. Let's talk about it as though two buddies are troubleshooting a tech problem together.
Why Do You Need More Than One PHP Version
I keep asking the question, "Why does some web project need
old PHP 7.4 while the new web app needs
PHP 8.2?" But it is how it goes in the web project world. It is better to run both
PHP versions than to force one version, and something breaks.
You might need to run
multiple PHP versions in the context of:
- Running tests of new features safely
- Supporting old legacy websites that are running fine
- Running new frameworks that just need PHP 8+
Makes sense right?
How to Install Multiple PHP Versions with Ease
You can install many PHP versions side by side with little hassle.
The simple plan is as follows:
- Add PHP repository.
- Install the desired PHP versions
- Tell each website which PHP version to use
It is a great feeling when everything works out of the box, isnβt it?
PHP-FPM for Each Version
Each PHP version has its own
PHP-FPM service, and you can create new pools for each site.
I typically create files that look like:
- /etc/php/7.4/fpm/pool.d/site1.conf
- /etc/php/8.2/fpm/pool.d/site2.conf
Then I assign each pool to a different port:
- Site 1 - 127.0.0.1:9074
- Site 2 - 127.0.0.1:9082
Now, each site is using the appropriate version of PHP. Simple and clean.
Connecting PHP with Nginx or Apache
This step is what determines whether your VPS runs smoothly or acts up.
With Nginx
In the server block, I would write:
fastcgi_pass 127.0.0.1:9082;
This indicates to
Nginx to operate on
PHP 8.2 for that site.
With Apache
Apache can utilize either
SetHandler or
proxy_fcgi. You will just need to match the correct
port or
socket. If you accidentally select the incorrect version, the site generally just breaks in unexpected ways. Believe me, I have done it.
Tips to Keep Everything Nice and What I Like to Call β Organized
Here is a list of small, but strong habits to keep:
- Good naming of each PHP-FPM pool
- Keep config files separate
- Use service restart for any change
- Know what site is using what PHP version
These little actions save you a lot of confusion in the future.