How To Install Nginx on Ubuntu 18.04

Install Nginx

sudo apt install nginx

Setup Server Blocks

sudo mkdir -p /var/www/

In /etc/nginx/sites-available/

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

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


        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/ /etc/nginx/sites-enabled/


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'

Subscribe to My Newsletter

The latest programming-related news, articles and resources - sent to your inbox monthly. Unsubscribe anytime.