How To Set Up nginx Virtual Hosts (Server Blocks) on Ubuntu 12.04 LTS

I decided to switch to NGINX based on the fact that every statistic shows it’s better than Apache.

Some articles on this can be found here or here.

So I found this guide from digitalocean.

About Virtual Hosts

Virtual Hosts are used to run more than one website or domain off of a single server. Note: according to the nginx website, virtual hosts are called Server Blocks on the nginx. However, for easy comparison with apache, I’ll refer to them as virtual hosts in this tutorial.

Set Up

The steps in this tutorial require the user to have root privileges on the virtual private server. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4. Furthermore, if I reference the user in a step, I’ll use the name www. You can implement whatever username suits you.

Additionally, you need to have nginx already installed on your VPS. If this is not the case, you can download it with this command:

 

Step One— Create a New Directory

The first step in creating a virtual host is to a create a directory where we will keep the new website’s information.

This location will be your Document Root in the nginx virtual configuration file later on. By adding a -p to the line of code, the command automatically generates all the parents for the new directory.

You will need to designate an actual DNS approved domain, or an IP address, to test that a virtual host is working. In this tutorial we will use example.com as a placeholder for a correct domain name.

However, should you want to use an unapproved domain name to test the process you will find information on how to make it work on your local computer in Step Six.

 

 

Step Two—Grant Permissions

We need to grant ownership of the directory to the right user, instead of just keeping it on the root system. You can replace the “www-data” below with the appropriate username.

sudo chown -R www-data:www-data /var/www/example.com/public_html
Additionally, it is important to make sure that everyone is able to read our new files.

Now you are all done with permissions.

 

Step Three— Create the Page

We need to create a new file called index.html within the directory we made earlier.

We can add some text to the file so we will have something to look at when the the site redirects to the virtual host.

Save and Exit

 

Step Four—Create the New Virtual Host File

The next step is to create a new file that will contain all of our virtual host information.

nginx provides us with a layout for this file in the sites-available directory (/etc/nginx/sites-available), and we simply need to copy the text into a new custom file:

 

Step Five—Set Up the Virtual Hosts

Open up the new virtual host file— you will see all the information you need to set up virtual host within.

We need to make a couple of changes in these few lines:

 

Uncomment “listen 80” so that all traffic coming in through that port will be directed toward the site
Change the root extension to match the directory that we made in Step One. If the document root is incorrect or absent you will not be able to set up the virtual host.
Change the server name to your DNS approved domain name or, if you don’t have one, you can use your IP address
You do not need to make any other changes to this file. Save and Exit.

The last step is to activate the host by creating a symbolic link between the sites-available directory and the sites-enabled directory. In apache, the command to accomplish this is “a2ensite”—nginx does not have an equivalent shortcut, but it’s an easy command nonetheless.

To both avoid the “conflicting server name error” and ensure that going to your site displays the correct information, you can delete the default nginx server block:

 

Step Six—Restart nginx

We’ve made a lot of the changes to the configuration. Restart nginx and make the changes visible.