How To Install Nginx on Ubuntu 18.04-16.04

How To Install Nginx on Ubuntu 18.04-16.04

Nginx web server is one of the leading web servers used worldwide and Nginx is an open-source web server. NGINX is a very light and fast web server, which is highly optimized and it can be also used as a reverse proxy, HTTP cache, and load balancer. in the following steps, I will show you how to install Nginx on ubuntu 18.04/16.04 Linux machine.

Install Nginx on Ubuntu 18.04-16.04

install nginx on ubuntu 18.04/16.04 with following commands

Update the system using apt update command

sudo apt update -y

install nginx on ubuntu 18.04 with the following command

sudo apt install nginx -y

verify is nginx is running or not by using status command

sudo systemctl status nginx

ubuntu@ip-10-0-0-25:~$ sudo systemctl status nginx
? nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: en
   Active: active (running) since Sun 2019-06-16 04:22:29 UTC; 1h 0min ago
     Docs: man:nginx(8)
  Process: 2359 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code
  Process: 2347 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process
 Main PID: 2363 (nginx)
    Tasks: 2 (limit: 1152)
   CGroup: /system.slice/nginx.service
           +-2363 nginx: master process /usr/sbin/nginx -g daemon on; master_pro
           +-2365 nginx: worker process

Jun 16 04:22:29 ip-10-0-0-25 systemd[1]: Starting A high performance web server
Jun 16 04:22:29 ip-10-0-0-25 systemd[1]: nginx.service: Failed to parse PID from
Jun 16 04:22:29 ip-1

here nginx is running, if not started in your machine you can start nginx on your machine with start command

sudo systemctl start nginx

stop, start, enable, disable the nginx

use the following commands to stop, start, enable, disable the nginx

sudo systemctl status nginx  – check the nginx status
sudo systemctl stop nginx    – stops the  nginx service
sudo systemctl start nginx   – start the nginx service
sudo systemctl enable nginx  – enables Nginx at startup
sudo systemctl disable nginx – prevents Nginx at startup

Allow Nginx Traffic Through ufw Firewall

if you are using ufw firewall in your Ubuntu Linux machine, you need to allow few ports in ufw firewall to work with nginx. let's see what are those.

sudo ufw app list

ubuntu@ip-10-0-0-25:~$ sudo ufw app list
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

it will show you the list of apps running on your machine. Here you can see 4 apps running Nginx HTTP, Nginx HTTPS, Nginx FULL, OpenSSH.

 

  • Nginx HTTP: This profile opens the port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile opens the port 443 (TLS/SSL encrypted traffic)
  • Nginx Full: This profile opens both ports 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)

Open port 80 for HTTP access:

by adding port to 80 to firewall ufw you can open port 80, you can use any one of the following commands

ufw allow  'Nginx HTTP'

or

ufw allow 80

Open port 443 for HTTPs access:

in a similar way, you can use any one of the following commands to open port 443

ufw allow  'Nginx HTTPS'

or

ufw allow 443

verify the firewall

you can check the firewall status of open ports using below command.

$ sudo ufw status numbered

ubuntu@ip-10-0-0-25:~$ sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 80                         ALLOW IN    Anywhere
[ 2] 443                        ALLOW IN    Anywhere
[ 3] 80 (v6)                    ALLOW IN    Anywhere (v6)
[ 4] 443 (v6)                   ALLOW IN    Anywhere (v6)

NOTE: if you are using aws ec2 instances open port no 80 for http and open port 443 for https in outgoing rules.

Verify Nginx is working on Ubuntu 18.04 LTS

by default, Nginx will start on http://ip     like http://3.95.60.3/

open the browser and enter ip address of the machine and hit enter you can see the nginx default page.

Leave a Reply

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