PHP-FPM (FastCGI Process Manager) is a PHP FastCGI implementation optimized for high-traffic environments. It allows you to run multiple PHP versions side-by-side, ideal for hosting diverse applications. This guide explains how to install and configure multiple PHP-FPM versions on Debian or Ubuntu servers.
Prerequisites
Step 1: Update the System
Before proceeding, ensure your package list is up to date:
sudo apt update && sudo apt upgrade -y
Step 2: Add the ondrej/php
PPA
The ondrej/php
repository provides access to multiple PHP versions:
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Step 3: Install Apache and FastCGI Module
Install Apache and required FastCGI modules:
sudo apt install -y apache2 libapache2-mod-fcgid
Enable necessary Apache modules:
sudo a2enmod actions fcgid alias proxy_fcgi
Restart Apache:
sudo systemctl restart apache2
Step 4: Install Multiple PHP Versions
Install the desired PHP versions and PHP-FPM packages. Below is an example with PHP 7.4, 8.0, and 8.1:
sudo apt install -y php7.4 php7.4-fpm php7.4-cli php7.4-mysql php7.4-curl php7.4-zip \
php8.0 php8.0-fpm php8.0-cli php8.0-mysql php8.0-curl php8.0-zip \
php8.1 php8.1-fpm php8.1-cli php8.1-mysql php8.1-curl php8.1-zip
You can verify installed PHP versions:
php7.4 -v
php8.0 -v
php8.1 -v
Step 5: Configure PHP-FPM Pools (Optional)
Each PHP version installs its own FPM service and pool configuration file. You can adjust settings such as user, group, and socket paths.
Example for PHP 7.4:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
Update the following lines as needed (optional customization):
user = www-data
group = www-data
listen = /run/php/php7.4-fpm.sock
listen.owner = www-data
listen.group = www-data
Repeat for other versions (/etc/php/8.0/fpm/pool.d/www.conf
, etc.)
Restart all PHP-FPM services:
sudo systemctl restart php7.4-fpm
sudo systemctl restart php8.0-fpm
sudo systemctl restart php8.1-fpm
Step 6: Configure Apache Virtual Hosts per PHP Version
To serve different PHP versions on different sites, create separate Apache virtual hosts using <FilesMatch>
and ProxyPassMatch
.
Example for a site using PHP 7.4:
<VirtualHost *:80>
ServerName site74.example.com
DocumentRoot /var/www/site74
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost/"
</FilesMatch>
</VirtualHost>
Example for a site using PHP 8.0:
<VirtualHost *:80>
ServerName site80.example.com
DocumentRoot /var/www/site80
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php8.0-fpm.sock|fcgi://localhost/"
</FilesMatch>
</VirtualHost>
Enable your virtual hosts and restart Apache:
sudo a2ensite site74.conf
sudo a2ensite site80.conf
sudo systemctl reload apache2
Step 7: Verify the Setup
Create a test.php
file in each site's DocumentRoot
:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/site74/test.php
echo "<?php phpinfo(); ?>" | sudo tee /var/www/site80/test.php
Visit http://site74.example.com/test.php
and http://site80.example.com/test.php
in a browser. Each should show a different PHP version with FPM/FastCGI as the Server API.
Step 8: Manage PHP-FPM Services
Each PHP version runs as its own service. Useful commands:
sudo systemctl status php7.4-fpm
sudo systemctl restart php8.0-fpm
sudo systemctl enable php8.1-fpm
Disclaimer
The instructions provided are for informational purposes only. Proceed at your own risk. Hosting Controller Inc. is not responsible for any data loss or system issues following this guide.