Setting up Nginx

TL;DR

sudo apt-get update
sudo apt-get install nginx
nginx -v

sudo systemctl status nginx
sudo systemctl start nginx
sudo systemctl enable nginx

# If you have ufw enabled already:
# sudo ufw allow 'Nginx Full'
# sufo ufw reload
Expected output (1/2)
Expected output (2/2)

Other useful commands

sudo systemctl stop nginx
sudo systemctl reload nginx
sudo systemctl restart nginx

Test nginx running

From withing the server (while connected remotely to it using ssh)

sudo apt-get install curl
curl –i 127.0.0.1

You should see the landing page nginx is loading in your console

From outside the server (from your working machine)

Just point your browser to http://<your server ip> and you should see the landing page loaded

Troubleshooting

  • If the page isn't loading externally but it's loading internally, then we might have forgotten allowing Nginx through the firewall
  • If the page isn't loading internally as well, check the running status of nginx sudo systemctl status nginx. If it's not running, start it by using sudo systemctl start nginx .

Important Nginx File Locations

By default, Nginx stores different configuration and log files in the following locations:

  • /var/www/html – Website content for the default nginx landing page (the one you saw above when you used curl).
  • /etc/nginx – Location of the main Nginx application files.
  • /etc/nginx/nginx.conf – The main Nginx configuration file.
  • /etc/nginx/sites-available – List of all websites configured through Nginx.
  • /etc/nginx/sites-enabled – List of websites actively being served by Nginx.
  • /var/log/nginx/access.log – Access logs for every request to your server.
  • /var/log/ngins/error.log – A log of any errors generated in Nginx.