Install Nginx on Ubuntu 20.04 Server
Tags: nginx, devops, ubuntu, linux
Published: 2021-05-31 13:28:12

Welcome to the short guide on how to install Nginx on your server running Ubuntu 20.04.

#A bit about Nginx

So what is Nginx? As per [nginx.com](https://nginx.com): 
> Nginx is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

So in short, Nginx is a web server that has many capabilities in serving the content on server to the users.

#Prerequisites

You have to check that the server you want to install Nginx on is properly configured before proceeding so you can install and configure Nginx without any future issues. You can read about it in the article here: [Configure Ubuntu 20.04 Server](https://dev.to/semirteskeredzic/configure-new-ubuntu-20-04-server-4p4b)

#1 - Install Nginx

As always, it is a good practice to update local package index before installing new packages:
```
$ sudo apt update
```
Now you can proceed by installing Nginx
```
$ sudo apt install nginx
```

#2 - Configure the firewall

Whether you have firewall enabled or disabled you should enable nginx service in the firewall configuration. Get the list of available application configurations by running following command:
```
$ sudo ufw app list
```
You should see the output similar to this:
```
Output
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH
```
Now, you can enable `Nginx Full` which will enable traffic through port `:80` and `:443` or you can choose one of them depending whether you require encrypted on unencrypted connection.
Go with HTTP if you are initially setting up nginx but you won't be adding SSL certificates, however if you are going to be adding a certificate later on, you can proceed with `Nginx Full`.
You enable option by running this command:
```
$ sudo ufw allow 'Nginx Full'
```
#3 - Check if everything works

Run the following command for nginx service to see its status:
```
$ systemctl status nginx
```
This command will provide output where you will see whether the service is running or if it encountered an error and it needs troubleshooting.

You can check if the Nginx runs and if the firewall is allowing connections by typing your server's ip in the address bar of your internet browser. If you don't know your IP, you can find out by running this command:
```
$ curl -4 icanhazip.com
```
When you enter your server IP address in the browser you should be greeted by the Welcome to Nginx page. If you see that page, your server runs as it should.

#4 - Manage Nginx

We have mentioned `systemctl` command earlier. You can use this command on any process in Ubuntu to query or send control commands to the system manager.
```
// Start (activate)
$ sudo systemctl start nginx

// Restart (Start or restart)
$ sudo systemctl restart nginx

// Stop (deactivate)
$ sudo systemctl stop nginx

// Reload (without losing connection)
$ sudo systemctl reload nginx
```

Additionally you can use `enable` and `disable` with `systemctl` to prevent the unit from starting automatically or to enable it to start at the boot.

#5 - Server Blocks (Optional but recommended)

If you worked with Apache you are familiar with `VirtualHost`. Instead of `VirtualHost`, Nginx uses `Server Blocks` - it allows you to run more than one website on a single machine.

Ubuntu stores Nginx server blocks configuration files in `/etc/nginx/sites-available` directory which are enabled through symlinks (symbolic links) to the `/etc/nginx/sites-enabled/`

On Ubuntu 20.04 one server block is enabled by default and it serves files out of the `/var/www/html` directory. This configuration is okay if you are having a single site on one machine, however if you want to run multiple websites on a single machine you should create a different directory structure similar to `/var/www/your_domain/html`. After creating the directory, you have to assign ownership to the user, and give the owner permissions to read, write, and execute files:

```
$ sudo mkdir -p /var/www/your_domain/html
$ sudo chown -R $USER:$USER /var/www/your_domain/html
$ sudo chmod -R 755 /var/www/your_domain
```
You can now create a simple test html file inside the html directory. With `nano` command you can do it immediately:
```
$ nano /var/www/your_domain/html/index.html
```
Add the HTML code you want, you can keep it simple 
```
<html>
  <body>
    <h1>Hello from the website!
    </h1>
  </body>
</html>
```
Save it by typing `CTRL` and `X` and then typing `Y` and then `ENTER`.
Now the Nginx will serve this html file after we create a server block in `sites-available` and symlink it to `sites-enabled`

This is basic blueprint for the server block that you will create by again running `nano` command:
```
$ sudo nano /etc/nginx/sites-available/your_domain
```
When file edit is open paste this (and don't forget to substitute your_domain with your actual domain)
```
server {
        listen 80;
        listen [::]:80;

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

        server_name your_domain www.your_domain;

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

Now we are creating a symlink:
```
$ sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
```
Test to make sure there are no syntax errors in any of Nginx files:
```
$ sudo nginx -t
```
If everything checks out, restart Nginx:
```
$ sudo systemctl restart nginx
```

Congrats! You have successfully installed and configured Nginx on your Ubuntu 20.04 Server. Thank you for reading!