How To Install Nginx Web Server On Ubuntu 20.04 / 19.10

We can install and configure the Nginx web server on ubuntu 20.04 and also used it for reverse proxy and load balancing. Let’s see, how to install Nginx on ubuntu.

How To Install Nginx Web Server On Ubuntu 20.04 / 19.10

Nginx(Pronounced as “Engine-X”) is an open-source web server that often uses as a reverse proxy or HTTP cache. This is available for Linux.

Check Out: How To Configure HTTP Based File Server On CentOS 8

We will update the Ubuntu system using the following commands and then install the Nginx. you don’t need to add any repository. 

sudo apt update
sudo apt upgrade -y
sudo apt install nginx

Once the installation finished, we need to start and enable the Nginx service.

sudo systemctl start nginx
sudo systemctl enable ngnix
sudo systemctl status nginx

Hit the “localhost” in any browser. you must be seeing the “Welcome to Nginx” message. 

Nginx is installed and running on your ubuntu machine 20.04. 

Check Out: How To Create LVM and Mount On Directory On Ubuntu 20.04

Allow Services in the firewall Rules:

We need to allow the Nginx HTTP in the firewall rules using the following commands.

ubuntu@ubuntu:~$ sudo ufw allow "Nginx HTTP"                                                              ## For port 80
Rule added
Rule added (v6)
ubuntu@ubuntu:~$ sudo ufw allow "Nginx HTTPS"                                                            ## For port 443
Rule added
Rule added (v6)

Check the status using the below commands.

ubuntu@ubuntu:~$ sudo ufw status | grep Nginx
Nginx  HTTP                                         ALLOW                                                  Anywhere 
Nginx  HTTPS                                       ALLOW                                                  Anywhere 
Nginx  HTTP (v6)                                 ALLOW                                                  Anywhere (v6) 
Nginx  HTTPS (v6)                               ALLOW                                                  Anywhere (v6)

Create a VirtualHost On Nginx:

You can see the default VirtualHost directory is placed at /var/www/html where you will keep your website data. you can also keep your website data at other locations as well.

Create a simple web page HTML file using the below command. 

echo " testing nginx website" > /var/www/html/index.html

Host Multiple Website On Nginx: 

You will need to create a virtual host to manage all the websites on the Nginx server. Let’s create a file name abc.conf inside the directory /etc/nginx/sites-enabled

vim abc.conf

server {
                listen 80;
                root /var/www/abc;
                server_name abc.com;

location / {
                      # First attempt to serve request as file, then
                      # as directory, then fall back to displaying a 404.
                      try_files $uri $uri/ =404;
}

}
server{
                 listen 80;
                 root /var/www/xyz;
                 server_name xyz.com;

location / {
                       # First attempt to serve request as file, then
                       # as directory, then fall back to displaying a 404.
                       try_files $uri $uri/ =404;
    }

}

Create a two website folder with the name of abc and xyz

sudo mkdir /var/www/abc
sudo mkdir /var/www/xyz

Check Out: How To Install Git Bash and Access AWS EC2 Instance

Make an index.html file in both directories using the below command. 

echo "xyz website" >> /var/www/xyz/index.html
echo "abc website" >> /var/www/abc/index.html

Once you have done the above task, we need to create a soft link to enable the virtual host. 

sudo ln -s /etc/nginx/sites-enabled/abc.conf /etc/nginx/sites-available/

Reload the Nginx service and check the website abc.com and xyz.com. you should be able to see the result. 

sudo systemctl reload nginx

Make an entry in /etc/hosts

127.0.0.1 abc.com xyz.com

You can check the Nginx logs at this location /var/log/nginx/

That’s it.

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment