Recommended VPS Complete list of best VPS hosting providers.

How to Setup LEMP On Ubuntu 16.04

LEMP stack is another most setup of a server or a virtual private server (VPS) besides LAMP. LEMP (or LNMP) stands for Linux, Nginx, MySQL and PHP. Nginx is well known for its stability and scale-ability. It’s a web server that can server millions of visitors a day with no single blip. For some people they simply prefer Nginx more than Apache except for those who can’t live without .htaccess might still stick with Apache. However, even some users combine Apache and Nginx together by putting it in front of Apache serving as a proxy server.

This tutorial shows you complete steps to build a working server using Nginx (v1.10.0), Apache 2, MySQL (v5.7.12) and PHP7 (fpm) on Ubuntu 16.04 (cloud/vps) server. As usual, I’ll try my best to put screenshot pics on each step to make sure you can have general picture on how all the steps are done successfully.

Also read:

Prerequisites

  1. A SSH client like Bitvise or Putty and basic knowledge about how to use it.
  2. Basic knowledge about common Unix command to SSH to your server.
  3. A server or VPS with at least 256MB of RAM (OpenVZ) but 512MB is recommended if you are using KVM or Xen.
  4. Ubuntu 16.04 either 32-bit or 64-bit.
  5. About 30 minutes of your time.
  6. a cup of coffee or tea.

Confused on choosing which VPS provider to setup an Apache based hosting server? Read my recommended VPS providers or this list of top low end cloud server providers. Quick recommendation: Digital Ocean, Atlantic.net or Ramnode.

In this guide I am using a 512MB RAM Cloud VPS from Atlantic running Ubuntu 16.04 x64.

Do not forget to always replace domain.tld with your own domain name and replace x.x.x.x with your server IP address.

Provision a cloud server

Step 1 – Login to Atlantic.net > click on Servers > Add Server.

2016-04-17_210817

Step 2 – Give your server a name and choose in which location you want it to be hosted at. Currently, Atlantic.net has 6 different locations available: New York, Dallas, Orlando, San Fransisco, Toronto (CA), and London (UK).

Create A Server on Atlantic

Step 3 – Choose Ubuntu 16.04 LTS as the operating system for your cloud vps.

Ubuntu 16.04 Atlantic Server

Step 4 – Decide which plan you want (and you need).

Choose VPS Plan

Step 5 – Finally, click on the Create Server button.

2016-04-17_211935

Step 6 – The process should take less than 1 minute to complete.

building cloud server

Step 6 – Once done, the login details will be emailed to your inbox.

Check Email

Step 7 – Login to your server via Putty.

Login Putty

Step 8 – Type this command to change default Hostname. Use proper FQDN hostname format:

hostname host.domain.tld

## example :
hostname atlantic.servermom.org

Change Hotsname Ubuntu

Step 9 – Close Putty and re-login using new session and you should now see it has changed. It is also a good practice to change given root password with your own.

relogin putty

Instal Nginx Webserver

Step 1 – Let’s first update package repositories on your Ubuntu system. Just issue this command :

apt-get update -y
apt-get upgrade -y

Step 2 – Now you can install Nginx, again, by using simple apt-get command.

apt-get install nginx -y

Install Nginx on Ubuntu 16.04

OPTIONAL: If you have ufw installed on your server, then you should then allow Nginx to use port for web traffic – port 80. Just use this command to enable it :

ufw allow 'Nginx HTTP'
ufw status

Step 3 – Open up your favorite web browser and try accessing your vps through IP address. You should now see default Nginx page.

nginx test page ubuntu 16.04 sample

Install MySQL Database Server

MySQL is still the most popular database server for many users although part of them have shifted their preference to MariaDB. Below is the way you can install MySQL and how to set it up on Nginx-based server

Step 1 – Issue command below :

apt-get install mysql-server -y

apt-get install mysql on nginx ubuntu server

Step 2 – You’ll then be asked to define a password for your root mysql account. You have to enter it twice and make sure you don’t forget the password.

Define MySQL Password

confirm mysql root password

Step 3 – MySQL will then continuing the installation process until it finished.

