How to install nginx on ubuntu 20.04

March 29th, 2021
Author Avinash Sharma

How to Install NGINX on Ubuntu 20.04

What is Nginx?

NGINX is open-source software for web servers, reverse proxy, caching, load balancing, media streaming, and more.

It started out as a web server designed for maximum performance and stability for web hosting.

More than 75% of the website that is hosted online are hosted on Nginx and Apache Web Server.

Table of Contents


Step 1 – Update The Ubuntu server

Before Installing the Nginx web server we need to upgrade the operating system, By using the following command:

sudo apt-get update
sudo apt-get upgrade

Step 2 - Installing Nginx

sudo apt install nginx

Step 3 – Adjusting the Firewall

What is ufw?

The Uncomplicated Firewall (ufw), is a frontend for IPTables and is particularly well-suited for host-based firewalls for web servers. ufw provides a framework for managing Netfilter

(Basically to filter out what incoming traffic is to be allowed) and a command-line (CLI) interface for manipulating the firewall.

You should get a listing of the application profiles that have the access to the external connections:

sudo ufw app list
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

As observed by the output, there are three profiles available for Nginx config:

Screenshot 2021-05-29 at 11 53 48 PM

It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured (To have height secret ). Right now, we will only need to allow traffic on port 80 it's our wish that if we want to enable the HTTP or HTTPS to your website.

In the future, we can add an SSL certificate in order to get the full advantage of the HTTPS protocol Heare is the link on how to enable the SSL certificate.

If not Enabled you can enable this by typing:

sudo ufw allow 'Nginx HTTP'

You can is the change by typing:

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

Step 4 – Checking your Web Server is running or not:

systemctl status nginx

This command gives you the status of the Nginx server:

nginx

Cheat Sheet for Nginx

Now that you have your web server up and running, let’s review some basic management commands (Cheat Sheet for Nginx).

To stop your Nginx web server, type:

sudo systemctl stop nginx

To start the Nginx web server when it is stopped, type:

sudo systemctl start nginx

To stop and then start (restart) the service again, type:

sudo systemctl restart nginx

If you are only making configuration changes in The Nginx config, Nginx can often reload without dropping connections, this is a great feature. To do this, type:

sudo systemctl reload nginx

By default, The Nginx server is configured to start automatically when the server boots. If that’s not what you want(to reduce the load on the server), you can disable this behavior by typing:

sudo systemctl disable nginx

To enable the service to start up at boot time, you can type:

sudo systemctl enable nginx

You have now learned basic management commands and should be ready to configure the site to host more than one domain.