Sunday, August 7, 2011

Setting up Private Virtual Hosts for Nginx on Ubuntu (11.04)

I am setting up private vitrual hosts for Nginx on my Ubuntu. I am following slicehost blog (see Resources section below).

1. Resources.
slicehost "Nginx Virtual Hosts #1"
slicehost "Nginx Virtual Hosts #2"

2. Build Directory Structure.
$ cd ~   # in my home directory
$ mkdir public_html
$ mkdir public_html/domain1.com  # first virtual host home directory
$ mkdir public_html/domain1.com/{public,private,log,backup} # and its sub-directoires
$ mkdir public_html/domain2.com  # second virtual host home directory
$ mkdir public_html/domain2.com/{public,private,log,backup} # and its sub-directories
It looks like this.






















3.Create Default Home Pages (index.html) for Each
$ gedit public_html/domain1.com/public/index.html
It's content is something like this:
<html>
  <head>
    <title>domain1.com</title>
  </head>
  <body>
    <h1>domain1.com</h1>
  </body>
</html>
Likewise,
$ gedit public_html/domain2.com/public/index.html
It's content is something like this:
<html>
  <head>
    <title>domain2.com</title>
  </head>
  <body>
    <h1>domain2.com</h1>
  </body>
</html>

4. Create vhost files.
$ sudo gedit /etc/nginx/sites-available/domain1.com
I copied and modified this from here at slicehost.
server {

            listen   80;
            server_name  www.domain1.com;
            rewrite ^/(.*) http://domain1.com/$1 permanent;

           }


server {

            listen   80;
            server_name domain1.com;

            access_log /home/socrateos/public_html/domain1.com/log/access.log;
            error_log /home/socrateos/public_html/domain1.com/log/error.log;

            location / {

                        root   /home/socrateos/public_html/domain1.com/public/;
                        index  index.html;

                        }

            }
The second one for domain2.com is similar.

5. Enable these vhosts.
By creating symlink files onto sites-enabled directory, the vhosts gets enabled.
$ sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/domain1.com
$ sudo ln -s /etc/nginx/sites-available/domain2.com /etc/nginx/sites-enabled/domain2.com

6. Finally Edit Hosts file.
My tiny intranet does not use/need DNS. I will just manually update hosts files on few machines.
I added the following entries to /etc/hosts file on this (Ubuntu) machine (192.168.12.13).
127.0.1.1 domain1.com
127.0.1.1 www.domain1.com
127.0.1.1 domain2.com
127.0.1.1 www.domain2.com
I added similar entries to C:\Windows\System32\drivers\etc\hosts on my Windows Visita.
192.168.12.13 domain1.com
192.168.12.13 www.domain1.com
192.168.12.13 domain2.com
192.168.12.13 www.domain2.com
Type in "www.domain1.com" on my browser, and the result looks like below. However, it did not display correctly at first: it kept displaying the (Nginx) default index.html until I finally deleted the symlink file default from the /etc/nginx/sites-enabled/. Also I had a little problem displaying domain2.com page from the Windows machine at frist: it ketp displaying a page from the internet where there is a real domain called "domain2.com". But after repeating refreshing and stopping and starting IE, it started to display my domain2.com which actually resides on my Ubuntu.

Note that it is important to remove any entry from the hosts files if you need to access an internet site with the same domain name.

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete