PHP-FPM uses a daemon to manage the multiple PHP versions on a single server. Together we can use Apache and PHP-FPM to host multiple PHP web sites, each using a different versions of PHP at the same time. This is useful because different applications may require different versions of PHP.
HP-FPM also offers configuration options for stderr and stdout logging, emergency restarts, and adaptive process spawning, which is useful for heavy-loaded sites. In fact, using Apache with PHP-FPM is one of the best stacks for hosting PHP applications, especially when it comes to performance.
To do this I am assuming you already have an Ubuntu 20.04 server running Apache2.
We can now start to install PHP, for this example I will install versions 7.4 and 7.2, as well as PHP-FPM and several additional extensions. To accomplish this, we will first need to add the Ondrej repository to your system.
First we need to install the software-properties-common package as it contains the components needed to add the repository.
sudo apt-get install software-properties-common -y
Now we can add the Ondrej repository to your system.
sudo add-apt-repository ppa:ondrej/php
Now lets update the repository.
sudo apt-get update
Next we are going to install PHP 7.4, you can choose the version you need by swapping the version number.
sudo apt-get install php7.4 php7.4-fpm php7.4-mysql libapache2-mod-php7.4 libapache2-mod-fcgid -y
- php7.4 is a metapackage used to run PHP applications.
- php7.4-fpm provides the Fast Process Manager interpreter that runs as a daemon and receives Fast/CGI requests.
- php7.4-mysql connects PHP to the MySQL database.
- libapahce2-mod-php7.4 provides the PHP module for the Apache webserver.
- ibapache2-mod-fcgid contains a mod_fcgid that starts a number of CGI program instances to handle concurrent requests.
You can add any other PHP modules needed with the normal apt install process.
Now we repeat the process for PHP 7.2, again you can choose the version you need by swapping the version number.
sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 -y
We can now check both the FPM services are running by checking the status.
sudo systemctl status php7.4-fpm
sudo systemctl status php7.2-fpm
Now we need to configure Apache so it can work with the multiple versions of PHP
sudo a2enmod actions fcgid alias proxy_fcgi
- actions is used for executing CGI scripts based on media type or request method.
- fcgid is a high performance alternative to mod_cgi that starts a sufficient number of instances of the CGI program to handle concurrent requests.
- alias provides for the mapping of different parts of the host filesystem in the document tree, and for URL redirection.
- proxy_fcgi allows Apache to forward requests to PHP-FPM.
Now restart Apache for the changes to take affect.
sudo systemctl restart apache2
Finally we can now configure our websites to use the version of PHP we want for them. To do this edit the website config file, append the following config to this file to choosing the relevant version of PHP, in this example we will use PHP 7.4
<FilesMatch \.php$> # For Apache version 2.4.10 and above, use SetHandler to run PHP as a fastCGI process server SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost" </FilesMatch>
Finally restart Apache again so the updated site configurations take affect.
sudo systemctl restart apache2