How to Run Two PHP Versions on One VPS with Nginx and PHP-FPM
One site needs PHP 8.1, another needs PHP 8.3, and both live on the same server. You do not need a second VPS for that. You can run two PHP versions one VPS with Nginx and PHP-FPM by installing a second PHP version, giving each version its own pool, and pointing each Nginx server block at the correct socket. Each site then runs on the version it needs, and neither site can break the other by changing a shared setting.
This guide uses Ubuntu 24.04, which ships PHP 8.3. We will add PHP 8.1 as the second version. The same pattern works for other pairs once you change the version numbers.
Why Run Two PHP Versions on One VPS
Old code is the usual reason. A WordPress plugin, a custom app, or a legacy script may fail on PHP 8.3 while a newer site on the same server needs it. Running two versions lets you move sites one at a time instead of forcing a single upgrade date on all of them.
Cost is the other reason. A second VPS means a second bill, a second set of updates, and a second server to watch. Two PHP versions on one VPS keep the extra work small: one Nginx install, one firewall, and one backup routine, plus a second PHP-FPM service and socket.
Two PHP Versions One VPS: How PHP-FPM Keeps Them Separate
PHP-FPM runs as a separate service for each PHP version, each with its own pool config and socket file. Nginx does not run PHP itself. It passes each request to the socket named in that site’s server block, so the choice of PHP version happens per site, not per server.
Each version also keeps its own config files. Settings for PHP 8.1 live under /etc/php/8.1/fpm/, while settings for PHP 8.3 live under /etc/php/8.3/fpm/. Change memory_limit in one folder and the other version keeps its old value. That separation is what makes multiple PHP versions Nginx setups safe to run in production. A pool defines how many worker processes PHP-FPM starts, which user runs them, and which socket they listen on, so size each pool to the traffic that version actually serves. Our guide to tuning PHP-FPM pm settings for a 2 GB RAM VPS shows how to pick numbers that fit a small server.
Install the Second PHP Version
Start by checking what is already installed. A standard LEMP install on Ubuntu 24.04 already has PHP 8.3.
php -v
systemctl status php8.3-fpm --no-pager
PHP 8.1 is not in the Ubuntu 24.04 repositories, so add the Ondrej PHP PPA, the common source for extra PHP versions on Ubuntu. Then install PHP 8.1 with FPM and the extensions your sites need.
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install php8.1-fpm php8.1-mysql php8.1-curl php8.1-mbstring php8.1-xml php8.1-zip -y
Install only the extensions each site needs. If a site later reports a missing function, install the matching extension for that exact version, such as php8.1-gd, then restart that version’s FPM service only.
Check that both services run after the install:
systemctl status php8.1-fpm --no-pager
systemctl status php8.3-fpm --no-pager
ls /run/php/
You should see two socket files, php8.1-fpm.sock and php8.3-fpm.sock. If one service failed to start, read its log with sudo journalctl -u php8.1-fpm --no-pager -n 50 before changing anything else.
Check Both PHP-FPM Pools
Each version ships with a default pool named www. Its config sits at /etc/php/8.1/fpm/pool.d/www.conf and /etc/php/8.3/fpm/pool.d/www.conf. Open both files and confirm the listen lines point at different sockets:
grep "^listen" /etc/php/8.1/fpm/pool.d/www.conf
grep "^listen" /etc/php/8.3/fpm/pool.d/www.conf
Expect output like this:
listen = /run/php/php8.1-fpm.sock
listen = /run/php/php8.3-fpm.sock
If both lines name the same socket, one service will fail to start. Fix the listen line, then restart that service. For a busy site, give it a dedicated pool instead of sharing the www pool: copy www.conf, change the pool name and socket, and set its own pm.max_children. The guide to PHP-FPM tuning for Nginx pm.max_children explains how to size that number from your RAM.
Set the right permissions on each pool
Pools run as www-data by default on Ubuntu. If Nginx returns a 502 error after a restart, check the pool user and socket permissions first. A socket owned by the wrong user blocks Nginx from passing requests to PHP-FPM, even when both services report as active.
Point Each Nginx Server Block at the Right Socket
Each server block has a fastcgi_pass line that names one socket. The site that needs PHP 8.1 points at the 8.1 socket, and the site that needs PHP 8.3 points at the 8.3 socket. Nothing else in the block needs to change.
Here is a server block for a site on PHP 8.1:
server {
listen 80;
server_name old.example.com;
root /var/www/old.example.com;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
}
And the same block for a site on PHP 8.3, with only the socket changed:
server {
listen 80;
server_name new.example.com;
root /var/www/new.example.com;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}
The nginx fastcgi_pass line is the only place where a site picks its PHP version. Copying a block and forgetting to change the socket is the most common way a new site ends up on the wrong version. If you are still setting up the blocks themselves, read our guide to setting up Nginx server blocks on Ubuntu 24.04 first.
After editing a block, test the config and reload Nginx. Use reload, not restart, when you only changed a server block, so existing connections stay alive:
sudo nginx -t
sudo systemctl reload nginx
Verify Each Site Uses the Right PHP Version
Do not trust the config alone. Create a small test file in each site root and check the version it reports.
echo "<?php phpinfo(); ?>" | sudo tee /var/www/old.example.com/info.php
echo "<?php phpinfo(); ?>" | sudo tee /var/www/new.example.com/info.php
Open http://old.example.com/info.php and http://new.example.com/info.php. The first page should show PHP 8.1 at the top, the second PHP 8.3. Also check the “Loaded Configuration File” line: it should point at /etc/php/8.1/fpm/php.ini for the old site and /etc/php/8.3/fpm/php.ini for the new one.
Delete both test files as soon as you have the answer. A public phpinfo() page lists your PHP version, extensions, and paths for anyone who finds it.
sudo rm /var/www/old.example.com/info.php /var/www/new.example.com/info.php
For WordPress, check Tools, then Site Health, then Info, then Server instead. That check stays behind the login.
Common Mistakes That Break a Two-Version Setup
Work through this list when a site shows the wrong version or stops loading PHP pages.
- Wrong socket path. A typo in
fastcgi_passgives a 502 Bad Gateway error. Compare the path in the server block with the file listed byls /run/php/. - CLI version confusion. The command
php -vshows the CLI version, which may differ from the version Nginx uses. Change the CLI default withupdate-alternativesonly if a cron job needs a specific version. It does not change what Nginx serves. - Extension installed for the wrong version. Installing
php8.3-mysqldoes not give PHP 8.1 the MySQL extension. Each version needs its own set. - Config edited in the wrong folder. Editing
/etc/php/8.3/fpm/php.iniwill not change how the PHP 8.1 site behaves. Check the version folder in the path before you save. - One service not restarted. After editing a pool file, restart only that version with
sudo systemctl restart php8.1-fpmorsudo systemctl restart php8.3-fpm. - OPcache holding old code. When you switch a site to a new version, restart its FPM service so it reads the current files. Our OPcache setup guide for PHP 8.3 on Ubuntu covers the settings that matter on a small VPS.
Keep Both Versions Updated and Secure
Two PHP versions mean two sets of security updates. Run sudo apt update && sudo apt upgrade on your normal schedule and check that both FPM services restart cleanly after a PHP update.
Plan the end of the older version too. PHP 8.1 still gets security fixes, but it will reach end of life before PHP 8.3 does. Use the time this setup buys you to test the old site on the newer version, fix what breaks, and move it over.
When a site is ready to move, change its fastcgi_pass line to the new socket, test the config, reload Nginx, and check the site with the phpinfo() test above. If the site works, remove the old version only after a few days of normal traffic. If it fails, change the socket line back. That quick rollback is the real value of running two PHP versions one VPS: each move is small, testable, and easy to undo.