How To Install Nginx on Ubuntu 18.04

Install Nginx

sudo apt install nginx

Setup Server Blocks

sudo mkdir -p /var/www/example.com

In /etc/nginx/sites-available/example.com

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

        root /var/www/example.com;
        index index.html;

        server_name example.com;

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

Create a link from the sites-available to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Systemd

Stop Nginx

sudo systemctl stop nginx

Start Nginx

sudo systemctl start nginx

Restart Nginx

sudo systemctl restart nginx

Reload Nginx

For configuration changes Nginx can be reloaded instead of restarting.

sudo systemctl reload nginx

Check Nginx Configuration

sudo nginx -t

Setup Firewall with UFW

Only for HTTP

sudo ufw allow 'Nginx HTTP'

Only for HTTPS

sudo ufw allow 'Nginx HTTPS'

For both HTTP & HTTPS

sudo ufw allow 'Nginx Full'