Linux VPS

Install multiple PHP versions on Ubuntu with the ondrej PPA

Add the ondrej/php PPA on Ubuntu, install PHP 7.4 to 8.4 side by side with FPM, choose the CLI default, assign versions per site in Apache or Nginx, and keep them updated.

IntermediateUbuntu VPS2 min read

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

Add the PPA

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

For 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-intl
apt -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-intl

Each 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 -v

Or 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_fcgi and 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.ini and 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-fpm

Place a temporary phpinfo() file on each site and confirm the expected version, then delete it.

Troubleshooting

  • Package not found: run apt update after 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-EXT for each.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.