A PHP version per site with PHP-FPM on Ubuntu and Debian
Run an old site on PHP 8.1 and a new one on 8.4 on the same Apache, each in its own PHP-FPM pool, and check the code before you switch.
With Apache's mod_php, PHP lives inside Apache: one version for every site on
the machine. A client site that still needs PHP 8.1 and a new build on 8.4
cannot both run. PHP-FPM fixes that. Each PHP version runs as its own service,
and Apache hands each site's .php requests to the version that site needs.
Install the versions
Ubuntu and Debian ship one PHP version per release. For more, add the
packages Ondřej Surý maintains: on Ubuntu the ppa:ondrej/php PPA, on Debian
his repository at packages.sury.org. Adding a package source is a decision
about your machine, so read its page first.
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.1-fpm php8.1-mysql php8.1-xml php8.1-mbstring php8.1-curl php8.1-zip php8.1-gd
sudo apt install php8.4-fpm php8.4-mysql php8.4-xml php8.4-mbstring php8.4-curl php8.4-zip php8.4-gd
Each version gets its own service and socket, like php8.1-fpm listening on
/run/php/php8.1-fpm.sock.
Move Apache from mod_php to PHP-FPM
mod_php needs Apache's prefork worker; PHP-FPM works with the faster event worker. Turn off mod_php, then turn on the FastCGI proxy:
sudo a2dismod php8.3 mpm_prefork
sudo a2enmod mpm_event proxy_fcgi setenvif
Use the mod_php version you have in place of php8.3. Until every site has a
handler, sites without one will download their PHP files instead of running
them, so do the next step before you reload Apache.
Point each site at its version
In each site's vhost, send .php files to the socket of the version it needs:
<VirtualHost *:443>
ServerName local.oldshop.test
DocumentRoot /var/www/local.oldshop.test
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.1-fpm.sock|fcgi://localhost"
</FilesMatch>
# SSLEngine and the certificate as before
</VirtualHost>
sudo apache2ctl configtest && sudo systemctl reload apache2
A phpinfo() page on each site shows the version it now runs.
A pool per site
Sharing php8.1-fpm's default pool works, but every site then shares its
settings and its workers. A pool per site, in
/etc/php/8.1/fpm/pool.d/oldshop.conf, gives each its own socket, settings and
process limits:
[oldshop]
user = www-data
group = www-data
listen = /run/php/oldshop.sock
listen.owner = www-data
listen.group = www-data
pm = ondemand
pm.max_children = 5
php_admin_value[memory_limit] = 512M
php_admin_value[upload_max_filesize] = 64M
Point the vhost at /run/php/oldshop.sock, then reload php8.1-fpm and
Apache.
Check the code before you switch
PHP removes and deprecates functions between versions, and a plugin that worked on 8.1 can stop with a fatal error on 8.4. PHPCompatibility, a ruleset for PHP_CodeSniffer, scans code for that without running it. PHPCompatibilityWP is its WordPress flavour, which knows what WordPress already polyfills. Scan the theme and plugins against the version you plan to move to, and read the errors before you switch.
With devmagic-panel
devmagic-panel does all of this from a site's Server and PHP tab:
- It gives the site its own PHP-FPM pool,
dmp-<folder>, with its own version and settings, and keeps its PHP settings in its own ini file instead of editingphp.ini. - Before you switch, Compatibility scans the active theme, plugins, must-use plugins, drop-ins and vendor folder with PHPCompatibilityWP, and shows each error with its file and line.
- After a switch it opens the site's home page and wp-admin, reads the error logs for new PHP fatal errors, and Undo puts the old version back.
- Services, PHP installs and updates the versions apt offers, and "How sites run" moves the whole machine from mod_php to PHP-FPM, with a check of every site and Undo.
It never adds a package source on its own: when apt has no other PHP version, it shows the command for you to run. See PHP versions in the docs.