Running Multiple versions of PHP 5.6 and 7.2 on Apache (Ubuntu)

After the launch of PHP 7, Every one wanted to move, to the latest version due to its performance and features.However its not easy to move all old legacy code in PHP 5 to PHP 7 easily. So often we have to run two servers one with PHP 5 and another with PHP 7 and move the sites one by one. This requires extra time and money.
However now with Ubuntu 16.04 and above and also other latest linux distro, you can run both PHP version simultaneously.

Here, I will setup both PHP 7 and PHP 5.6 on Apache.
Step 1 : Update the Package

 sudo apt update 

Step 2 : We will run PHP using fastcgi, So we have to install fastcgi,
for ubuntu 18.04

 sudo apt install libapache2-mod-fcgid

for ubuntu 16.04

 sudo apt install libapache2-mod-fastcgi

Step 3 : Install software-properties-common

 sudo apt install software-properties-common

Step 4: Add Ondřej’s PHP repository to download multiple versions of PHP 5.6 and PHP 7.x, and update again

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

Step 5: Now install the version of PHP you require. Ondřej’s repository provides PHP 5.6 and PHP 7.x. In this example we will install PHP 5.6 and PHP 7.2.

 
 sudo apt install php5.6 php5.6-fpm  
 sudo apt install php7.2 php7.2-fpm

Step 6: You can check the installation by droping down the folder.

 
 ls /var/run/php/
 output:
 total 8
 -rw-r--r-- 1 root     root     4 Feb 17 16:50 php5.6-fpm.pid
 srw-rw---- 1 www-data www-data 0 Feb 17 16:50 php5.6-fpm.sock
 -rw-r--r-- 1 root     root     5 Feb 17 16:51 php7.2-fpm.pid
 srw-rw---- 1 www-data www-data 0 Feb 17 16:51 php7.2-fpm.sock

Step 7: Now you can install all the PHP extension required for, like MYSQL, Curl, XML etc required.

 
 ls /var/run/php/
 output:
 sudo apt install php-mysql
 sudo apt install php5.6-mysql
 sudo apt install php5.6-curl
 sudo apt install php7.2-curl
 sudo apt install php7.2-xml
 sudo apt install php5.6-xml

Step 8: Configure Apache and Add some Apache modules using a2enmod.

 
  sudo a2enmod actions alias proxy_fcgi fcgid   // for ubuntu 18.04
  sudo a2enmod actions alias proxy_fcgi fastcgi // for ubuntu 16.04
  sudo service apache2 restart
 

Step 9: Now open your virtualhost .conf file and add the directive with the required version

 


...
    ServerName techtutorial.online
    ServerAlias www.techtutorial.online
    

    DocumentRoot /var/www/html/techtutorial
    
        SetHandler "proxy:unix:/var/run/php/php5.6-fpm.sock|fcgi://localhost/"
    

...


 
 


...
    ServerName mysubdomain.techtutorial.online
    DocumentRoot /var/www/html/techtutorial/suddomain
    
        SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
    

...


 

Step 10: create two php info page in the location and you can check that both PHP are running simultaneously.

 

For nginx :

http://Running Multiple versions of PHP 5.6 and 7.2 on Nginx (Ubuntu)

Leave a Reply

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

RSS
Follow by Email
Facebook
Twitter