How To Handle Multiple PHP Versions With Apache WebServer On Ubuntu 20.04 / 18.04 / 16.04

In this tutorial we will configure Apache to run PHP 7.2 and PHP 5.6 simultaneously, and choose between them using Virtual Hosts or .htaccess.

There are several ways to achieve the same goal and run multiple websites on different PHP versions simultaneously. As for running multiple PHP versions on one server, my choice is Apache with FastCGI.

Prerequisites

1. You should already have Apache installed and serving web pages before following this guide.

If you need to install Apache/LAMP Server on Ubuntu, please check our post.

2. To install Multiple php version check our post.

Note: Now we are assuming you have Apache and multiple PHP installed, if not please check our above reference URL.
Step 1: Let’s begin by updating the package lists.
sudo apt update
Step 2: If fpm module is not installed use below command.
sudo apt install php7.2-fpm php5.6-fpm

Once installed, you should have two new sockets in /var/run/php/.

sudo ls -al /var/run/php/

To Verify fpm serivce:

sudo systemctl status php7.2-fpm && sudo systemctl status php5.6-fpm
Step 3: Apache Configuration

Now enable few modules required for the configuration of multiple PHP versions with Apache.These modules are necessary to integrate PHP FPM and FastCGI with Apache server.

Ubuntu 20.04 / 18.04 users.

sudo a2enmod actions alias proxy_fcgi fcgid

Ubuntu 16.04 users.

sudo a2enmod actions alias proxy_fcgi fastcgi
Now, restart the Apache by using below commands.
sudo systemctl restart apache2
Step 4 : Create two Virtual Host for php 5.6 and php7.2 (Change the domain name)

Create webroot directory for your both website.

 

cd /var/www/ && mkdir php56 php72

Create VirtualHost for 1st website.

sudo vim /etc/apache2/sites-available/php56_example.conf 
<VirtualHost *:80>
ServerName php56.example.com
DocumentRoot /var/www/php56
<Directory /var/www/php56>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>

Create VirtualHost for 2nd website.

sudo vim /etc/apache2/sites-available/php72_example.conf
<VirtualHost *:80>
ServerName php72.example.com
DocumentRoot /var/www/php72
<Directory /var/www/php56>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost"
</FilesMatch>
</VirtualHost>

Now we need to restart apache and PHP-FPM Services and enable modules/Website Configuration file.

Enable modules/Website Configuration file

sudo a2enmod rewrite && sudo a2ensite php72_example.conf && sudo a2ensite php56_example.conf 

Restart Services

 

sudo service apache2 restart && sudo service php7.2-fpm restart && sudo service php5.6-fpm restart
Step 5: Test PHP

Add info.php in both the website directory and paste the below code.

<?php
phpinfo(); 
?>

We can now load this file in the browser by going to http://php56.example.com/info.php or http://php72.example.com/info.php

PHP 72 Click to view image
PHP 56 Click to view image

Optional Step 6 : Htaccess Method

1. Need to enable AllowOverride ALL in virtual host.

2. Enable rewrite module.

*We have already enabled both steps above*

Then add the below code in .htaccess file according to php version.

<FilesMatch \.php> 
SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/" 
</FilesMatch>
Using the above tutorial you have learned How To Handle Multiple PHP Versions With Apache Server.
Please Drop Comment if you faced any issue at any point, I will try to help asap.
50% LikesVS
50% Dislikes

10 Comments

  • Right away I am going away to do my breakfast, after having my breakfast coming yet again to read other news. Suzanne Talbot Valeda

  • This is one awesome blog post. Really looking forward to read more. Great. Carlina Alasdair Keung

  • I really like your writing style, excellent information, thanks for putting up : D. Elfrida Waite Carolus

  • Great post! We will be linking to this great post on our website. Keep up the great writing. Mary Ty Eliot

  • Appreciating the dedication you put into your website and in depth information you present. Mommy Marlin Ho

  • Greate post. Keep posting such kind of info on your page. Tybi Moritz Magavern

  • Excellent article. I will be dealing with many of these issues as well.. Doti Prentice Kilk

    • Thank You, I hope your issue is resolved by Doc.

  • Great delivery. Great arguments. Keep up the great spirit. Kendra Jethro Ned

  • The unbelievable benefit of no banks excited about corporations with useful belongings have an interest. Janie Felicio Hendrika

Comments are closed.