by April 13, 2002 0 comments



To set up virtual hosting, first set up a machine running PCQLinux 7.1. Suppose the IP address of the machine is 192.168.1.1 and the hostname is
www.pcquest.com. When you install the Apache Web server on the machine, by default it will serve the index.html file in the /var/www/html directory when you key in http://192.168.1.1 or https://www.pcquest.com in your Web browser. Of course, if you want to use a host name rather than an IP address, you’ll have to either use DNS or the hosts file in /etc directory. For simplicity, we used the hosts file. An entry in this file is in the form:

For example,

192.168.1.1 www.pcquest.com

All versions of Windows also have a hosts file. On Win NT or 2K you will find the hosts file in winnt\system32\drivers\etc and on Win 98 you will find it in the windows directory. 

Coming back to Apache, in our example, it runs the pcquest.com website by default. Suppose we want to run two more websites (say,
computersathome.com  and dataquest.com) on the same machine. That is, each site would have its own directory and an index.html file. For this, we set up additional sites on Apache with document roots /var/www/compathome and /var/www/ dataquest. Such sites, which are different from the default one, are called virtual hosts. 

There are two approaches to creating virtual hosts: IP based or name based. In IP-based virtual hosting, each site runs on a different IP address while in name-based virtual hosting, each site is run on the same IP address. Let’s look at IP-based virtual hosting first.

IP-based virtual hosting
For IP based, you’ll need either multiple network cards on a Linux machine or multiple IP addresses assigned to the same network card (known as IP aliasing). (For more, see box, Multiple IP Addresses.) Suppose the Linux machine has multiple network cards having IP addresses 192.168.1.1, 192.168. 2.1 and 172.16.0.1. The pcquest .com site is already set at 192.168. 1.1, and we want to run computersathome.com at 192.168.2.1 and dataquest.com at 172.16.0.1.

To create virtual hosts, open httpd.conf file from /etc/httpd/conf directory and scroll to Section 3: Virtual Hosts, and type the following lines
below it.


DocumentRoot /var/www/compathome
ServerName www.computersathome.com


DocumentRoot /var/www/dataquest
ServerName www.dataquest.com

Here we assign a different DocumentRoot for each virtual host. With ServerName we assign the site name to the virtual hosts. Save the file and create the directories compathome and dataquest in /var/www. Create a file named index.html in each directory with some text contents, say “Welcome to Computers@ Home” and “Welcome to Dataquest” respectively. Restart the Apache Web server as

/etc/rc.d/init.d/httpd restart

Append the following in the hosts file on the machine from which you want to access the sites.

192.168.2.1 www.computersathome.com
172.16.0.1 www.dataquest.com 

When you key in the URLs http://www.computersathome.com
and http://www.dataquest.com, you will be able to see the welcome pages of the respective sites served by the same Apache Web server, but from different locations, /var/www/compathome and /var/www/dataquest respectively. 

Name-based virtual hosting
For this you must select an IP address other than the default 192.168.1.1, which was being used for the pcquest.com website. Otherwise, the virtual hosts setup will override the default site and the site won’t show up. Suppose you select 192.168.2.1, then this single IP address can host both Computers@Home and Dataquest sites. Open httpd.conf file in /etc/httpd/ conf directory and scroll to Section 3: Virtual Hosts. Type the following lines below it.

NameVirtualHost 192.168.2.1

DocumentRoot /var/www/compathome
ServerName www.computersathome.com


DocumentRoot /var/www/dataquest
ServerName www.dataquest.com

With NameVirtualHost we tell Apache that the IP address 192.168.2.1 will receive requests for virtual hosts based on names. Note that in the tag we are specifying the same IP address as with NameVirtualHost. Create the directories and index.html files as described for IP-based virtual hosting and restart Apache. 

Now on the machine, from which you want to access the sites, append the following in the hosts file.

192.168.2.1 www.computersathome.com 
192.168.2.1 www.dataquest.com 

This time when you key in the URLs, the request goes to the same IP address but the names specified in the URL (www.computersathome.com 
and www.dataquest. com) direct Apache to compare it with the name specified with ServerName for each virtual host. It then serves the index.html file from the appropriate directory. 

Shekhar Govindarajan

Multiple IP Address

If you have a single network card in Linux, it will be called eth0. Suppose it has the IP address 192.168.1.1 and you want to assign two more IP addresses to it (192.168.2.1 and 172.16.0.1). To assign 192.168.2.1, create a file named ifcfg-eth0:0 in /etc/sysconfig/network-scripts directory and type the following.

DEVICE=eth0:0
USERCTL=no
ONBOOT=yes
BOOTPROTO=none
BROADCAST=192.168.2.255
NETWORK=192.168.2.0
NETMASK=255.255.255.0
IPADDR=192.168.2.1

For 172.16.0.1, create another file named ifcfg-eth0:1 in the same directory and type the following.

DEVICE=eth0:1
USERCTL=no
ONBOOT=yes
BOOTPROTO=none
BROADCAST=172.16.0.255
NETWORK=172.16.0.0
NETMASK=255.255.255.0
IPADDR=172.16.0.1

Restart the network interfaces as

/etc/rc.d/init.d/network restart

Now, when you issue ifconfig you should be able to see eth0:0 and eth0:1 along with eth0.

If you prefer a graphical utility for the above procedure, launch X Window and from within a terminal window, issue netcfg. Click on the Interfaces tab. With eth0 selected, click on Alias. In the window that pops up, fill in the IP address and the netmask. Remember to select the option Activate interface at boot time.

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

<