The Best Price for IPv4/IPv6 Lease – Any RIR & Any Geo-LocationOrder Now
Hostperl

Configure Multi-PHP Versions on Ubuntu VPS with Apache

By Raman Kumar

Share:

Updated on Jun 24, 2026

Configure Multi-PHP Versions on Ubuntu VPS with Apache

Why Running Multiple PHP Versions Actually Matters

Most VPS customers hit this problem eventually. You're hosting a legacy WordPress site on PHP 7.4, a newer client project on PHP 8.1, and your own app on 8.3 — all on the same server. With shared hosting, you'd ask support and hope for the best. On a Hostperl VPS, you control this directly.

Apache on Ubuntu handles multiple PHP versions cleanly through php-fpm (FastCGI Process Manager). Each virtual host gets its own PHP version and pool. One site breaking changes nothing for the others.

This tutorial covers Ubuntu 22.04 and 24.04 with Apache 2.4. The steps work whether you're managing a handful of client sites or running a full reseller stack without a control panel.

What You'll Need Before Starting

  • Ubuntu 22.04 LTS or 24.04 LTS with root or sudo access
  • Apache 2.4 installed and running
  • At least two domains or subdomains pointed to your server
  • software-properties-common installed (for the PHP PPA)

If Apache isn't installed yet, run:

sudo apt update && sudo apt install apache2 -y

Check it's active before proceeding:

sudo systemctl status apache2

Step 1: Add the Ondřej Surý PHP PPA

Ubuntu's default repositories only carry one PHP version per release. The Ondřej Surý PPA is the standard way to get multiple PHP versions on a single Ubuntu VPS — it's well-maintained and widely used in production.

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

Once the PPA is added, you can install any combination of PHP versions you need.

Step 2: Install Multiple PHP-FPM Versions

Install the versions you need along with their FPM packages. This example sets up PHP 7.4, 8.1, and 8.3:

sudo apt install php7.4 php7.4-fpm php7.4-mysql php7.4-curl php7.4-xml php7.4-mbstring -y
sudo apt install php8.1 php8.1-fpm php8.1-mysql php8.1-curl php8.1-xml php8.1-mbstring -y
sudo apt install php8.3 php8.3-fpm php8.3-mysql php8.3-curl php8.3-xml php8.3-mbstring -y

Each version runs as its own FPM service. Confirm they're all running:

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

If any are inactive, start them manually:

sudo systemctl start php8.1-fpm && sudo systemctl enable php8.1-fpm

Step 3: Enable Required Apache Modules

Apache needs two modules to proxy PHP requests to FPM: proxy_fcgi and setenvif. Disable mod_php if it's loaded — it conflicts with the FPM approach.

sudo a2enmod proxy_fcgi setenvif
sudo a2dismod php7.4 php8.1 php8.3 2>/dev/null
sudo systemctl reload apache2

The 2>/dev/null suppresses errors if those modules aren't loaded — safe to include either way.

Step 4: Configure Apache Virtual Hosts per PHP Version

This is the core of the setup. Each virtual host references a specific FPM socket, which tells Apache which PHP version to use for that site.

FPM sockets live at paths like:

  • /run/php/php7.4-fpm.sock
  • /run/php/php8.1-fpm.sock
  • /run/php/php8.3-fpm.sock

Create a virtual host file for your first site (running PHP 7.4):

sudo nano /etc/apache2/sites-available/legacy-site.conf

Paste this configuration:

<VirtualHost *:80>
    ServerName legacy-site.example.com
    DocumentRoot /var/www/legacy-site

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
    </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/legacy-site-error.log
    CustomLog ${APACHE_LOG_DIR}/legacy-site-access.log combined
</VirtualHost>

Now create a second virtual host for a site on PHP 8.3:

sudo nano /etc/apache2/sites-available/new-site.conf
<VirtualHost *:80>
    ServerName new-site.example.com
    DocumentRoot /var/www/new-site

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/php8.3-fpm.sock|fcgi://localhost"
    </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/new-site-error.log
    CustomLog ${APACHE_LOG_DIR}/new-site-access.log combined
</VirtualHost>

Enable both sites and reload Apache:

sudo a2ensite legacy-site.conf new-site.conf
sudo systemctl reload apache2

Step 5: Verify Each Site Uses the Correct PHP Version

Drop a phpinfo() file into each document root to confirm the right version is loading:

echo '<?php phpinfo(); ?>' | sudo tee /var/www/legacy-site/info.php
echo '<?php phpinfo(); ?>' | sudo tee /var/www/new-site/info.php

