Running several PHP versions side by side on Ubuntu 26.04
4 min read
One server, a PHP 7.4 site, a PHP 8.3 site and a PHP 8.5 site. How to install the versions together on Ubuntu 26.04, give each site its own PHP-FPM pool, and keep the command line, Composer and cron on the right version.
PHP versions do not fight each other on Ubuntu. Each one installs into its own directories, runs its own PHP-FPM service and listens on its own socket. The web server decides, per site, which socket gets the request.
This guide sets up PHP 7.4, 8.3 and 8.5 together on Ubuntu 26.04 with nginx. We ran the install steps on a fresh Ubuntu 26.04.1 system on 12 November 2026.
In short
- Ubuntu 26.04 only has PHP 8.5. Other versions come from
packages.sury.org/php. - Each version has its own service (
php7.4-fpm), socket (/run/php/php7.4-fpm.sock) and configuration (/etc/php/7.4/). - In nginx, the
fastcgi_passline of a site chooses the version. - Give every site its own pool and Linux user, not the shared
wwwpool. - On the command line, call the version by name:
php7.4,php8.3.
1. Add the repository
Ubuntu 26.04 has no PHP 7.4 or 8.3, and the old ppa:ondrej/php does not cover this release. Add Ondřej Surý's repository as described in Ondřej's PHP PPA on Ubuntu 26.04.
2. Install the versions
sudo apt-get install \
php7.4-fpm php7.4-cli php7.4-mysql php7.4-xml php7.4-mbstring php7.4-curl \
php8.3-fpm php8.3-cli php8.3-mysql php8.3-xml php8.3-mbstring php8.3-curl \
php8.5-fpm php8.5-cli php8.5-mysql php8.5-xml php8.5-mbstring php8.5-curl
Extensions are per version. If the 7.4 site needs gd, install php7.4-gd; having php8.5-gd does not help it.
After this, you have three of everything:
ls /etc/php
7.4 8.3 8.5
grep -h '^listen =' /etc/php/*/fpm/pool.d/www.conf
listen = /run/php/php7.4-fpm.sock
listen = /run/php/php8.3-fpm.sock
listen = /run/php/php8.5-fpm.sock
3. Point each site at its version
In the site's nginx server block:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
Change the socket name, test and reload:
sudo nginx -t && sudo systemctl reload nginx
That is the whole switch. To move the site to PHP 8.3 later, change php7.4 to php8.3 in that one line.
4. One pool per site
The default www pool runs every site as the same user, www-data. With that setup a hole in one site can read the files of all the others. This matters most when one of them runs an old PHP version.
Create a Linux user per site and a pool for it. For a site called shop on PHP 7.4, put this in /etc/php/7.4/fpm/pool.d/shop.conf:
[shop]
user = shop
group = shop
listen = /run/php/php7.4-fpm-shop.sock
listen.owner = www-data
listen.group = www-data
pm = ondemand
pm.max_children = 8
php_admin_value[open_basedir] = /home/shop/:/tmp/
Then:
sudo systemctl restart php7.4-fpm
and use unix:/run/php/php7.4-fpm-shop.sock in that site's nginx block. open_basedir keeps the site's PHP code inside its own directory.
5. The command line
php without a number is whichever version the alternatives system points to:
update-alternatives --list php
/usr/bin/php7.4
/usr/bin/php8.3
/usr/bin/php8.5
sudo update-alternatives --set php /usr/bin/php8.5
Do not rely on this default for a site's own tasks. Name the version:
php7.4 artisan migrate
php8.3 /usr/bin/composer install
The same goes for cron. A line that says php /home/shop/cron.php runs on the server's default version, and silently changes behaviour when somebody changes the default. Write /usr/bin/php7.4 /home/shop/cron.php.
Composer checks the PHP version it runs under. If you run composer install with PHP 8.5 for a 7.4 site, it may install packages that 7.4 cannot load.
6. Keep an eye on memory
Each version runs its own PHP-FPM master process, and each pool has its own workers. Three versions with several pools add up. On small servers use pm = ondemand so that idle sites use no workers, and set pm.max_children per pool instead of leaving the default.
7. Remove what you no longer use
When the last site leaves a version, remove it:
sudo apt-get purge 'php7.4-*'
An unused old PHP is still installed software with known holes.
The same thing without the manual work
This is exactly what a server panel is for. In Zefily every site gets its own Linux user, its own pool and open_basedir by default, and the PHP version is a dropdown on the site's page. On Ubuntu 26.04 it offers PHP 7.3 to 8.5 with nginx and OpenLiteSpeed, and refuses to remove a version a site still uses. See Web stack.
Frequently asked questions
Can I run two PHP versions on the same Ubuntu server?
Yes. Each version installs into its own directory under /etc/php, with its own PHP-FPM service and socket. The web server picks the socket per site.
How do I choose the PHP version for one nginx site?
Set fastcgi_pass in that site's server block to the socket of the version, for example unix:/run/php/php8.3-fpm.sock, then reload nginx.
How do I change the default php command?
With update-alternatives --set php /usr/bin/php8.5. For a site's own tasks, call the version by name instead, such as php7.4.
Do I need to install extensions for every PHP version?
Yes. Extensions are packaged per version: php7.4-gd and php8.5-gd are separate packages.
Which PHP does cron use?
The server's default php, unless the cron line names a version. Write the full path, such as /usr/bin/php7.4, so the task does not change when the default changes.
See supported systems for the full version matrix.