How To Setup a NGINX Virtual Host Using a Custom Domain

Getting Started

Thanks to: http://www.farinspace.com/nginx-virtual-host/

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:

  1. /var/www: this is where virtual host site files are stored
  2. /etc/nginx/sites-available: this is where virtual host config files are stored
  3. /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:

mkdir /var/www/example.com
mkdir /var/www/example.com/html

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:

echo "Hello World" > /var/www/example.com/html/index.php

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:

vim /etc/nginx/sites-available/example.com.conf

This file will have the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server
{
    server_name .example.com;
 
    access_log /var/log/nginx/example.com.access.log;
 
        error_log /var/log/nginx/example.com.error.log;
 
    root /var/www/example.com/html;
 
    index index.php index.html index.htm;
 
    # use fastcgi for all php files
    location ~ \.php$
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
 
    # deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }
}

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:

3
server_name .yourdomain.com;

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:

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

And the final step here is to restart Nginx:

/etc/init.d/nginx restart

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”.