Visit http://legacy-site.example.com/info.php and http://new-site.example.com/info.php in your browser. The PHP Version field at the top should show 7.4 and 8.3 respectively.

Delete those files immediately after confirming. Leaving phpinfo() exposed on a live site leaks server configuration details — removing it is a basic item on any VPS security checklist.

sudo rm /var/www/legacy-site/info.php /var/www/new-site/info.php

Step 6: Tune PHP-FPM Pool Settings per Site

Each PHP version has its own pool configuration directory. The default pool for PHP 8.1 is at /etc/php/8.1/fpm/pool.d/www.conf. Creating per-site pool files lets you set separate memory limits and process counts for each site.

Copy the default pool as a starting point:

sudo cp /etc/php/8.1/fpm/pool.d/www.conf /etc/php/8.1/fpm/pool.d/client-site.conf

Edit the new pool file and change these key values:

[client-site]
user = www-data
group = www-data
listen = /run/php/php8.1-client-site.sock
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
php_admin_value[memory_limit] = 256M
php_admin_value[upload_max_filesize] = 64M

Update the virtual host's SetHandler line to point to the new socket path, then restart FPM:

sudo systemctl restart php8.1-fpm

Per-pool configuration is especially useful when different clients have different resource needs. The agency VPS playbook covers isolation and billing considerations in more depth.

Step 7: Adding SSL to Multi-PHP Virtual Hosts

The PHP version setup doesn't complicate SSL at all — you just duplicate the virtual host block for port 443.

If you're using Certbot with Let's Encrypt:

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d legacy-site.example.com -d new-site.example.com

Certbot detects both virtual hosts and configures SSL automatically. It preserves the SetHandler directives you set, so PHP version routing stays intact after the certificate is applied.

Common Issues and Quick Fixes

A few problems come up regularly with this setup. Here's what to check before raising a support ticket.

502 Bad Gateway: Almost always means the FPM service isn't running, or the socket path in the virtual host doesn't match the actual socket location. Run ls /run/php/ to see what sockets exist, then compare them to your SetHandler line.

Wrong PHP version loading: Confirm you've reloaded Apache after saving changes with sudo systemctl reload apache2. Also check that mod_php isn't still enabled — it overrides FPM silently. Run apache2ctl -M | grep php to check.

PHP extensions missing on one version: Each version needs its own extension packages. Installing php8.1-gd does nothing for 8.3. Install extensions per-version explicitly.

If you're planning a broader migration onto this setup, the shared-to-VPS migration guide walks through transferring site files, databases, and DNS without extended downtime.

Keeping PHP Versions Updated

The Ondřej PPA pushes updates through standard apt. A weekly cron job or regular manual run covers security patches:

sudo apt update && sudo apt upgrade -y

This updates all installed PHP versions at once. After a minor version update (e.g. 8.3.6 → 8.3.10), FPM restarts automatically.

Major version upgrades — moving a site from 8.1 to 8.2, for example — require more care. Install the new version, test the site, update the virtual host's socket path, then decommission the old pool. Never switch a production site's PHP version without testing on a staging copy first. See our notes on staging site hosting if you don't have that workflow in place yet.

Once you have full server control, running multiple PHP versions on Ubuntu VPS is straightforward. Hostperl VPS plans give you root access, SSD-backed storage, and the flexibility to configure Apache, FPM pools, and PHP versions exactly as this tutorial describes. Need more headroom for high-traffic or agency workloads? Our dedicated server plans scale without the contention of a shared environment.

Frequently Asked Questions

Can I run more than three PHP versions simultaneously?

Yes. There's no hard limit beyond your server's RAM. Each FPM service adds a small baseline memory footprint — roughly 20–40 MB idle depending on pool settings. On a 4 GB VPS, four or five versions running in parallel is comfortable.

Does this setup work with WordPress multisite?

It does. Each WordPress install on a separate virtual host gets its own FPM socket, so different installs can run different PHP versions. Within a single multisite network, all subsites share whichever version the primary domain's virtual host is configured to use.

What happens if I update PHP 7.4 and a site breaks?

PHP 7.4 reached end-of-life in 2022, so migrating those sites forward should be on your list. If a patch update breaks something, pin the package version with sudo apt-mark hold php7.4-fpm to pause updates while you debug.

Is PHP-FPM faster than mod_php?

For most VPS workloads, yes. PHP-FPM with Apache's event MPM handles concurrent connections more efficiently than prefork with mod_php. You also get better process isolation between sites, which matters when hosting multiple clients. Check the VPS control panel comparison if you're weighing whether a panel like cPanel or DirectAdmin would handle this for you instead.