How To Ubuntu 16.04 Multiple Php Nginx

How to Ubuntu 16.04 Multiple PHP Nginx

Ubuntu 16.04 is a robust operating system that is commonly used for web application development. This operating system uses Nginx as the web server and PHP as the programming language. It is not uncommon to have multiple PHP versions on a server especially if you’re developing different applications that require different versions of PHP. In this article, we are going to explore how to Ubuntu 16.04 multiple PHP Nginx.

Preparing The System

Before we proceed to install and configure multiple PHP versions on Ubuntu 16.04, we need to update our system packages. This helps us to ensure that our system is up-to-date and that we have the latest packages. Here is the command to update our system:

sudo apt-get update && sudo apt-get upgrade

Install Nginx

The first step towards using multiple PHP versions on Ubuntu 16.04 is installing the Nginx web server. To install Nginx, use the following command:

sudo apt-get install nginx

Once Nginx is installed, we can confirm whether it’s up and running with the following command:

sudo systemctl status nginx

Install PHP

The next step is to install the various versions of PHP that we require. Ubuntu 16.04 comes with PHP 7. To install it just use the following command:

sudo apt-get install php-fpm php-mysql php-mbstring

With PHP 7 installed, we also need to install other versions of PHP. In this case, let’s install PHP 5.6 and PHP 7.1:

sudo apt-get install php5.6-fpm php5.6-mysql php5.6-mbstring
sudo apt-get install php7.1-fpm php7.1-mysql php7.1-mbstring

Once we’ve installed the various PHP versions, we need to configure Nginx to use them.

Configure Nginx

The configuration file for Nginx is found in the /etc/nginx/ directory. Navigate to this directory by typing the following command:

cd /etc/nginx/

Next, create a new file called example.com inside the sites-available directory:

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

Paste the following code into this file. This sets up a server block with two different locations, one for PHP 7 and the other for PHP 5.6.

server {
listen 80 ;
server_name example.com;
root /var/www/example.com;

index index.php index.html index.htm;

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

location /php5.6 {
alias /var/www/example.com/php5.6;
try_files $uri $uri/ /php5.6/index.php?$args;
}

location /php7.1 {
alias /var/www/example.com/php7.1;
try_files $uri $uri/ /php7.1/index.php?$args;
}

location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}

Before proceeding, ensure that the server_name line reflects your domain name. Also, ensure that the root directory defined in this file (/var/www/example.com) reflects your website’s location.

We also have two locations that correspond to PHP 5.6 and PHP 7.1. These locations map to directories where we have installed PHP 5.6 and PHP 7.1 respectively.

Next, we need to enable this server block by creating a symbolic link to the /etc/nginx/sites-enabled directory. The command to do this is:

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

Finally, check that the Nginx is properly configured by typing the following command:

sudo nginx -t

If Nginx is properly configured, we should get the following output:

nginx: configuration file /etc/nginx/nginx.conf test is successful

Conclusion

In conclusion, using multiple PHP versions in Ubuntu 16.04 is possible with a little configuration. We’ve covered how to install and configure multiple PHP versions using Nginx. With this guide, you should be able to get started with developing web applications on Ubuntu 16.04. Don’t forget to update your system regularly to ensure that you have the latest packages.

Happy developing!

Leave a Reply

Your email address will not be published. Required fields are marked *