How to Run Multiple PHP versions with Nginx on Ubuntu?

You can run multiple php versions on Ubuntu without any issue. You require Multiple php versions if applications are built in different php versions like php 7.1 and php 7.2. Here, we will install PHP7.0, PHP7.1, PHP7.2, and PHP7.3 on the server.

  1. First, we will add PPA, using the below command.

    # sudo apt install software-properties-common
    # sudo add-apt-repository ppa:ondrej/php
  2. Run the Update Command.

    # sudo apt update
  3. Install PHP 7.2 with below command.

    # sudo apt install php7.2 php7.2-fpm
  4. Install php extensions using below commands.

    # sudo apt install php7.2-mysql php7.2-mbstring php7.2-xml php7.2-gd php7.2-curl
  5. Now, install PHP 7.1 using below command.

    # sudo apt install php7.1 php7.1-fpm
  6. Install common PHP7.1 extensions.

    # sudo apt install php7.1-mysql php7.1-mbstring php7.1-xml php7.1-gd php7.1-curl

         You can install php 7.0 as we have installed php 7.1 and 7.2

  1. Install Nginx with the below command.

    # sudo apt install nginx
  2. Create 2 folders to test website with PHP 7.1 and PHP 7.2

    echo "<?php phpinfo();?>" > /var/www/php72/index.php
    echo "<?php phpinfo();?>" > /var/www/php71/index.php
  3. We will create server blocks for both the website on Nginx.

    # sudo nano /etc/nginx/sites-available/php71.example.com

          Insert the following content for the site with PHP 7.1


# Application with PHP 7.1
#
server {
	listen 80;

	root /var/www/php71;
	index index.php;
	server_name php71.test.com;

	location ~* \.php$ {
		# With php-fpm unix sockets
		fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
		include         fastcgi_params;
		fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
		fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
	}
}
  1. Now, create a second VirtualHost configuration file to work with PHP 7.2.

    # sudo nano /etc/nginx/sites-available/php72.example.com
    
    # Application with PHP 7.2
    #
    server {
    	listen 80;
    
    	root /var/www/php72;
    	index index.php;
    	server_name php72.test.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;
    	}
    }
    
  2. Create a symbolic link for both config files located under /etc/nginx/sites-enabled directory.

    
    # sudo ln -s /etc/nginx/sites-available/php71.example.com /etc/nginx/sites-enabled/
    
    # sudo ln -s /etc/nginx/sites-available/php72.example.com /etc/nginx/sites-enabled/
  3. Let us restart the Nginx.

    # sudo systemctl restart nginx.service
  4. You can access both the website in the browser and you will see the different versions for each.

Related Articles

How you can change the ip address of the domain in Webmin control panel?

Following are the steps to change the IP Address of the domain in Webmin control panel. Login...

How to set up a cron to take MySQL Database Backup daily and remove the older backup files that are older than 10 days?

Login to your Linux server using ssh. Open your crontab using the below command Crontab -e...

How to add user to sudo group in a Linux VPS?

With Sudo you can provide special privileges to any user or group. In Linux, there are few...

HOW TO INSTALL NEXTCLOUD ON UBUNTU?

Nextcloud is an open-source and secure PHP-based content collaboration platform. It is used for...

How To Configure Redis Caching to Speed Up WordPress on Ubuntu 18.04?

Redis is an open-source in-memory data structure store. It can be used as a DB and cache. It...

  • 0 Users Found This Useful

Was this answer helpful?