Ubuntu ships one PHP version per release (8.3 on Ubuntu 24.04). The widely used ondrej/php PPA, maintained by a Debian PHP maintainer, provides every supported PHP version so you can run several side by side. Each version gets its own PHP-FPM service, so different sites can use different versions.
Before you start
- Ubuntu 22.04 or 24.04 with sudo access.
- A web server (Apache or Nginx) if you serve websites; see LEMP stack or LAMP stack.
- On AlmaLinux or Rocky use Remi instead: PHP from Remi. On cPanel use EasyApache 4.
Add the PPA
apt update && apt -y install software-properties-common
add-apt-repository -y ppa:ondrej/php
apt updateFor Apache, also add ppa:ondrej/apache2 so Apache stays compatible with the PHP packages.
Install PHP versions
Install each version you need with FPM and common extensions, for example 8.2 and 8.4:
apt -y install php8.2-fpm php8.2-mysql php8.2-curl php8.2-gd php8.2-mbstring php8.2-xml php8.2-zip php8.2-intlapt -y install php8.4-fpm php8.4-mysql php8.4-curl php8.4-gd php8.4-mbstring php8.4-xml php8.4-zip php8.4-intlEach version runs as its own service, for example php8.2-fpm, with a socket at /run/php/php8.2-fpm.sock.
Choose the command-line default
update-alternatives --set php /usr/bin/php8.4
php -vOr pick interactively with update-alternatives --config php. Composer and cron jobs use this default unless you call /usr/bin/php8.2 directly.
Assign a version per site
- Nginx: in the site's server block set
fastcgi_pass unix:/run/php/php8.2-fpm.sock;and reload Nginx. - Apache: enable
proxy_fcgiand in the virtual host add:<FilesMatch \.php$> SetHandler "proxy:unix:/run/php/php8.2-fpm.sock|fcgi://localhost" </FilesMatch>
Settings and hardening
- Edit each version separately:
/etc/php/8.2/fpm/php.iniand pool files in/etc/php/8.2/fpm/pool.d/. - Run each site's pool under its own Linux user for isolation.
- Remove versions you no longer use:
apt purge 'php7.4*'. - Updates arrive through normal
apt upgrade.
Verify
systemctl status php8.2-fpm php8.4-fpmPlace a temporary phpinfo() file on each site and confirm the expected version, then delete it.
Troubleshooting
- Package not found: run
apt updateafter adding the PPA; check your Ubuntu release is still supported by the PPA. - 502 Bad Gateway: the socket path does not match the installed version.
- Extension missing in one version: extensions are per version; install
php8.2-EXTfor each.