Continue Mysql Install

Step 4 – You may also issue standard mysql setup command :

mysql_secure_installation

Just answer each question accordingly.

Install PHP7 (php7-fpm)

Unlike Apache, Nginx doesn’t have native support to PHP processing. In order to make PHP works together Nginx, additional PHP module is needed and is called fastCGI process manager or it usually known as php-fpm. On top of that, you may need to do some additional settings too adjust Nginx and to make it run flawlessly with PHP processing. Simply follow steps below..

Installing PHP :

Step 1 – Install PHP with its php-fpm module (fastCGI process manager) and php-mysql module (so PHP can cooperate with MySQL server).

apt-get install php-fpm php-mysql

installing php7 fpm on nginx ubuntu 1604

Using that simple command, several PHP modules will also be installed including the PHP processing unit itself. The modules installed like :

  • php-common
  • php7.0-cli
  • php7.0-common
  • php7.0-fpm
  • php7.0-json
  • php7.0-mysql
  • php7.0-opcache
  • php7.0-readline

Setting up PHP7 fpm :

Step 2 – Use your favorite text editor to adjust php configuration. In this example I use Nano.

nano /etc/php/7.0/fpm/php.ini

Step 3 – Look for (in Nano it is Control+W) a line of cgi.fix_pathinfo. Change that line as following

## before the change
;cgi.fix_pathinfo=1

## after the change
cgi.fix_pathinfo=0

change cgi fix path php fpm

Save changes and exit the editor (which in Nano it is Control+O then Control+X).

Step 4 – Now you need to restart php-fpm service.

systemctl restart php7.0-fpm

restart php fpm service

Step 5 – Now let’s configure Nginx to use the newly installed PHP processor. For this task, you have to change Nginx virtual host (server block) so you can also add your default domain as well. Edit it using any text editor like Nano which is for me easiest :

mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup
nano /etc/nginx/sites-available/default

The first line of the command above is to create a backup of your existing server block file whereas the next one will launch text editor.

Step 6 – Now put these lines of Nginx configuration to the editor. Please adjust some parts like your domain name and server IP:

server {
    listen x.x.x.x:80;
    listen [::]:80 ipv6only=on;

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

    server_name domain.tld;

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

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

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

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

few notes :

  • Replace x.x.x.x with your IPv4 server address
  • Replace [::] with your IPv6 server address.
  • Remove the listen [::]:80 ipv6only=on; if you don’t have IPv6.
  • You may replace /var/www/html with your own web directory like /var/www/domain.tld. However, you can leave it as its is for the sake of following this tutorial.
  • Replace domain.tld with your own domain name.

editing nginx server bloc

In the example above I’ve just commented out IPv6 config line because currently Atlantic.net doesn’t have IPv6 yet.

Step 7 – Issue this command to check for configuration error(s) if any.

nginx -t

Step 8 – Finally restart Nginx.

systemctl reload nginx

restart nginx service ubuntu 1604

Step 9 – Create PHP test file.

nano /var/www/html/info.php

Step 10 – Put this simple PHP code in there. The code allows you to see that PHP processing can really render PHP scripts as well as to check current PHP configurations:

<?php
phpinfo();
?>

Do not forget to save then exit the editor (Control+O then Control+X)

php info scipts

Step 11 – Finally, let’s verify it. Launch Firefox or Chrome and access your server through the IP. Eventually, if you’ve also change your DNS records, you can use your domain to access it

http://x.x.x.x/info.php
http://domain.tld/info.php

Check PHP Version Info

Step 12 – So you now know that the LEMP stack you’ve just set up is working properly. You can now remove that PHP test file since it might be misused.

rm /var/www/html/info.php

Finally, at this point you have installed and configured full LEMP stack properly. It means you have had a foundation to host any websites on it. All you have to do is just adding some more additional Virtual Host / Server Block for each domain you want to host to that server. Enjoy..

Next : How to create Nginx Server Block file to add new website on your Ubuntu 16.04 LEMP server.

One Comment

Add a Comment

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

Get more stuff like this
in your inbox

Subscribe and get interesting stuff plus faster updates to your email.