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

sudo apt update

Install nginx

sudo apt install nginx

# Update firewall

List available ufw apps:

sudo ufw app list

Sample output:

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS

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:

sudo ufw allow 'Nginx Full'

Verify the change

sudo ufw status

The above command will output which traffic is allowed.

# Configuring nginx

Check installed version

sudo apt list --installed | grep nginx

Check systemd service

sudo systemctl status nginx

You should see this output showing that nginx is running

● 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:

sudo systemctl enable nginx

You can use these other systemd commands to manage nginx:

# 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:

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:

curl -4



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 in our case.


  1. Create the directory to store our static content (we will use /var/www/
<pre><code class="language-bash">sudo mkdir -p /var/www/</code></pre>
  1. Ensure the created directory is accessible by the current user
<pre><code class="language-bash">sudo chown -R ${USER}:${USER} /var/www/

sudo chmod -R 755 /var/www/

  1. Add a sample file to the directory (/var/www/
<pre><code class="language-bash">cat &gt; /var/www/ &lt;&lt;EOF

<html> <head> <title>Hola from!</title> </head> <body> <h1>Hey!</h1> <p>Our site <i></i> is working as expected.</p> </body> </html> EOF

  1. 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/ with the required content.

Use this command:

cat > /etc/nginx/conf.d/ <<EOF
server {
    listen 80;

    root /var/www/;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
  1. Test to make sure that there are no syntax errors in any of the nginx files

    <pre><code class="language-bash">sudo nginx -t</code></pre>
  2. If there are no errors, restart nginx to reload config

    <pre><code class="language-bash">sudo systemctl restart nginx</code></pre>
  3. To ensure that your domain can reach the server, edit your DNS config to ensure that A records of the site is pointing to the server IP.

Your site should now be up and running. Confirm by visiting

# 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