How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 16.04

The LEMP software stack is a group of software that can be used to create dynamic web pages & apps. LEMP describe a Linux operating system, Nginx web server, MySQL database and PHP for dynamic processing.

How To Install Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 16.04
Prerequisites
Very first we need to connect through root user
$ ssh root@your_server_ip
Please complete the login process by accepting the warning about host authenticity, if it appears, then providing your root authentication (password or private key). If it is your first time logging into the server with a password, you will also be prompted to change the root password. Once you are logged in as root, we’re prepared to add the new user account that we will use to log in from now on. This example creates a new user called “aroham”, but you should replace it with a username that you like: Create a New User
adduser aroham
To add these privileges to our new user, we need to add the new user to the “sudo” group.
$ usermod -aG sudo aroham
On the server, as the root user, enter the following command to temporarily switch to the new user
su - aroham
Now we are ready to processing setup LEMP, All of the software we will be using for this procedure will come directly from Ubuntu’s default package repositories. This means we can use the apt package management suite to complete the installation.
Step 1: Install the Nginx Web Server
As this is our first time using “apt” for this session, we need updating our local package index.
$ sudo apt-get update
Now we can install server
$ sudo apt-get install nginx
On Ubuntu 16.04, Nginx is configured to start running upon installation. if you have the ufw firewall running, as outlined in our initial setup guide, you will need to allow connections to Nginx. Nginx registers itself with ufw upon installation, so the procedure is rather straightforward. It is recommended that you enable the most restrictive profile that will still allow the traffic you want. Since we haven’t configured SSL for our server yet, in this guide, we will only need to allow traffic on port 80.
$ sudo ufw allow 'Nginx HTTP'
You can verify the change by typing:
$ sudo ufw status
With the new firewall rule added, you can test if the server is up and running by accessing your server’s domain name or public IP address in your web browser.
http://server_domain_or_IP
Step 2: Install MySQL to Manage Site Data
We can install this easily through this command
$ sudo apt-get install mysql-server
Now it will prompt to supply a root (administrative) password for use within the MySQL system. If we need different MySQL User then we can create
mysql -u root -p
It will prompt root password, to enter the root password and process further This example creates a new user called “arohamdb”, but you should replace it with a username that you like:
mysql > CREATE USER 'arohamdb'@'localhost' IDENTIFIED BY 'password';
At this point, new user has no permissions to do anything with the databases. Therefore, the first thing to do is to provide the user with access to the information they will need.
mysql > GRANT ALL PRIVILEGES ON * . * TO 'newuser'@'localhost';
Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.
mysql > FLUSH PRIVILEGES;
Step 3: Install PHP for Processing
We now have Nginx installed to serve our pages and MySQL installed to store and manage our data. Now we need to install PHP to access our web pages Since Nginx does not contain native PHP processing like some other web servers, we will need to install php-fpm, which stands for “fastCGI process manager”. We will tell Nginx to pass PHP requests to this software for processing. We can install this module and will also grab an additional helper package that will allow PHP to communicate with our database backend. The installation will pull in the necessary PHP core files. Do this by typing:
$ sudo apt-get install php-fpm php-mysql
Configure the PHP Processor
We now have our PHP components installed, but we need to make a slight configuration change to make our setup more secure. Open the main php-fpm configuration file with root privileges:
$ sudo nano /etc/php/7.0/fpm/php.ini
What we are looking for in this file is the parameter that sets cgi.fix_pathinfo. This will be commented out with a semi-colon (;) and set to “1” by default.
cgi.fix_pathinfo=0
Save and close the file when you are finished. Now, we just need to restart our PHP processor by typing:
$ sudo systemctl restart php7.0-fpm
Step 4: Configure Nginx to Use the PHP Processor
Now, we have all of the required components installed. The only configuration change we still need is to tell Nginx to use our PHP processor for dynamic content.
$ sudo nano /etc/nginx/sites-available/default
We need to make some changes to this file for our site. First, we need to create index.php as when a directory is requested that file will be served very first if available. We can modify the server_name directive to point to our server’s domain name or public IP address. For the actual PHP processing, we just need to uncomment a segment of the file that handles PHP requests by removing the pound symbols (#) from in front of each line. File should be look like that
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 server_domain_or_IP;

	location / {
	    try_files $uri $uri/ =404;
	}

	location ~ \.php$ {
	    include snippets/fastcgi-php.conf;
	    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
	}

	location ~ /\.ht {
	    deny all;
	}
}
When you’ve made the above changes, you can save and close the file. Now most important part is test your configuration file for syntax errors by typing:
$ sudo nginx -t
If any errors are reported, go back and recheck your file before continuing. When you are ready, reload Nginx to make the necessary changes:
$ sudo systemctl reload nginx
Step 5: Create a PHP File to Test Configuration
Your LEMP stack should now be completely set up. Now we need to test to validate that Nginx can correctly hand .php files. We can do this by creating a test PHP file in our document root. Open a new file called serverinfo.php within your document root in your text editor:
$ sudo nano /var/www/html/serverinfo.php
Type or paste the following lines into the new file. This is valid PHP code that will return information about our server:
<?php
phpinfo();
?>
When you are finished, save and close the file. Now, you can visit this page in your web browser by visiting your server’s domain name or public IP address followed by /serverinfo.php:
http://server_domain_or_IP/serverinfo.php
You should see a web page that has been generated by PHP with information about your server
Conclusion
We have configured a LEMP stack configured on your Ubuntu 16.04 server. This gives you a very flexible foundation for serving web content to your visitors.
See Also
How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04

Now is the time you start getting more online