Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Web Servers»Nginx»How to Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04

    How to Install Multiple PHP Version with Nginx on Ubuntu 18.04 & 16.04

    RahulBy RahulJune 16, 20184 Mins ReadUpdated:December 13, 2019

    Generally, web hosting manager used a separate server for each PHP version application deployment. Which increases the hosting cost. Alternatively, you can run multiple Docker containers for multiple PHP versions.

    This tutorial helps you with the installation and configuration of two VirtualHost on the Nginx web server with different PHP versions. First VirtualHost will work with PHP 5.6 and another VirtualHost will run with PHP 7.2. So just go through this tutorial. You can also use more than two PHP versions with Nginx as required but this tutorial covers two only.

    PHP Installation

    For the installation of PHP versions, we use the PPA maintained here. Use the below couple of commands to add the PPA to your system.

    sudo apt install python-software-properties
    sudo add-apt-repository ppa:ondrej/php
    

    For this tutorial, I used PHP 5.6 and PHP 7.2 to configure it with the Nginx web server. To use the multiple PHP versions, we will use PHP FPM and FastCGI. Let’s install the following packages on your system.

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

    You may also need to install additional PHP modules. For the tutorial, only the above packages are required.

    After installation, php-fpm services will be started automatically. Use the following commands to make sure both services are running.

    sudo systemctl status php5.6-fpm
    sudo systemctl status php7.2-fpm
    
    • Recommended: How to Enable/Disable PHP Modules on Ubuntu

    Nginx Installation

    The Nginx web server is packages are available in the official Ubuntu repository. Launch terminal on your system or login with ssh for remote systems. Execute the following commands to install the latest available version of the Nginx web server.

    sudo apt update 
    sudo apt install nginx
    

    Nginx Configuration

    Get ready for the configuration of websites in the Nginx server. For the testing purpose, I am configuring two websites to work with two different-2 PHP versions. First, create two directories on your server.

    sudo mkdir /var/www/php56
    sudo mkdir /var/www/php72
    

    Now, create and index.php containing the phpinfo() function.

    echo "<?php phpinfo(); ?>" > /var/www/php56/index.php
    echo "<?php phpinfo(); ?>" > /var/www/php72/index.php
    

    After that create server blocks for both sites on Nginx. The latest version of Nginx keeps the server block configuration files under /etc/nginx/sites-available directory. Create a file for the first virtual host and edit in your favorite text editor.

    sudo vim /etc/nginx/sites-available/php56.example.com
    

    Add the following content. Make sure to use the correct ServerName and directory path according to your setup. This website is configured to work with PHP 5.6.

    # Application with PHP 5.6
    #
    server {
    	listen 80;
    
    	root /var/www/php56;
    	index index.php;
    	server_name php56.example.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;
    	}
    }
    

    Similarly, create a second VirtualHost configuration file to work with PHP 7.2. Edit configuration file in text editor:

    sudo vim /etc/nginx/sites-available/php72.example.com
    

    Add the following content to file with proper ServerName and DocumentRoot.

    # Application with PHP 7.2
    #
    server {
    	listen 80;
    
    	root /var/www/php72;
    	index index.php;
    	server_name php72.example.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;
    	}
    }
    

    All right, You have created both websites in your Nginx. But they are still not active. Nginx keeps active sites under /etc/nignx/sites-enabled directory. You can simply create a symbolic link for both config files to this directory or use the below command to do the same.

    sudo ln -s /etc/nginx/sites-available/php56.example.com /etc/nginx/sites-enabled/
    sudo ln -s /etc/nginx/sites-available/php72.example.com /etc/nginx/sites-enabled/
    

    After making all the changes restart Nginx to reload new settings changes.

    sudo systemctl restart nginx.service
    

    Your setup has been completed now. Go to the next step to test your setup.

    Test Setup

    All done. You can access both sites in your favirote web browser . You will see that php56.example.com shows the version PHP 5.6 and php72.example.com is showing the PHP 7.2 as the configuration.

    Multiple PHP Version with Nginx & PHP 7.2

    Multiple PHP Version with Nginx & PHP 5.6

    Congratulations, You system is ready to host websites with different PHP versions. Happy hosting.

    nginx PHP php-fpm
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Access Remote Mailbox on Linux Terminal
    Next Article How To Install MySQL on Ubuntu 18.04 (Bionic)

    Related Posts

    How to Install Composer on Ubuntu 22.04

    Updated:June 24, 20223 Mins Read

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:June 19, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:June 28, 20225 Mins Read

    How to Increase Request Timeout in NGINX

    Updated:January 13, 20222 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    6 Comments

    1. Jay Teli on June 18, 2020 2:27 pm

      Excellent Guide..
      However i had to do two more step :
      1) vim /etc/hosts
      2) add below 2 lines , save and exit.
      127.0.0.1 php56.example.com
      127.0.0.1 php72.example.com

      Reply
    2. Nurlan on February 19, 2019 11:58 am

      Thank you!! works for me.

      Reply
    3. Nurlan on February 19, 2019 11:38 am

      how to open multiple php versions if i dont have dns… opening only one default page.

      Reply
    4. Morpheus on October 12, 2018 9:57 pm

      Nice tutorial. To extend it a little bit, how can we have two versions of php also in the cli? if you use composer, for example, the cli will say default version php7.2 but actually you need for that certain project to use php5. An answer is super appreciated.

      Reply
      • Rahul K. on October 15, 2018 7:49 am

        You can define any php version with composer command as :

        /usr/bin/php5.6 /usr/local/bin/composer help

        /usr/bin/php7.2 /usr/local/bin/composer help

        Reply
    5. Paolo on October 5, 2018 1:34 pm

      Thanks excellent guide..I was looking for a long time
      In case of memcached or opcache do I have to make any changes?
      Hello

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to run “npm start” through docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.