How to install and configure Nginx on Ubuntu 21.04

To Install and Configure Nginx on Ubuntu 21.04

Introduction:

For web servers, Nginx is a free and open-source application for Linux. It works as a reverse proxy server, directing web traffic to specific servers. Nginx can be used independently as a web server.

Installation Procedure:

Step 1: Check the OS version by using the following command

linuxhelp@linuxhelp:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 21.04
Release:	21.04
Codename:	hirsute

Step 2: Login by using the following command

linuxhelp@linuxhelp:~$ sudo -s
[sudo] password for linuxhelp:

Step 3: Changing to Home Directory

root@linuxhelp:/home/linuxhelp# cd ~

Step 4: Install the Nginx by using the following command

root@linuxhelp:~# apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libnginx-mod-http-geoip2 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream
  libnginx-mod-stream-geoip2 nginx-common nginx-core
Suggested packages:
  fcgiwrap nginx-doc
The following NEW packages will be installed:
  libnginx-mod-http-geoip2 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream
  libnginx-mod-stream-geoip2 nginx nginx-common nginx-core
0 upgraded, 9 newly installed, 0 to remove and 262 not upgraded.
Need to get 645 kB of archives.
After this operation, 2,382 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Ign:1 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 nginx-common all 1.18.0-6ubuntu8.2
Ign:2 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 libnginx-mod-http-geoip2 amd64 1.18.0-6ubuntu8.2
Ign:3 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 libnginx-mod-http-image-filter amd64 1.18.0-6ubuntu8.2
Ign:4 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 libnginx-mod-http-xslt-filter amd64 1.18.0-6ubuntu8.2
Ign:5 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 libnginx-mod-mail amd64 1.18.0-6ubuntu8.2
Ign:6 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 libnginx-mod-stream amd64 1.18.0-6ubuntu8.2
Ign:7 http://in.archive.ubuntu.com/ubuntu hirsute-updates/main amd64 libnginx-mod-stream-geoip2 amd64 1.18.0-6ubuntu8.2
Setting up nginx-core (1.18.0-6ubuntu8.2) ...
 * Upgrading binary nginx                      [ OK ] 
Setting up nginx (1.18.0-6ubuntu8.2) ...
Processing triggers for man-db (2.9.4-2) ...
Processing triggers for ufw (0.36-7.1) ...

Step 5: Check the status by using the following command

root@linuxhelp:~# 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 Fri 2021-10-22 18:53:24 IST; 1min 9s ago
       Docs: man:nginx(8)
    Process: 6904 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 6908 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 7026 (nginx)
      Tasks: 3 (limit: 2257)
     Memory: 3.8M
     CGroup: /system.slice/nginx.service
             ├─7026 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─7029 nginx: worker process
             └─7030 nginx: worker process

Oct 22 18:53:24 linuxhelp systemd[1]: Starting A high performance web server and a reverse proxy server...
Oct 22 18:53:24 linuxhelp systemd[1]: Started A high performance web server and a reverse proxy server.

Step 6: Making the Document Root Directory

root@linuxhelp:~# mkdir /var/www/test1.com

Step 7: Creating the index.html in Document Root Directory

root@linuxhelp:~# vi /var/www/test1.com/index.html
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to linuxhelp</h1>
</body>
</html>

Step 8: Give permission to the Document Root directory

root@linuxhelp:~# chown -R www-data. /var/www/test1.com/

Step 9: Create Server Block in the following location

root@linuxhelp:~# vi /etc/nginx/sites-available/test1.com
server {

listen 80;
listen [::]:80;
root /var/www/test1.com;
index index.html index.htm index.nginx-debian.html;
server_name test1.com www.test1.com;
location /
    { try_files $uri $uri/ =404;

  }
}

Step 10: Create symbolic links to enable the site

root@linuxhelp:~# ln -s /etc/nginx/sites-available/test1.com /etc/nginx/sites-enabled/test1.com

Step 11: Entering hosts in host file

root@linuxhelp:~# vi /etc/hosts

Step 12: Restart the Nginx by using the following commands

root@linuxhelp:~# systemctl restart nginx

Step 13: Ping by server block’s domain name in web browser

With this installation of NGINX server on UBUNTU 21.04 comes to end.

FAQ
Q
Who is using Nginx?
A
Some high-profile companies using Nginx include Autodesk, Atlassian, Intuit, T-Mobile,
GitLab, DuckDuckGo, Microsoft, IBM, Google, Adobe, Salesforce, VMWare, Xerox,
LinkedIn, Cisco, Facebook, Target, Citrix Systems, Twitter, Apple, Intel, and many more
Q
Is LiteSpeed better than Nginx?
A
LiteSpeed performs better than Nginx: it transfers files faster and uses less CPU and memory
Q
Why NGINX is fast?
A
This is the main reason why Nginx is faster, meaning it can serve more requests per second than Apache on the same hardware.
Q
Why NGINX is so popular?
A
Its light-weight resource utilization and its ability to scale easily on minimal hardware
Q
NGINX better than Apache?
A
NGINX performs 2.5 times faster than Apache
according to a benchmark test performed by running up to 1,000 simultaneous connections.