Bobcares

How to run multiple PHP versions on One Server Using Apache and PHP-FPM on Ubuntu

by | Oct 22, 2020

Wondering how to run multiple PHP versions on Apache PHP-FPM? Here’s how we do it.

Here at Bobcares, we have seen several such Apache related queries as part of our Server Management Services for web hosts and online service providers.

Today we’ll see how to run multiple PHP versions on Apache PHP-FPM.

 

How to run multiple PHP versions on Apache PHP-FPM

Here are the steps our Support Engineers follow to run multiple PHP versions on Apache PHP-FPM.

Make sure that you have one Ubuntu 20.04 server with at least 1GB of RAM set up along with a sudo non-root user and a firewall. Also, an Apache webserver set up and configured.

 

1. Install PHP Versions 7.2 and 7.3 with PHP-FPM

First, we shall install the PHP versions 7.2 and 7.3, as well as the PHP-FPM and other extensions as well. But in order to accomplish this, we need to first add the Ondrej PHP repository.

Run the below command to install software-properties-common.

$ sudo apt-get install software-properties-common -y

This software-properties-common package provides the apt-add-repository command-line utility using which you can add the ondrej/php PPA repository.

Now add the ondrej/php repository by running the below command.

$ sudo add-apt-repository ppa:ondrej/php

Next, update the repository.

$ sudo apt-get update -y

Then install php7.2, php7.2-fpm, php7.2-mysql, libapache2-mod-php7.2, and libapache2-mod-fcgid by running the below command.

$ sudo apt-get install php7.2 php7.2-fpm php7.2-mysql libapache2-mod-php7.2 libapache2-mod-fcgid -y

Now repeat the same process for PHP version 7.3. Run the below command to install php7.3, php7.3-fpm, php7.3-mysql, and libapache2-mod-php7.3.

$ sudo apt-get install php7.3 php7.3-fpm php7.3-mysql libapache2-mod-php7.3 -y

Once you install both the PHP versions, start the php7.2-fpm service:

$ sudo systemctl start php7.2-fpm

Then verify the status of php7.2-fpm service.

$ sudo systemctl status php7.2-fpm

Finally, enable several modules so that your the Apache2 service can work with multiple PHP versions:

$ sudo a2enmod actions fcgid alias proxy_fcgi

Now, in order to apply the changes, restart the Apache.

$ sudo systemctl restart apache2

 

2. Creating Directory Structures for Both Websites

First, create a document root directories for both site1.your_domain and site2.your_domain.

$ sudo mkdir /var/www/site1.your_domain
$ sudo mkdir /var/www/site2.your_domain

The Apache webserver runs as a www-data user and www-data group by default. To ensure having correct ownership and permissions of the website root directories, run the below commands.

$ sudo chown -R www-data:www-data /var/www/site1.your_domain
$ sudo chown -R www-data:www-data /var/www/site2.your_domain
$ sudo chmod -R 755 /var/www/site1.your_domain
$ sudo chmod -R 755 /var/www/site2.your_domain

Then create an info.php file inside each website root directory. This will display each website’s PHP version information. Start with site1.

$ sudo nano /var/www/site1.your_domain/info.php

Next, add the below line

<?php phpinfo(); ?>

Save the file and close it. Then copy the info.php file you created to site2.

$ sudo cp /var/www/site1.your_domain/info.php /var/www/site2.your_domain/info.php

Now the web servers must have the document root directories that each site requires to serve data to visitors.

 

3, Configuring Apache for Both Websites

Now we shall create 2 virtual host configuration files. This will let the 2 websites to work simultaneously with two different PHP versions.

We need to create a virtual host file with the correct directives so that Apache will serve this content. Instead of modifying the default configuration file located at /etc/apache2/sites-available/000-default.conf, its better to create two new ones inside the directory /etc/apache2/sites-available/.

Firstly, create a new virtual host configuration file for the website site1.your_domain by running the below command.

$ sudo nano /etc/apache2/sites-available/site1.your_domain.conf

Add the below content in /etc/apache2/sites-available/site1.your_domain.conf. Ensure the website directory path, server name, and PHP version match your setup:

<VirtualHost *:80>
ServerAdmin admin@site1.your_domain
ServerName site1.your_domain
DocumentRoot /var/www/site1.your_domain
DirectoryIndex info.php

<Directory /var/www/site1.your_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}/site1.your_domain_error.log
CustomLog ${APACHE_LOG_DIR}/site1.your_domain_access.log combined
</VirtualHost>

Save the file and close it.

Then create a new virtual host configuration file for the website site2.your_domain. Now specify this subdomain to deploy php7.3.

$ sudo nano /etc/apache2/sites-available/site2.your_domain.conf

Then add the below content in /etc/apache2/sites-available/site2.your_domain.conf.

<VirtualHost *:80>
ServerAdmin admin@site2.your_domain
ServerName site2.your_domain
DocumentRoot /var/www/site2.your_domain
DirectoryIndex info.php

<Directory /var/www/site2.your_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}/site2.your_domain_error.log
CustomLog ${APACHE_LOG_DIR}/site2.your_domain_access.log combined
</VirtualHost>

Save and close the file.

Then check the Apache configuration file for any syntax errors by running the below command.

$ sudo apachectl configtest

You must see the below output.

Output
$ Syntax OK

Then enable both virtual host configuration files by running the below commands.

$ sudo a2ensite site1.your_domain
$ sudo a2ensite site2.your_domain

Next, disable the default site, since you won’t need it.

$ sudo a2dissite 000-default.conf

Lastly, restart the Apache service so that the changes takes place.

$ sudo systemctl restart apache2

 

4. Testing Both Websites

Now we have configured the two websites to run two different PHP versions. So now test the results.

For that, open the web browser and visit the two websites http://site1.your_domain and http://site2.your_domain.

Now you must see the site1.your_domain page having PHP version 7.2 and site2.your_domain having PHP version 7.3.

Since you have tested the websites, you can remove the info.php file. Because they contain sensitive information about your server and are accessible to unauthorized users. So to remove the files, run the below command.

$ sudo rm -rf /var/www/site1.your_domain/info.php
$ sudo rm -rf /var/www/site2.your_domain/info.php

Finally, we have set a single Ubuntu 20.04 server handling two websites with two different PHP versions.

[Need any further assistance with Apache queries? – We are here to help you.]

 

Conclusion

In today’s writeup, we saw how to run multiple PHP versions on Apache PHP-FPM.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.