How To Install Linux, nginx, MySQL, PHP 5.6 (LEMP) stack on Ubuntu 16.04

Standard

Step 1: Install the Nginx Web Server

$ sudo apt-get update

$sudo apt-get install nginx

Enable UFW (Ubuntu Firewall)

$ sudo ufw enable

$ sudo ufw status

$ sudo ufw allow ‘Nginx HTTP’

Step 2: Install MySQL

$ sudo apt-get install mysql-server

Step 3: Install PHP 5.6

Use the following set of command to add PPA for PHP 5.6 in your Ubuntu system and install PHP 5.6.

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install -y php5.6

Check Installed PHP Version:

$ php -v 

PHP 5.6.29-1+deb.sury.org~xenial+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

You can install php5.6 modules too for example

sudo apt-get install php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml

Step 4: Install PHP5.6-FPM

To install PHP-FPM, open terminal and type in these commands. We will configure the details of nginx and php details in the next step:

$ sudo apt-get install php5.6-fpm

We need to make another small change in the php5-fpm configuration.Open up http://www.conf:

 sudo nano /etc/php/5.6/fpm/pool.d/www.conf

Find the line, listen = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /run/php/php5.6-fpm.sock
.

listen = /run/php/php5.6-fpm.sock

Save and Exit.

Restart php-fpm:

/etc/init.d/php5.6-fpm restart

Step 5: Configure Nginx to Use the PHP Processor

sudo nano /etc/nginx/conf.d/default.conf

Currently, with the comments removed, the Nginx default server block file looks like this:

/etc/nginx/conf.d/default.conf
server {
    listen 80 default_server;
    listen [::]:80 default_server;

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

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass unix:/run/php/php5.6-fpm.sock;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
                
     }
}