Setup Server Blocks In Nginx On Ubuntu 14.04 LTS

by
Share this Article: Facebook7Google+1Twitter18LinkedIn0Reddit0StumbleUpon1

Server Blocks (a.k.a Virtual Hosts in Apache webserver) are used to setup more than one domain or websites using a single IP address. This is very useful if anybody wants to run multiple websites using a single IP address on single VPS. VirtualHost is an Apache term. Nginx does not have Virtual hosts, it has “Server Blocks” that use the server_name and listen directives to bind to tcp sockets.

In this tutorial, let me show how to setup Server Blocks in nginx web server on Ubuntu 14.04 LTS. Be mindful that this tutorial is only tested On Ubuntu 14.04 32bit edition. I may not issue any assurance that this will work on all other Ubuntu lower versions and Ubuntu derivatives.

Scenario

For this tutorial, I will be using Ubuntu 14.04 32bit LTS, and I am going to host two testing websites called “unixmen1.local” and “unixmen2.local”. My test box IP address and hostname are 192.168.1.250/24 and server.unixmen.local respectively. Change the virtual domain names as per your requirement.

Install Nginx Webserver

Prior to install nginx server, let us update our Ubuntu server:

sudo apt-get update

Now, install nginx web server using the following command:

sudo apt-get install nginx

After installing nginx server, let us test whether the web server is working properly or not by navigating to the URL http://ip-address/.

Welcome to nginx! - Mozilla Firefox_001

As you see in the above picture, nginx webserver is working.

Setting Up Server Blocks

Nginx default document root is /usr/share/nginx/html, but we’re using /var/www/ location to setup Server Blocks.

1. Create Virtual Directories

Now, let us proceed to setup server blocks. As I mentioned earlier, I am going to host two sites called “unixmen1.local”, and “unixmen2.local”.

Create a public to place the two server blocks data’s.

First, let us create a directory for unixmen1.local site:

sudo mkdir -p /var/www/unixmen1.local/public_html

Then, create the directory for unixmen2.local site:

sudo mkdir -p /var/www/unixmen2.local/public_html

2. Setting Up Ownership and Permissions

The above directories are owned by root user now. We should change the ownership of these two directories to the regular user, instead of root user.

sudo chown -R $USER:$USER /var/www/unixmen1.local/public_html/
sudo chown -R $USER:$USER /var/www/unixmen2.local/public_html/

The “$USER” variable indicates the currently logged in user.

Set the read permissions to the above directory, so that everyone can read files from that directory.

sudo chmod -R 755 /var/www/

We have created the directories for holding the websites data’s and assigned the necessary permissions and ownership to them.

4. Create Sample pages for Server Blocks

Now, we have to the sample pages to be served through the websites. First, let us create a sample page to the unixmen1.local Server Block.

Create a index.html for unixmen1.local,

sudo vi /var/www/unixmen1.local/public_html/index.html

Add the following contents:

<html>
 <head>
 <title>www.unixmen1.local</title>
 </head>
 <body>
 <h1>Welcome To Unixmen1.local website</h1>
 </body>
</html>

Save and close the file.

Similarly, add the sample page to the second server block.

sudo vi /var/www/unixmen2.local/public_html/index.html

Add the following contents:

<html>
 <head>
 <title>www.unixmen2.local</title>
 </head>
 <body>
 <h1>Welcome To Unixmen2.local website</h1>
 </body>
</html>

Save and close the file.

5. Create Server Block Files

By default, nginx comes with a default server block file called default. We will copy the “default” file contents to our new server block files.

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/unixmen1.local
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/unixmen2.local

Now, modify the unximen1.local file to reflect with our new own values.

sudo vi /etc/nginx/sites-available/unixmen1.local

Make the relevant changes that reflect to the unixmen1 site.

[...]
server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/unixmen1.local/public_html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name unixmen1.local www.unixmen1.local;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
[...]

Like wise, modify the second server block file.

sudo vi /etc/nginx/sites-available/unixmen2.local

Make the relevant changes that reflect to the unixmen2 site.

[...]
server {
        listen 80;
        listen [::]:80;

        root /var/www/unixmen2.local/public_html;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name unixmen2.local www.unixmen2.local;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }
[...]

Please note that you have to remove the default_server and ipv6only=on options in the second server block(unixmen2.local) file.

After modifying the server blocks files, disable the default server block, and enable new server blocks as shown below.

sudo rm /etc/nginx/sites-enabled/default
sudo ln -s /etc/nginx/sites-available/unixmen1.local /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/unixmen2.local /etc/nginx/sites-enabled/

Finally, restart the nginx service.

sudo service nginx restart

That’s it. Now, we successfully configured the nginx server blocks on our Ubuntu server.

Testing Server Blocks

Edit file /etc/hosts,

sudo vi /etc/hosts

Add the virtual domain names one by one as shown below.

[...]
192.168.1.250   unixmen1.local
192.168.1.250   unixmen2.local

Save and close the file.

Open up your browser and point to the URL http://unixmen1.local or http://unixmen2.local. You should see the sample pages which we created earlier.

Unixmen1.local Test page:

www.unixmen1.local - Mozilla Firefox_004

Unixmen2.local Test page:

www.unixmen2.local - Mozilla Firefox_005

If you want to access these sites from your remote systems, you should add the actual domain name records in your DNS server. Hence, I don’t have any actual domain names and DNS server, I tested this only on my local system, and Its worked perfectly as I expected.

Cheers!

For questions please refer to our Q/A forum at : http://ask.unixmen.com/

Share this Article: Facebook7Google+1Twitter18LinkedIn0Reddit0StumbleUpon1