Running Multiple versions of PHP 5.6 and 7.2 on Nginx (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 nginx.
Step 1 : Update the Package

 sudo apt update 

Step 2 : Install software-properties-common

 sudo apt install software-properties-common

Step 3: 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 4: 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 5: 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 6: 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 7: Now open your nginx virtualhost conf file and edit

 
  location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
 
  location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
 

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

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

 

Leave a Reply

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

RSS
Follow by Email
Facebook
Twitter