Recycling Ip Addresses with Apache

ArticleCategory: [Choose a category for your article]

System Administration

AuthorImage:[Here we need a little image form you]

[Photo of the Author]

TranslationInfo:[Author and translation history]

original in en Atif Ghaffar 

AboutTheAuthor:[A small biography about the author]

I live and work in Switzerland as a webmaster/unix administrator. My passions include, Linux, unix, Perl, Apache and GPL softwares. More about me can be found on my homepage

Abstract:[Here you write a little summary]

This article demonstrates how to serve multiple virtual webservers using the same ip address with Apache on Unix/Linux. The article assumes that the reader has some knowledge of Apache and DNS and unix.
The target audience of this article is ISPs, System Administrator and anyone who cares to read and learn.

ArticleIllustration:[This is the title picture for your article]

[Illustration]

ArticleBody:[The article body]

Why should we recycle IP addresses?

Ip address recycling is useful for number of reasons.

  1. one can easily run out of ip addresses
  2. one might have only one ip address (as in my case)
  3. More system administrators time to add interfaces to host to use multiple ip addresses.

How to recycle ip adresses?

Using Apache it is simple to serve multiple domains with a single ip address.
For this article we will assume the ip address to be 192.168.1.1, and hostname hometranet.home.
Simply add a line like this to your httpd.conf file
NameVirtualHost 192.168.1.1:80
Next we will add three virtual servers using the same ip address
#Host for unix files
<VirtualHost 192.168.1.1:80>
	ServerName	unix.hometranet.home
	DocumentRoot	/www/unix/html
	ScriptAlias	/cgi-bin/ /www/unix/cgi-bin/
	TransferLog	/www/unix/logs/access_log
	ErrorLog	/www/unix/logs/error_log
</VirtualHost>

#Host for perl stuff
<VirtualHost 192.168.1.1:80>
	ServerName	perl.hometranet.home
	DocumentRoot	/www/perl/html
	ScriptAlias	/cgi-bin/ /www/perl/cgi-bin/
	TransferLog	/www/perl/logs/access_log
	ErrorLog	/www/perl/logs/error_log
</VirtualHost>


#Here we host some cool Apache stuff
<VirtualHost 192.168.1.1:80>
	ServerName	apache.hometranet.home
	DocumentRoot	/www/apache/html
	ScriptAlias	/cgi-bin/ /www/apache/cgi-bin/
	TransferLog	/www/apache/logs/access_log
	ErrorLog	/www/apache/logs/error_log
</VirtualHost>


So using one ip address we have easily managed 3 different virtualhosts.

Next add CNAMES or A Records for these names in your nameserver's
zone file and dont forget to increment the serial number :)
for eg: in my /var/named/hometranet.home.fwd, I would add
perl.hometranet.home. IN A 192.168.1.1 apache.hometranet.home. IN A 192.168.1.1 unix.hometranet.home. IN A 192.168.1.1
Please note: If you call this host by any other name than the ones given in the virtual config then the first virtual host will respond.
Before Apache 1.3.x the last one would have had responded.
For eg: If this host is named madmag then calling http://madmag.hometranet.home will give you the same result as calling unix.hometranet.home.

Using wildcards, dynamic hostnames

This is the cool part. Apache allows wildcards to be used in hostanames.
Meaning you can set a domain *.home.hometranet.home and all queries to user1.home.hometranet.home, user2.home.hometranet.home, user3.home.hometranet.home,etc will be responded.
Example:

#host for users
<VirtualHost 192.168.1.1:80>
	ServerName	home.hometranet.home
	ServerAlias     *.home.hometranet.home
        DocumentRoot	/www/home/html
	TransferLog	/www/home/logs/access_log
	ErrorLog	/www/home/logs/error_log
</VirtualHost>

and an entry in the named zone file.

*.home.hometranet.home.  IN      A       192.168.1.1

Note: This trick will not work if you are using Microsoft DNS Server. Apparently it refuses to accept wildcards in hostnames.
(But you arent using one, are you??)
I once wrote a small system that allows users on the network to add their CV in the database, and using the above trick, their CVs were availabe by http://theirUserName.cv.developer.ch.

My virtual hosts configuration file

Here is a bit more exhaustive configuration file that i have separated from my main httpd.conf file
This one is actually on my server.

To include this file in the main httpd.conf you can use the Include directive in your main httpd.conf.
Include "/etc/vhosts.conf"

Additional Resources

[Apache name-based Virtual Host Support]
http://www.apache.org/docs/vhosts/name-based.html
[Apache Virtual Host documentation]
http://www.apache.org/docs/vhosts/index.html
[An In-Depth Discussion of Virtual Host Matching]
http://www.apache.org/docs/vhosts/details.html

mirror server hosted at Truenetwork, Russian Federation.