NGINX Car by Walker Cahall

NGINX Car by Walker Cahall

Verified and Tested 03/3/15

Introduction

This how-to will show you how to install LEMP on a Ubuntu 16.04 cloud server or VPS. LEMP is a web service stack that consists of a Linux operating system, Nginx (pronounced “engine-x”), MySQL, and PHP. The main difference between LAMP and LEMP is that LAMP uses Apache, and LEMP uses Nginx. LEMP has been gaining popularity in the last few years because it excels in speed and scalability.

Prerequisites

A server with Ubuntu 16.04 installed.  If you do not have a server, consider SSD Cloud Hosting from Atlatnic.Net.

Installing LEMP on a Ubuntu 16.04  Cloud Server Or VPS

First we want to make sure that your server is up to date by running the commands:

sudo apt update
sudo apt upgrade

Note: Depending on your installation you may need to remove apache2. You can do that by running the commands:

sudo apt remove apache2*

Followed by:

sudo apt autoremove

 

Installing Nginx on Ubuntu 16.04

To install Nginx, use the command:

sudo apt install nginx

When it asks “Do you want to continue?”, hit Enter.

Start the Nginx service with the following command:

sudo service nginx start

We can now test Nginx by going to your hostname or IP address in your browser’s address bar. If you do not know your server’s IP address, you can run the following command:

ifconfig

You should get a result similar to the image below.

An example of ifconfig showing the IP address 172.20.6.154

An example of ifconfig showing the IP address 172.20.6.154

In our example, 192.168.0.192 is the IP address. So in our browser we would go to http://192.168.0.192.

You should see a web page that looks like the image below.

This example is the default nginx web page on Ubuntu 16.04

This example is the default nginx web page on Ubuntu 16.04

Now that Nginx is installed, we can move on to installing MySQL.

Installing MySQL on Ubuntu 16.04

Install MySQL with the command:

sudo apt install mysql-server

When it asks “Do you want to continue?”, hit Enter.

Shortly after, a screen similar to the image below will appear.  You need enter a password for your MySQL root user. It should be a strong password.

Insert your secure password for your new MySQL root password

Insert your secure password for your new MySQL root password

Hit enter to continue. Once you have hit enter, a new screen will appear prompting you to re-enter the password you just picked.

Retype your MySQL password

Retype your MySQL password

Now that MySQL is installed we need to do the MySQL secure installation by running the command:

sudo mysql_secure_installation

Enter your MySQL root password. When it asks “Change the root password?”, type “N” followed by Enter. The rest of the questions are up to you. For standard installations, you can hit Enter for the defaults. It will look similar to the code box below.

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n]
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n]
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

 

Now that MySQL is installed, we can now install PHP.

Installing PHP on Ubuntu 16.04

Install PHP with the following command:

sudo apt install php php-fpm php7.0-mysql

When it asks “Do you want to continue?”, hit Enter.

For Nginx to work with PHP correctly, we need to edit an Nginx configuration file. In this how-to, we are going to use a simple Nginx config file.

First, we need to move the original configuration file to a new file name. Run the command:

sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old

Using a text editor of your choice, we are going to make a file called default in /etc/nginx/sites-available. For nano use the command:

sudo nano /etc/nginx/sites-available/default

Copy the following into your text editor:

server {
        listen       80;
        server_name  your_site_name.com;
        root /usr/share/nginx/html;
        index index.php index.html;

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

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;

        location = /50x.html {
                root /var/www/html;
        }

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

In nano, to exit and save, hit Ctrl+x , type “y”, and then Enter.

Since we made changes to the configuration file, we need to restart Nginx, by running the command:

sudo service nginx restart

We are now going to make a simple PHP page to test.

Using a text editor of your choice, we are going create a file called info.php in /usr/share/nginx/html/.

sudo nano /usr/share/nginx/html/info.php

Copy the following into your text editor.

<?php
phpinfo();
?>

In your browser, you can go to http://Your-Hostname/info.php or http://Your-IP-Address/info.php. As above, in this example, we would use http://192.168.0.192/info.php.

You should see a web page similar to the one below.

An example of the info.php web page

An example of the info.php web page

Once you are done testing, it is a good idea to remove the info.php file, since it may give a potential attacker information that can be used to craft a specific attack against your server. To do that run the command:

sudo rm /usr/share/nginx/html/info.php

Congratulations, you have installed LEMP on Ubuntu 16.04. Thank you for following this how-to. Please check back for more updates, or take a look at our how-to on Installing WordPress on Ubuntu 16.04!