how to install nginx on ubuntu 16.04

Introduction

Cloud VPS Hosting

Nginx is one of the most popular open source web server software. It is widely used for load balancing, reverse proxying, media streaming and much more. Nginx is the only all‑in‑on high-performance HTTP server, designed to serve high volume of concurrent connections, as an alternative to Apache web server. Today Nginx is installed on more then 450 million servers world wide, and is used by one of the highest traffic website on the web like Dropbox, Zynga games, Netflix. There is no question that Nginx is the  best-in-class web server and application delivery solution.  In this guide we will show you how to install Nginx on Ubuntu 16.40. Installing Nginx on Ubuntu 16.04, is an easy task, just carefully follow the steps bellow and your should have Nginx installed on your Ubuntu server in less then 10 min. This guide should work on other Ubuntu based Linux VPS systems as well but was tested and written for Ubuntu 16.04 VPS. Let’s get started.

Install Nginx

Nginx is available in the Ubuntu package repositories simple. First update the apt cache with the following command:

sudo apt update

and install Nginx by issuing:

sudo apt install nginx

Once the instillation is completed Nginx will be automatically started.
You can make sure that Nginx service is running with the following command:

sudo systemctl status nginx

The output should look like bellow:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2018-03-31 01:50:44 CDT; 8s ago
 Main PID: 716 (nginx)
   CGroup: /system.slice/nginx.service
           ├─716 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ├─717 nginx: worker process
           ├─718 nginx: worker process
           ├─719 nginx: worker process
           └─720 nginx: worker process

Open firewall ports

If you are using ufw you need to open HTTP port 80 and/or HTTPS port 433. Ufw comes with profiles based on the default ports of most common daemons and programs.

To open both Nginx ports run the following command:

sudo ufw allow 'Nginx Full'

To verify the change run:

sudo ufw status

The output should look like bellow:

Status: active

To                         Action      From
--                         ------      ----
Nginx Full                 ALLOW       Anywhere
Nginx Full (v6)            ALLOW       Anywhere (v6)

You can now open your browser, enter your server IP address into your browser address bar and you should see the default Nginx page.

 How To Install Nginx on Ubuntu 16.04

Managing Nginx service

You can manage the Nginx service same as any other systemd unit.

Start the nginx service with the following command:

sudo systemctl start nginx

Stop the service with:

sudo systemctl stop nginx

Restart the service with:

sudo systemctl restart nginx

Check the status of the service with:

sudo systemctl status nginx

Enable the service on system boot with:

sudo systemctl enable nginx

Disable the service on system boot with:

sudo systemctl disable nginx

Create a new server block

The default Nginx installation will have one server block enabled with a document root set to /var/www/html.
In this guide we will create a new server block for the domain example.com and set the document root to /var/www/example.com.

First create the domain document root with the following command:

sudo mkdir -p /var/www/example.com

and then create an index.html file with the following content:

sudo vim /var/www/example.com/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>example.com</title>
</head>
<body>
    <h1>example.com server block</h1>
</body>
</html>

Next create a new server block with the following content:

sudo vim /etc/nginx/sites-available/example.com.conf
server {
       listen 80;
       listen [::]:80;

       server_name example.com www.example.com;

       root /var/www/example.com;
       index index.html;

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

Activate the server block by creating a symbolic link :

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

Restart Nginx

Test the Nginx configuration and restart nginx:

sudo nginx -t
sudo systemctl restart nginx

Now if you enter example.com into your browser address bar you should see example.com server block.


How To Install Nginx on Ubuntu 16.04Of course, you don’t have to install Nginx on Ubuntu 16.04, if you use one of our Optimized Nginx Web Hosting Solutions, in which case you can simply ask our expert Linux admins to install and configure Nginx on Ubuntu 16.04 for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install Nginx on Ubuntu 16.04, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.