How to Handle Multiple PHP Versions with Nginx Server on Ubuntu 220.04|18.04|16.04

In this Tutorial we will configure Nginx to run PHP7.2 and PHP5.6 simultaneously and choose between them using VirtualHosts.

Prerequisites:

  1. Basic Understanding of Linux Commands.
  2. You should have Nginx installed and serving web pages before following this guide.

Check our blog here to configure LEMP Stack

  3. To install Multiple php version with FPM module. Check our blog here.

  4. Two valid domain names pointed with your VPS IP address. In this tutorial, we will use site1.example.com and site2.example.com.

Note: Now we are assuming you have Nginx and multiple PHP installed, if not please check our above reference url's.

Step 1: Let's begin by updating the package lists.

sudo apt update

Step 2: If PHP fpm module is not installed use below commands.

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

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

sudo ls -al /var/run/php/

To verify FPM service:

sudo systemctl status php7.2-fpm && sudo systemctl status php5.6-fpm

Step 3: Nginx Configuration

Create two VirtualHosts for php56 and php72 websites. Change the domain name in configuration.

  • Create Webroot directory for both websites.
sudo cd /usr/share/nginx/  && mkdir php56 php72

Create VirtualHost for 1st Website.

sudo vim /etc/nginx/conf.d/php56_website.conf
server {
listen 80;
root /usr/share/nginx/php56/;
index index.php;
server_name php56.example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 240;
}
}

Create VirtualHost for 2nd Website.

sudo vim /etc/nginx/conf.d/php72_website.conf
server {
listen 80;
root /usr/share/nginx/php72/;
index index.php;
server_name php72.example.com;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 240;
}}
  • Now we need to restart php fpm and nginx service.
sudo service nginx restart && sudo service php7.2-fpm restart && sudo service php5.6-fpm restart

Step 4: Test PHP

To verify the php version create the info.php file in webroot directory of both websites and copy paste below code.

<?php  
phpinfo();  
?>

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

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

Using the above tutorial you have learned How To Install LEMP Stack on Ubuntu Servers.

Please Drop Comment if you faced any issue at any point, I will try to help asap.
100% LikesVS
0% Dislikes

24 Comments

Comments are closed.