Home Linux How to Install Different PHP Versions (5.6,7.1 or 7.2) | Multiple Websites...

How to Install Different PHP Versions (5.6,7.1 or 7.2) | Multiple Websites | Nginx | Ubuntu Server

Key Points:
1. How to install different PHP versions (5.6,7.1 or 7.2).
2. How can I configure different PHP versions with multiple websites in Nginx on Ubuntu server?

Different PHP version on Ubuntu Different PHP version on Ubuntu

This tutorial will let you know how to install different PHP versions to configure with multiple websites in Nginx web server on Ubuntu. Here we are using Ubuntu 18.04. In a practical approach, sometimes developer needs to run multiple PHP version to test websites. But if we are going to buy a separate server for each than it would cost more. So the solution is to configure multiple PHP versions with multiple virtual hosts (websites) on the same server.

Recommended: Install different PHP Versions | multiple websites | Nginx | CentOS 7 Server.

Required

  1. PHP different versions (here using PHP 5.6 and PHP 7.2
  2. Nginx Web Server
  3. Multiple Websites

Install Different PHP Versions

To install different PHP Versions, we will use repository ppa ondrej/php.

 arun@arun-virtual-machine:~$  sudo add-apt-repository ppa:ondrej/php 

Install PHP 5.6

Run the following command to install PHP 5.6 with some modules.

 arun@arun-virtual-machine:~$ sudo apt install php5.6 php5.6-fpm

Install PHP 7.2

Now we will install the another PHP version i.e. PHP 7.2.

arun@arun-virtual-machine:~$ sudo apt install php7.2 php7.2-fpm

Install Nginx Web Server

Nginx web server is available in the official repository. We just need to run the nginx installation command.

 arun@arun-virtual-machine:~$ sudo apt-get install nginx

Configuration

Firstly, we will create 2 websites to run with 2 different PHP version. We will create site1.com and site2.com for testing purposes at local environment.

 arun@arun-virtual-machine:~$  sudo mkdir -p /var/www/html/site1.com
arun@arun-virtual-machine:~$ sudo mkdir -p /var/www/html/site2.com

websites root directories are created. Under the root directory create the index.php file to display the PHP version for testing.

$ echo "<?php phpinfo(); ?>" > /var/www/html/site1.com/index.php
$ echo "<?php phpinfo(); ?>" > /var/www/html/site2.com/index.php

Next step is to configure the virtual host for the 2 websites in Nginx. Create the configuration file for 2 websites under the directory /etc/nginx/sites-available and paste the following code in the file.

$ sudo vi  /etc/nginx/sites-available/site1.com
  
server {
listen 80;

root /var/www/html/site1.com;
index index.php;
server_name site1.com;

location ~* \.php$ {
# With php-fpm unix sockets
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}

Exactly we will create 2nd virtual host for the second website i.e. site2.com

$ sudo vi  /etc/nginx/sites-available/site2.com
 
server {
listen 80;

root /var/www/html/site2.com;
index index.php;
server_name site2.com;

location ~* \.php$ {
# With php-fpm unix sockets
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}

We have successfully created and configured both the websites. Now need to create the symbolic link from the sites-available to the sites-enabled.

$ sudo  ln  -s  /etc/nginx/sites-available/site1.com  /etc/nginx/sites-enabled/
$ sudo  ln  -s  /etc/nginx/sites-available/site2.com  /etc/nginx/sites-enabled/

Everything is done. Now we will verify the Nginx configuration for the syntax error.

$ sudo nginx -t
nginx syntax check commandnginx syntax check command

Now reload the settings, to take affect the changes done.

$ sudo service nginx restart

In the local environment, to make DNS working successful, create the entry in the /etc/hosts file.

$ sudo  vi  /etc/hosts
host entry in linuxhost entry in linux

Now check the created websites, Is working with different PHP versions or not? Just browse the websites in the browser.

site1.com
site2.com

PHP Version 5.6 on UbuntuPHP Version 5.6 on Ubuntu
PHP Version 7.2 on UbuntuPHP Version 7.2 on Ubuntu



Congratulations !!! You have learned how to install different PHP versions with multiple Websites in Nginx on Ubuntu server.

LEAVE A REPLY

Please enter your comment!
Please enter your name here