Install Multiple PHP Version with Nginx On a Same DigitalOcean Droplet for Laravel.


Hosting Multiple Laravel Websites with Different Version of PHP on a same Nginx DigitalOcean Droplet

If you are looking to host multiple laravel projects with different versions of PHP on a single Nginx DigitalOcean Droplet, then in this article I’ll guide you step-by-step on how to install multiple PHP version with LEMP Stack on Ubuntu.

Step 1: the official Ubuntu repositories may not have the old version of PHP or more up-to-date versions of PHP, therefore you will first need to add the ondrej/php repository to your system.

sudo apt update
sudo add-apt-repository ppa:ondrej/php

Step 2: Update the repository

sudo apt update

Step 3: Next Install Different versions of PHP

To Install PHP 5.7
sudo apt-get install php5.7-fpm php5.7-mysql php5.7-mbstring

To Install PHP 7.0
sudo apt-get install php7.0-fpm php7.0-mysql php7.0-mbstring

To Install PHP 7.1
sudo apt-get install php7.1-fpm php7.1-mysql php7.1-mbstring

To Install PHP 7.2
sudo apt-get install php7.2-fpm php7.2-mysql php7.2-mbstring

Step 4: After Installing Different PHP version, start the PHP FPM service for specific version

To Start PHP 5.7 
sudo systemctl start php5.7-fpm

To Start PHP 7.0
sudo systemctl start php7.0-fpm

To Start PHP 7.1
sudo systemctl start php7.1-fpm

Step 5: Next, verify the status of the PHP FPM service:

sudo systemctl status php5.7-fpm
sudo systemctl status php7.0-fpm
sudo systemctl status php7.1-fpm

Step 6: Important configuration changes that must be applied for specific version of PHP

here is how to open specific version of PHP configuration file

sudo nano /etc/php/5.7/fpm/php.ini
sudo nano /etc/php/7.0/fpm/php.ini
sudo nano /etc/php/7.1/fpm/php.ini

You must first open the php.ini configuration file, Once it is open, use Ctrl+W and now type in cgi.fix_pathinfo= and click enter, This will take you to the right line. You will see a semicolon the left of this line. Delete the semi colon and then change the 1 into a 0 and save the file.To save something in Nano, just press Ctrl+X and type Y and then press Enter.

You may also adjust the value in your php.ini file for configurations such as upload_max_filesize , post_max_size, max_execution_time, max_input_time and memory_limit according to your project requirement.

Step 7: To see the changes take effect we need to restart php-fpm by typing following command in your terminal.

sudo systemctl restart php5.7-fpm
sudo systemctl restart php7.0-fpm
sudo systemctl restart php7.1-fpm

At this point you have installed different PHP versions on your server. With most of the configuration completed, it is time to setup the server engine and define which version of the PHP will be used by specific Laravel projects. 

Step 8: Nginx server block file is where it comes to play around with PHP Versions. 

Nginx Server Blocks allows you to run more than one website on a single machine with different version of PHP and different configurations. With Server Blocks, you can specify the site document root (the directory which contains the website files), create a separate security policy for each site, use different SSL certificates for each site and much more.

A typical Nginx server block file for Laravel projects should looks like this: I have highlighted the code block where you have to play with PHP version.

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/techalyst/public;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name techalyst.com www.techalyst.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    //this is the code block where you will define the version of the PHP to use, in this example we use PHP 7.2
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

This article is not meant for introducing on How to create multiple nginx server block file for hosting multiple laravel project in same droplet on DigitalOcean. You can read my following useful articles to do that.

  1. Host Multiple Laravel Project in Same Droplet on Digital Ocean | Deploy Multiple Laravel Apps
  2. Laravel Hosting with Digital Ocean Droplet Step By Step Tutorial

Still If you face PHP version error when you execute command line PHP or Composer commands, you should read the following article.

Run Multiple PHP Version with Composer Command line, Laravel with Nginx on DigitalOcean Droplet

If you have any other questions, experience or insights on "Installing and Running Multiple PHP Version with Nginx On same DigitalOcean Droplet" please feel free to leave your thoughts in the comments bellow, Don't forget to share the posts.

Written by Akram Wahid 1 year ago

are you looking for a chief cook who can well craft laravel and vuejs, to make some awsome butterscotch,
yes then it is right time for you to look at my profile.

Do you want to write Response or Comment?

You must be a member of techalyst to proceed!

Continue with your Email ? Sign up / log in

Responses

Be the first one to write a response :(

{{ item.member.name }} - {{ item.created_at_human_readable }}

{{ reply.member.name }} - {{ reply.created_at_human_readable }}