Reader Stacks

How to Install Multiple Versions of PHP on the Same Ubuntu Server

Ondřej Surý's PPA packages every PHP version alongside each other under version-suffixed names, letting each site on a shared server run its own PHP-FPM pool at its own version.

Running multiple PHP versions side by side on the same Ubuntu server is a common need — one legacy application still requires PHP 7.4, while a newer project needs PHP 8.4 — and Ondřej Surý's widely-used PPA makes this genuinely straightforward by packaging every version under a distinct, version-suffixed name.

Adding the PPA

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Installing multiple specific versions

sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-mbstring
sudo apt install php8.4 php8.4-fpm php8.4-mysql php8.4-mbstring

Each version installs under its own version-suffixed package and binary names (php7.4, php8.4) — they coexist without conflicting, since neither overwrites the other's files.

Switching the CLI's default PHP version

sudo update-alternatives --set php /usr/bin/php8.4
php -v # confirms the version now in effect for the CLI

update-alternatives only changes which version the bare php command on the CLI points to — it has no effect on which version a specific website actually runs, which is controlled separately at the web server level.

Running each PHP-FPM version as its own separate service

sudo systemctl status php7.4-fpm
sudo systemctl status php8.4-fpm

Each installed version gets its own independent PHP-FPM service and socket file — this is what actually allows two different sites on the same server to each run their own specific PHP version simultaneously.

Pointing an Nginx site at a specific PHP-FPM version

server {
    # ...
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

The specific socket path (php8.4-fpm.sock versus php7.4-fpm.sock) in each site's Nginx config is what actually determines which PHP version that particular site runs — this is the real mechanism behind running multiple PHP versions on one server, more so than which version the CLI's php command happens to point to.

Checking which extensions are installed for a specific version

php8.4 -m
dpkg -l | grep php8.4

Extensions are installed per-version (php8.4-mbstring is a separate package from php7.4-mbstring) — a module needed by an application has to be explicitly installed for each PHP version that application actually runs under, not just once globally.