In today’s world we have many hosted platforms available across internet through which we can we can host our website, Most easy approach which usually people follow is to manage to ask someone to manage their website, they just manage their website data.
Mostly Hosted platforms is managed to some third party organisations which provide some login to access and manage website data.

But Sometime there are situations where we like to manage whole hosting platform or like to act as third-party provider which used to manage complete platforms as solutions. So for it we have many Web-Server like which used to host website.
Nginx is most popular Web server used to manage some of heavy traffic website in world. It is resource friendly and well occupied with various important features.

In this post we will try to understand how we can install and work on Nginx on Ubuntu 18.04 Machine.

SetUp

We are using Ubuntu 18.04 as of latest July 2020.

root@srv29:~# lsb_release -d
Description:    Ubuntu 18.04.4 LTS
root@srv29:~# uname -r
4.15.0-111-generic

Nginx version used for this post

root@srv29:~# nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

Installation

For installation of Nginx we are going to use Ubuntu package manager apt-get with below command

sudo apt-get install nginx

It could ask you for acceptance while installing it, you need to accept it. It will install automatically on you machine.Installation time depend upon systems bandwidth, resource and previously installed packages.

Firewalls

I suggest , please disable firewall. But in case you like to work with firewall then you need to open ports for Nginx. Let’s see how we can allow access for Nginx on Ubuntu firewall. Ubuntu firewall ufw has very easy method to allow any application.

root@srv29:~# ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    Anywhere                  
80                         ALLOW IN    192.168.0.0/24            
443                        ALLOW IN    192.168.0.0/24            

root@srv29:~# ufw app list
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

So in above output we can see, firewall allow 80(HTTP), 443(HTTPS) and SSH from outside.So we are good to start nginx and work on it.

Start Nginx

As we above installed and open ports for Nginx, now we should start nginx service, but Ubuntu automatically start nginx services after installation of its package.

root@srv29:~# 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: enabled)
   Active: active (running) since Tue 2020-07-21 19:33:10 UTC; 1h 42min ago
     Docs: man:nginx(8)
 Main PID: 31555 (nginx)
    Tasks: 2 (limit: 2317)
   CGroup: /system.slice/nginx.service
           ├─31555 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─31558 nginx: worker process

Jul 21 19:33:10 srv29.geekpills.com systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 21 19:33:10 srv29.geekpills.com systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jul 21 19:33:10 srv29.geekpills.com systemd[1]: Started A high performance web server and a reverse proxy server.

So Nginx service is running on Ubuntu 18.04 Machine. let’s try to connect it from another machine.So let’s try to connect http://Server-IP. You will get below screen on browser

This is default page of nginx. To start/stop/restart/reload we could use systemctl commands.

# systemctl status nginx
# systemctl start nginx
# systemctl stop nginx
# systemctl restart nginx
# systemctl reload nginx

# systemctl enable nginx
# systemctl disable nginx

Nginx Server is using /var/www/html as DocumentRoot, which contain all its hosting data which we like to host through this website. By-default this directory contain one HTML file which will host once nginx start working.

root@srv29:/var/www/html# ll index.nginx-debian.html 
-rw-r--r-- 1 root root 612 Jul 21 19:33 index.nginx-debian.html

We can check this on browser with Machine IP or with Machine name like below.Default nginx file.

But if we replace with own index file, then it will privileges over default index file.

root@srv29:/var/www/html# ll
-rw-r--r-- 1 root root   35 Jul 21 21:26 index.html
-rw-r--r-- 1 root root  612 Jul 21 19:33 index.nginx-debian.html

After nginx reload, it will get in effect like below.

But if we want to host another website on same machine, We need to host it like virtual hosting in Apache called Server blocks. You need to add your own domain at Domain name Server(DNS).
As mentioned above we have Nginx document directory /var/www/html which contain all hosted files for first Website hosted from this Machine, but if we need to host another website, it good to create another DocumentRoot for second website. So to create another website another hostalias on same machine like below.

root@srv29:/var/www/html# ls -ld  /var/www/srv29-1.geekpills.com/html/
drwxr-xr-x 2 root root 4096 Jul 26 01:30 /var/www/srv29-1.geekpills.com/html/

I am using as root to run these Website to maintain this hosted files, But in case you are using another user to maintain this Website, Then i should suggest please change ownership of this directory index for this user like below.

chown USER:GROUP -R /var/www/srv29-1.geekpills.com

This will access to change data on this directory to that user, so that user or team can change accordingly to Website requirement anytime.

Let’s create sample index HTML file to show once sire is ready to present on browser.

In Nginx, we need to create server block with all details of new hosting documents in /etc/nginx/sites-available in same way like default website already has

root@srv29:~# cat  /etc/nginx/sites-available/srv29-1.geekpills.com 
server {
        listen 80;
        listen [::]:80;

        root /var/www/srv29-1.geekpills.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name srv29-1.geekpills.com www.srv29-1.geekpills.com;

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

Above configuration file will redirect all request comes to nginx towards DocumentRoot of new Website hosting index file. To we get in effect all these configuration we need to enable New Website like below.

root@srv29:~# ln -s /etc/nginx/sites-available/srv29-1.geekpills.com /etc/nginx/sites-enabled/

root@srv29:~# ll /etc/nginx/sites-enabled/
total 8
drwxr-xr-x 2 root root 4096 Jul 26 02:56 ./
drwxr-xr-x 8 root root 4096 Jul 26 01:18 ../
lrwxrwxrwx 1 root root   34 Jul 21 19:33 default -> /etc/nginx/sites-available/default
lrwxrwxrwx 1 root root   48 Jul 26 01:19 srv29-1.geekpills.com -> /etc/nginx/sites-available/srv29-1.geekpills.com

Now Check nginx configuration with below command

root@srv29:~# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

And restart nginx service.

systemctl restart nginx

Now we can check New website through Web browser like below.

So in this post we saw some of important file are directory where we contain some important directory structure.

    • /etc/nginx — Main nginx configuration directory which contain various nginx configuration file which helps to configure Nginx.
    • /etc/nginx/nginx.conf> — This file is main configuration file of nginx which get read first when naginx get any service request on its listen port.
    • /etc/nginx/sites-available/ — This contain Website directory which has all configuration of various website include its documentRoot etc
    • /etc/nginx/sites-enabled/ — This directory basically contain link of enabled website, if site is present in above available directory and not linked here then it will not run. It’s link need to present in this directory to publish Website

    Through Above discussion now we know how to run Nginx on Ubuntu machine.