How to setup an NGINX website



Nginx has the ability to host multiple websites, all at different domains/sub-domains. This guide will show you how to point a domain at the server, and display a different website than the default NGINX page.

Make the domain point to the server

In your domains DNS records ensure that the domain/sub-domain is pointing to the servers IP address, otherwise NGINX won't get told to serve the website

Create the website directory

We need to create a directory for the website to live, and add a basic webpage to it. Websites typically live in /var/www/ but sometimes in users home directories, or elsewhere. We'll stick with the former

Change directory permissions to www-user

Nginx's default group is www-user, so to serve the website we'll grant permissions to www-user

Add user account to www-user

Make it easier...

Create an nginx site config

To get nginx to check the domain, and return the correct website, we need to configure it to do so

Create a file in

/etc/nginx/sites-available/

Containing the contents below. This will set the server to listen on port 80 (http) for any requests from the domain name set. It will then direct the root domain to the directory in root, and set the index page the file index.html, or index.htm if the former wasn't found

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

	location / {
	     root   /var/www/ ;
	     index  index.html index.htm ;
	}

Create a symlink to sites-enables

Now to get nginx to check this config we'll need to symlink it to from sites-available, to sites-enabled

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

Reload nginx

sudo systemctl reload nginx

If the above command fails, you can check your nginx config for errors with

sudo nginx -t

Then once any errors have been fixed, reload nginx again, and it should be good to go

Add a basic webpage

So we can tell the config is working, we'll add a basic webpage.

Check the website

In your browser enter the domain name we've just setup in nginx, and you'll see that the basic HTML page we've created is being displayed