Grid Guides

Explore How System On Grid Can Work For You

How to Install Nginx on Ubuntu 18.04


Table of Contents

Introduction

Nginx is the popular web server in the world and it is the backbone for hosting most significant sites on the web. It is used as a reverse proxy and it is user-friendly then Apache.

In this guide, we will discuss how to Install Nginx on Ubuntu 18.04 server.

Prerequisites

Before installing Nginx, you should have access to an Ubuntu 18.04 server and created a non-root user account with sudo privileges by following our guide, Initial server setup with Ubuntu 18.04.

Installing Nginx

Nginx is available in Ubuntu’s repositories, so you have to install it from these repositories using apt packages.

First of all, you have to update the local packages index so that you have an access to most recent package listings and then you can install Nginx.

$ sudo apt update
$ sudo apt install nginx

Now apt will install Nginx and required dependencies to your server.

Adjusting the Firewall

If you want to test Nginx, the first step is the firewall software needs to be adjusted to allow access to the service. If you install Nginx, it registers itself as a service with ufw installation to allow Nginx access.

We can show the application programmes that ufw knows by using below command.

$ sudo ufw app list   

Then you will get the following list.

ufwlistofapplications

As above output, there are three profiles available for Nginx:

Nginx Full

This port opens both port 80 and port 443

Nginx HTTP

This port opens only port 80

Nginx HTTPS

This profile opens only port 443

You can configure SSL for your server by entering the below command.

$ sudo ufw allow 'Nginx HTTP'

You can check the status by entering the below command.

$ sudo ufw status

You can see HTTP traffic allowed in the following output.

nginxufwstatus

Checking Web Server

After installation, Ubuntu 18.04 starts Nginx. The web server starts and running. You can check whether the system is running or not by entering the below command.

$ systemctl status nginx

nginxstatus First of all, to test Nginx you have to request a page from Nginx. Then you can see the default Nginx landing page to confirm the software is working correctly through navigating to your server’s IP address. You can get an IP address in several ways. Use the following command to know the IP address.

$ ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

Now you can check it in your browser’s address bar.

Another way to check this, by entering the below command.

$ curl -4 icanhazip.com

If you have server’s IP address then enter it in your browser’s address bar.

http://216.200.116.243

Then you can see Nginx landing page:

nginxlandingpage

Then your server is running correctly.

Managing the Nginx Process

Now you are having a web server up and it is running.

Use the below command, to stop web server:

$ sudo systemctl stop nginx

Use the following command to start web server:

$ sudo systemctl start nginx

Enter the below command to restart:

$ sudo systemctl restart nginx

To reload the web server, enter the below command.

$ sudo systemctl reload nginx

If you want to disable the Nginx, enter the following command.

$ sudo systemctl disable nginx

If you want to re-enable Nginx, use the below command.

$ sudo systemctl enable nginx

Conclusion

So far we have discussed how to install Nginx.