Running Multiple PHP Versions on the Same Apache2 Server

Running multiple PHP versions on one Apache server is a common need — a legacy application stuck on PHP 7.4 alongside a new project on PHP 8.3, both on the same box.

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.4-fpm php8.3-fpm

# Each version runs as its own FPM pool, listening on a different socket
<VirtualHost *:80>
    ServerName legacy.example.com
    DocumentRoot /var/www/legacy

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>
<VirtualHost *:80>
    ServerName new.example.com
    DocumentRoot /var/www/new

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

This only works cleanly with PHP-FPM, not the older mod_php — mod_php ties exactly one PHP version to the entire Apache process, which is precisely the limitation PHP-FPM’s per-virtual-host socket routing solves.