In this post we will see how we can install the Nginx web server in our Ubuntu server, and how to configure it to serve some files.

If you have already a web server installed, like Apache, you either have to uninstall it or change the default port that is listening because it will conflict with Nginx.

Install Nginx

We install nginx and any required dependencies:

sudo apt install nginx

Adjusting the Firewall

After you have followed the initial server setup and enabled the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can check that UFW has an application profile for Nginx like so:

sudo ufw app list

You should get a listing of the application profiles:

Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS

The three profiles do the following:

  • Nginx HTTP: Opens port 80
  • Nginx HTTPS: Opens port 443
  • Nginx Full: Opens both port 80 and port 443

We will not set up any SSL certificate, so we will not use port 443, so we can open only port 80:

sudo ufw allow 'Nginx HTTP'

else to open both ports:

sudo ufw allow 'Nginx Full'

Verify the change:

sudo ufw status

Check Web Server status

We can check the status of Nginx, by running:

sudo systemctl status nginx

If everything is ok, we should see a green active (running) status.

Also, we can check the local network sockets to see the port that Nginx is using:

sudo netstat -tlpn | grep nginx


tcp        0      0    *               LISTEN      15004/nginx: master
tcp6       0      0 :::80                   :::*                    LISTEN      15004/nginx: master

We can also visit http://server-ip to check the default page that Nginx serves. The page is located at /var/www/html/index.nginx-debian.html.

Setting Up Server Blocks

We will create a Server Block (similar logic to Apache’s Virtual Hosts) that can be used to configure a web server on our server with a domain name. The Server Blocks allows us to have more than one domains on our single server.

For this example, we will use the domain. You should replace it everywhere we use it with your own registered domain. You can follow the Duck DNS guide to see how to get a free * domain.

Nginx has a default html server block enabled that is located under /var/www/html. We will create a new directory under /var/www with the domain name we have as the directory name. This way, if we want to add more domain names in the future it will be as simple as creating a new directory there. We will leave the default /var/www/html directory to be served if the client request does not match any other sites.

Files to be served

Firstly, create the directory:

sudo mkdir /var/www/

Next, it is best to assign the ownership of the directory to a user and not leave the ownership to root as it can create some security problems. In order to assign the ownership to your current user you can run:

sudo chown -R $USER:$USER /var/www/

In order to make sure that the permissions are correct run:

sudo chmod -R 755 /var/www/

Finally, we can create a basic index.html page to display at the website:

vim /var/www/

and add:

<!DOCTYPE html>
<html lang="en">
    <title>Hello Friend</title>

    <h1>Hello Friend</h1>
    <p>This is a website</p>

Nginx Configuration File

In order for Nginx to server the files we have created at /var/www/ we need to create a new configuration file under /etc/nginx/sites-available (instead of editing the default one at /etc/nginx/sites-available/default):

sudo vim /etc/nginx/sites-available/

And add the following configuration block, which is similar to the default one but configured for our domain name:

server {
        listen 80;
        listen [::]:80;

        root /var/www/;
        index index.html index.htm index.nginx-debian.html;

        location / {
                try_files $uri $uri/ =404;

The directives we used are explained:

  • listen: Tells Nginx to listen for connections for both IPv4 and IPv6.
  • server_name: The domain of the website that we are looking for.
  • root: The directory where the website files are located.
  • index: The default file in the website files that Nginx will serve first.
  • location: Tells Nginx how to look up files, and if not found throw a 404 error.

After saving we can make sure we don’t have syntax errors:

sudo nginx -t

Now we need to create a soft link to the /etc/nginx/sites-enabled directory in order for Nginx to read it during startup:

ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled

And finally we can reload Nginx to update the changes we made:

sudo systemctl reload nginx

Now if you type in your browser you should the contents in the index.html we wrote.

Nginx Service Commands

Some useful systemd commands for managing the Nginx service.

To stop the web server:

sudo systemctl stop nginx

To start the web server:

sudo systemctl start nginx

To stop and start again the web server:

sudo systemctl restart nginx

To reload simple configuration changes without stopping the web server:

sudo systemctl reload nginx

To disable the web server from starting automatically at boot time:

sudo systemctl disable nginx

To enable the web server for starting automatically at boot time:

sudo systemctl enable nginx

Nginx Files and Directories

Some useful locations for Nginx in order to find quickly what you need:


  • /var/www/html: The web content that will be served by default, by Nginx.


  • /etc/nginx: The Nginx configuration directory.
  • /etc/nginx/nginx.conf: The main Nginx configuration file for global settings.
  • /etc/nginx/sites-available: The directory where server blocks are stored per-site. These configuration files will not be used by Nginx if they are not linked to sites-enabled.
  • /etc/nginx/sites-enabled: The directory where enabled server blocks are stored per-site. Usually, these configuration files are created by linking the ones from sites-available.


  • /var/log/nginx/access.log: Every request to the Nginx web server is logged in this file.
  • /var/log/nginx/error.log: Every Nginx error is logged in this file.