Need to know how to run multiple versions of PHP on Apache PHP-FPM? This guide will show you how to go about it.
Here at Ibmi Media, as part of our Server Management Services, we regularly help our customers to to fix Apache related issues affecting their Servers.
Now, let us look into how to run multiple versions of PHP on Apache PHP-FPM.
We will see the steps to follow to run multiple PHP versions on Apache PHP-FPM with an Ubuntu 20.04 Server having a RAM of about 1GB.
Also ensure that the Server has a root user with Apache Web server installed on it.
Now follow the steps below;
To begin, install both PHP versions 7.2 and 7.3 with PHP-FPM and some extensions.
Start by installing the package "software-properties-common".
Next, add Ondrej PHP repository by running command below;
sudo add-apt-repository ppa:ondrej/php
Then, update the added repository with the command below;
sudo apt-get update -y
Now you can install PHP 7.2, php7.2-fpm and some extensions by running the command below;
sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 libapache2-mod-fcgid -y
Do the same for PHP 7.3 by running the command below;
sudo apt-get install php7.3 php7.3-fpm php7.3-mysql libapache2-mod-php7.3 -y
After the installation of PHP is completed, start the php7.2-fpm service by running the command below;
sudo systemctl start php7.2-fpm
To verify the status of php7.2-fpm service, run the command below;
sudo systemctl status php7.2-fpm
To enable the Apache2 service to function correctly with multiple versions of PHP on the server, enable some modules as shown below;
sudo a2enmod actions fcgid alias proxy_fcgi
To effect changes, restart Apache by running the command below;
sudo systemctl restart apache2
To begin, create a directory root directory for different sites hosted on the server.
Lets say we are dealing with two sites with names, "first_domain" and "second_domain" respectively, then run the commands below;
sudo mkdir /var/www/first_domain
sudo mkdir /var/www/second_domain
It is important to note that Apache web server runs as a "www-data" user and by default it belongs to the group "www-data".
To assume the correct permissions and ownership of the website root directories, simply run the commands below;
sudo chown -R www-data:www-data /var/www/first_domain
sudo chown -R www-data:www-data /var/www/second_domain
sudo chmod -R 755 /var/www/first_domain
sudo chmod -R 755 /var/www/second_domain
Now, you can create an "info.php" file inside each website root directory which will help to display the website's PHP version information.
For the first domain "first_domain", run the command below;
sudo nano /var/www/first_domain/info.php
Then add the following details into the file and save;
<?php phpinfo(); ?>
Also exit this file.
Next, do the same for the second domain by simply copying this file to the root directory of the second site as shown below;
sudo cp /var/www/first_domain/info.php /var/www/second_domain/info.php
In this step, we will create 2 different virtual host configuration files to allow 2 websites to function at the same time with two different versions of PHP.
Now create two virtual host file in the directory "/etc/apache2/sites-available/".
For the first site "first_domain", create a new virtual host configuration file implementing php7.2 by running the command below;
sudo nano /etc/apache2/sites-available/first_domain.conf
Now add the following contents into this file as shown below;
<VirtualHost *:80>
ServerAdmin admin@first_domain
ServerName first_domain
DocumentRoot /var/www/first_domain
DirectoryIndex info.php
<Directory /var/www/first_domain>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<FilesMatch \.php$>
# From the Apache version 2.4.10 and above, use the SetHandler to run PHP as a fastCGI process server
SetHandler "proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/first_domain_error.log
CustomLog ${APACHE_LOG_DIR}/first_domain_access.log combined
</VirtualHost>
Now save and exit this file.
Next, create a new virtual host configuration file for the second website "second_domain" implementing php7.3 with the command below;
sudo nano /etc/apache2/sites-available/second_domain.conf
Then add the contents below into this file;
<VirtualHost *:80>
ServerAdmin admin@second_domain
ServerName second_domain
DocumentRoot /var/www/second_domain
DirectoryIndex info.php
<Directory /var/www/second_domain>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<FilesMatch \.php$>
# 2.4.10+ can proxy to unix socket
SetHandler "proxy:unix:/run/php/php7.3-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/second_domain_error.log
CustomLog ${APACHE_LOG_DIR}/second_domain_access.log combined
</VirtualHost>
Now save and exit this file.
To test that the syntax in the Apache configuration file is correct, run the command below;
sudo apachectl configtest
Now you get an output which will look like this;
sudo a2ensite first_domain
sudo a2ensite second_domain
Next, since you will not need the default site, you can disable it by running the command below;
sudo a2dissite 000-default.conf
After this, restart the Apache service to effect changes by running the command below;
sudo systemctl restart apache2
Since, the website, PHP configuration has been configured on both websites, you can proceed with testing it.
Now open your web browser and visit the websites , http://first_domain and http://second_domain .
You will see that first_domain assumes PHP version 7.2 while second_domain assumes PHP version 7.3.
Now that the test is successful, remove the info.php file by running the commands below;
sudo rm -rf /var/www/first_domain/info.php
sudo rm -rf /var/www/second_domain/info.php
This article will guide you through the steps you need to take to run multiple PHP versions on Apache PHP-FPM. We implemented this on Ubuntu 20.04 server handling two websites with two different PHP versions (PHP 7.2 and 7.3).