Install Nginx and configure Virtual host in Ubuntu 20.04

Nginx is one of the popular websites. It is used as a web server in the popular LEMP stack being the E in the acronym. It is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache.

In this guide, we will explore how to use nginx in Ubuntu 20.04, i.e. Installing nginx and configuring nginx to serve a virual site.

Installing Nginx

update our local package index so that we have access to the most recent package listings

1
sudo apt update

Install nginx

1
sudo apt install nginx

Update firewall

List available ufw apps:

1
sudo ufw app list

Sample output:

1
2
3
4
5
6
7
$ sudo ufw app list

    Available applications:
      Nginx Full
      Nginx HTTP
      Nginx HTTPS
      OpenSSH

The listed Nginx profiles:

  • Nginx Full - Both http - 80 and https - 443
  • Nginx HTTP - only http traffic 80
  • Nginx HTTPS - Only https traffic 443 - TLS/SSL encrypted traffic

Enable the profile you want - both http and https in our case:

1
sudo ufw allow 'Nginx Full'

Verify the change

1
sudo ufw status

The above command will output which traffic is allowed.

Configuring nginx

Check installed version

1
sudo apt list --installed | grep nginx

Check systemd service

1
sudo systemctl status nginx

You should see this output showing that nginx is running

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
● 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-07-30 14:07:33 UTC; 2min 42s ago
       Docs: man:nginx(8)
   Main PID: 4684 (nginx)
      Tasks: 5 (limit: 9257)
     CGroup: /system.slice/nginx.service
             ├─4684 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─4685 nginx: worker process
             ├─4686 nginx: worker process
             ├─4687 nginx: worker process
             └─4688 nginx: worker process

Jul 30 14:07:33 frhb64566ds systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 30 14:07:33 frhb64566ds systemd[1]: Started A high performance web server and a reverse proxy server.

To ensure that the nginx service is always running even after reboots, use this systemnd command to enable:

1
sudo systemctl enable nginx

You can use these other systemd commands to manage nginx:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# To start the service
sudo systemctl start nginx

# To restart the service
sudo systemctl restart nginx

# To reload the service config
sudo systemctl reload nginx

# To stop the service config
sudo systemctl restart nginx

# To disable the service config
sudo systemctl restart nginx

To confirm that nginx is actually running and is reachable, use this command:

1
curl http://server_ip/

where server_ip is the ip of your server. If by any reason you do not know the public ip of your server use this to check:

1
curl -4 icanhazip.com

Then:

1
curl http://178.170.10.54/

You should get default nginx page.

Serving simple static page nginx

Lets take a look at an example to serve static page with nginx. Static page is html and css that doesn’t need any server side rendering. We are going to use the example domain your.domain.com in our case.

Steps:

  • Create the directory to store our static content (we will use /var/www/your.domain.com)
1
sudo mkdir -p /var/www/your.domain.com
  • Ensure the created directory is accessible by the current user
1
2
3
sudo chown -R ${USER}:${USER} /var/www/your.domain.com

sudo chmod -R 755 /var/www/your.domain.com
  • Add a sample file to the directory (/var/www/your.domain.com/index.html)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
cat > /var/www/your.domain.com/index.html <<EOF

<html>
<head>
<title>Hola from your.domain.com!</title>
</head>
<body>
<h1>Hey!</h1>
<p>Our site <i>your.domain.com</i> is working as expected.</p>
</body>
</html>
EOF
  • Create nginx configuration in the directory /etc/nginx/conf.d where nginx searches for configs to include when serving. We will create the file /etc/nginx/conf.d/your.domain.com.conf with the required content. Use this command:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
cat > /etc/nginx/conf.d/your.domain.com.conf <<EOF
server {
    listen 80;
    server_name your.domain.com;

    root /var/www/your.domain.com;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}
EOF
  • Test to make sure that there are no syntax errors in any of the nginx files
1
sudo nginx -t
  • If there are no errors, restart nginx to reload config
1
sudo systemctl restart nginx
  • To ensure that your domain can reach the server, edit your DNS config to ensure that A records of the site your.domain.com is pointing to the server IP.

Your site should now be up and running. Confirm by visiting http://your.domain.com

Important Nginx Files and Directories in Nginx

  • /var/www/html: This is the web content directory. This contains the default page content in the file index.nginx-debian.html. That page is rendered when no specifix server host is defined.

  • /etc/nginx: This contains all nginx configuration files.

  • /etc/nginx/nginx.conf: This is the main global nginx configuration file

  • /etc/nginx/sites-available/: The directory where per-site server blocks can be stored. Nginx will not use the configuration files found in this directory unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory, and then enabled by linking to the other directory.

  • /etc/nginx/sites-enabled/: The directory where enabled per-site server blocks are stored. Typically, these are created by linking to configuration files found in the sites-available directory.

  • /etc/nginx/snippets: This directory contains configuration fragments that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are good candidates for refactoring into snippets.

  • /var/log/nginx/access.log: Every request to your web server is recorded in this log file unless Nginx is configured to do otherwise.

  • /var/log/nginx/error.log: Any Nginx errors will be recorded in this log.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy