This guide assumes that you’ve been following along from the previous tutorial: How to Install and Configure a NGINX Server (LEMP Stack)
Important: to properly setup your virtual host you will need to be able to create a DNS A RECORD for a domain or subdomain you own. I use GoDaddy to register my domain names and they offer free DNS service. Check with your domain registrar as a similar service may be available to you.
First lets familiarize ourselves with the following three directories:
/var/www: this is where virtual host site files are stored
/etc/nginx/sites-available: this is where virtual host config files are stored
/etc/nginx/sites-enabled: this is where symbolic links (or file shortcuts) to the virtual host config files are placed
Setting Up The Site Files
First up is the
/var/www directory where your site files will be kept. Lets start by creating a new directory for the new virtual host, as well as its web root directory:
I typically setup the
html directory as the web root, to be able to put non web accessible files outside of the root directory.
Lets create a simply
index.php file which we will use to confirm that our new virtual host is working:
Setting Up The Nginx Virtual Host Config File
Now we must setup the Nginx config file for our virtual host. We will be creating individual config files for each virtual host in the
/etc/nginx/sites-available directory. Create the following file:
This file will have the following content:
Important: I’ve created a new DNS A RECORD (using GoDaddy’s free Total DNS service) and pointed it to our new server’s IP address. I then change the following line, in the virtual host config file (see above), to reflect the domain name I am using:
If you’ve been following along so far, basically all your virtual host configs will be defined in the
/etc/nginx/sites-available directory, to enable a config, you would create a symbolic link (or file shortcut) in the
/etc/nginx/sites-enabled directory, equally to disable a config, you would remove the symbolic link. Nginx will do the rest and search the
/etc/nginx/sites-enabled directory for active virtual host configs.
Now we will use the
/etc/nginx/sites-enabled directory and create a symbolic link to the virtual host config file:
And the final step here is to restart Nginx:
You should now be able to open up a browser and go to
http://yourdomain.com/ where you should see a simple message reading “Hello World”.