Installing NGINX on Ubuntu

First you need to update the Ubuntu and then simply run sudo apt install as below

sudo apt update

When NGINX is installed it will create a folder called as /www inside /var/ directory in Ubuntu. Here is how folders look like before installation

/var$ ll
total 52
drwxr-xr-x 13 root root 4096 Apr 30 23:26 ./
drwxr-xr-x 19 root root 4096 Jun 15 14:39 ../
drwxr-xr-x 2 root root 4096 Apr 15 2020 backups/
drwxr-xr-x 12 root root 4096 Jun 15 14:40 cache/
drwxrwxrwt 2 root root 4096 Apr 30 23:25 crash/
drwxr-xr-x 38 root root 4096 Jun 15 14:39 lib/
drwxrwsr-x 2 root staff 4096 Apr 15 2020 local/
lrwxrwxrwx 1 root root 9 Apr 30 23:15 lock -> /run/lock/
drwxrwxr-x 9 root syslog 4096 Jun 15 14:39 log/
drwxrwsr-x 2 root mail 4096 Apr 30 23:15 mail/
drwxr-xr-x 2 root root 4096 Apr 30 23:15 opt/
lrwxrwxrwx 1 root root 4 Apr 30 23:15 run -> /run/
drwxr-xr-x 6 root root 4096 Apr 30 23:36 snap/
drwxr-xr-x 4 root root 4096 Apr 30 23:17 spool/
drwxrwxrwt 6 root root 4096 Jun 15 14:39 tmp/

Please note there is no /www directory as we have not installed NGINX yet. Please note that, if you have any other server such as apache in install, /www will already be there.
Now let us install NGINX.

sudo apt install nginx
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6
  libxpm4 nginx-common nginx-core
Suggested packages:
  libgd-tools fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8 libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6
  libxpm4 nginx nginx-common nginx-core
0 upgraded, 17 newly installed, 0 to remove and 60 not upgraded.
Need to get 2431 kB of archives.
After this operation, 7891 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://us-west-1.ec2.archive.ubuntu.com/ubuntu focal/main amd64 fonts-dejavu-core all 2.37-1 [1041 kB]
Get:2 http://us-west-1.ec2.archive.ubuntu.com/ubuntu focal/main amd64 fontconfig-config all 2.13.1-2ubuntu3 [28.8 kB]
Get:3 http://us-west-1.ec2.archive.ubuntu.com/ubuntu focal/main amd64 libfontconfig1 amd64 2.13.1-2ubuntu3 [114 kB]
Get:4 http://us-west-1.ec2.archive.ubuntu.com/ubuntu focal-updates/main amd64 libjpeg-turbo8 amd64 2.0.3-0ubuntu1.20.04.1 [117 kB]
Get:5 http://us-west-1.ec2.archive.ubuntu.com/ubuntu focal/main amd64 libjpeg8 amd64 8c-2ubuntu8 [2194 B]
......
Setting up libnginx-mod-stream (1.18.0-0ubuntu1.2) ...
Setting up libtiff5:amd64 (4.1.0+git191117-2ubuntu0.20.04.1) ...
Setting up libfontconfig1:amd64 (2.13.1-2ubuntu3) ...
Setting up libgd3:amd64 (2.2.5-5.2ubuntu2) ...
Setting up libnginx-mod-http-image-filter (1.18.0-0ubuntu1.2) ...
Setting up nginx-core (1.18.0-0ubuntu1.2) ...
Setting up nginx (1.18.0-0ubuntu1.2) ...
Processing triggers for ufw (0.36-6) ...
Processing triggers for systemd (245.4-4ubuntu3.6) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

After installation /www will be created. Here is how it looks like after installation

var$ ll
total 56
drwxr-xr-x 14 root root 4096 Jun 15 14:42 ./
drwxr-xr-x 19 root root 4096 Jun 15 14:39 ../
drwxr-xr-x 2 root root 4096 Apr 15 2020 backups/
drwxr-xr-x 12 root root 4096 Jun 15 14:40 cache/
drwxrwxrwt 2 root root 4096 Apr 30 23:25 crash/
drwxr-xr-x 39 root root 4096 Jun 15 14:42 lib/
drwxrwsr-x 2 root staff 4096 Apr 15 2020 local/
lrwxrwxrwx 1 root root 9 Apr 30 23:15 lock -> /run/lock/
drwxrwxr-x 10 root syslog 4096 Jun 15 14:42 log/
drwxrwsr-x 2 root mail 4096 Apr 30 23:15 mail/
drwxr-xr-x 2 root root 4096 Apr 30 23:15 opt/
lrwxrwxrwx 1 root root 4 Apr 30 23:15 run -> /run/
drwxr-xr-x 6 root root 4096 Apr 30 23:36 snap/
drwxr-xr-x 4 root root 4096 Apr 30 23:17 spool/
drwxrwxrwt 6 root root 4096 Jun 15 14:39 tmp/
drwxr-xr-x 3 root root 4096 Jun 15 14:42 www/

NGINX registers itself as a service with ufw after installation.

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

Let us allow traffic using NGINX HTTP by using following command

$sudo ufw allow 'Nginx HTTP'
Rules updated
Rules updated (v6)

Finally just to ensure NGINX is running file, you can check the status using following command

$ sudo service nginx status
● 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 2021-06-15 14:42:10 UTC; 2min 35s ago
Docs: man:nginx(8)
Main PID: 2030 (nginx)
Tasks: 2 (limit: 1160)
Memory: 5.3M
CGroup: /system.slice/nginx.service
├─2030 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2031 nginx: worker process

Jun 15 14:42:10 ip-172-31-7-66 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 15 14:42:10 ip-172-31-7-66 systemd[1]: Started A high performance web server and a reverse proxy server.

other commands.

$sudo service nginx status
$sudo service nginx start
$sudo service nginx stop
$sudo service nginx restart
$sudo service nginx reload
$sudo service nginx disable
$sudo service nginx enable

Now you can access your server and it will display below page

Hope this is helpful. Please let me know if you have any query or getting any unexpected error.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.