Virtual Domains with a single IP Address - Mini HOWTO

Robert Prouse http://www.alteridem.net

v1.0, 5 May 1998 Copyright 1998

This HOWTO covers setting up a web server with multiple domains, but only a single IP address.  This is useful for small ISP's or Web Hosting companies where your allocated range of IP addresses is small.  The Apache documentation covers this fairly well, but getting it to work in conjunction with DNS and Sendmail is more difficult. 

Note:  This Mini-HOWTO has been superceeded by the Virtual Services Howto, please refer to that document.

Table of Contents

1.0 Setting Up your System
1.1 Configuring DNS
1.2 Setting Up Apache
1.3 Configuring Sendmail
2.0 Adding a New Domain
3.0 References

1.0 Setting Up your System

Before you begin setting up your system for single IP virtual domains, you should first have the latest versions of Apache, Sendmail and BIND (DNS) set up and running on your system.

For all of the following examples, I will use realdomain.com as the primary domain name of the server.  virtdomain1.com and virtdomain2.com will be the virtual domain names.  The IP address of the server will be 192.168.1.1.  I will assume that the domain names are registered with
Internic.  Each virtual domain should have an account on the server with a home directory, a public_html sub-directory and mail.  For the purpose of the following examples, I will assume that the users will be virt1 and virt2 for the two virtual domains.

1.1 Configuring DNS

We will assume that you already have your DNS configured and working for your primary domain.  If not see TCP/IP Network Administration by Craig Hunt, it is an excellent guide.

1. If you will be hosting a number of domains, then you will end up with numerous zone files which will quickly fill up your /etc directory. To keep everything neat and organized, create a directory /ect/named.data.

  mkdir /etc/named.data

2. Move all of your DNS configuration files into the named.data directory except for named.boot.

3. Now edit your named.boot file to reflect the changes to the location of your configuration files and to add the new domains. A simple named.boot file would look like this;

  ;
  ; named.boot - boot file for name server
  ;
  ; The directory where the configuration files are located
  directory /etc/named.data
  ; type     domain                       source host/file          backup
  file
  cache      .                            named.cache
  primary    realdomain.com               realdomain.com.zone
  primary    0.0.127.IN-ADDR.ARPA         named.local
  primary    1.168.192.IN-ADDR.ARPA       192.168.1.rev
  ;
  ; Virtual Domains start here
  ;
  primary    virtdomain1.com              virtdomain1.com.zone

Note that the directory directive indicates that all files are located in /etc/named.data.  Each virtual domain has one entry in named.boot pointing to it's zone file.

4. Create the zone file for the virtual domain as follows;

  $ORIGIN com.
  virtdomain1       IN  SOA    virtdomain1.com. root.virtdomain1.com. (
                       10018                  ; serial
                       43200                  ; refresh 
                       3600                   ; retry
                       3600000                ; expire 
                       2592000                ; minimum
                       )
                    IN  NS     ns.realdomain.com.
                    IN  MX     10  virtdomain1.com.
  ;
  ; define hosts in this zone
  ;
  virtdomain1.com.  IN  A      192.168.1.1
  $ORIGIN virtdomain1.com.
  www               IN  CNAME  virtdomain1.com.
  ftp               IN  CNAME  virtdomain1.com.

5. Restart the named deamom with the following command;

  named.restart

 

1.2 Setting Up Apache

Apache 1.1 and later support non-IP virtual hosts.  Users accessing the domain must be using a browser that is HTTP/1.1 compliant or they will be sent the primary domain's web pages instead.  This should not be a problem though as Netscape Navigator 2.0 and Internet Explorer 2.0 or
later versions both support this.

If you must support older browsers, see the file host.html in the Apache Documentation for a partial work-around or use seperate IP addresses for each virtual host.

1. Start by creating a symbolic link to your httpd directory in your root directory.  This will make it easier to work with your web server and will keep your paths short, saving typing.  A command like this will do;

  ln -s /var/lib/httpd/ /www

2. Create a new directory in your httpd directory to hold all of your virtual webs.  From here you will create links to the actual web sites. This will allow you to change the location of a domains web without restarting httpd.

  mkdir /www/virtual

3. In the configuration file for Apache, httpd.conf, add the following for each virtual domain.

  <VirtualHost 192.168.1.1>
  ServerName www.virtdomain1.com
  ServerAdmin webmaster@virtdomain1.com
  DocumentRoot /www/virtual/www.virtdomain1.com
  ErrorLog logs/www.virtdomain1.com-error_log
  TransferLog logs/www.virtdomain1.com-error_log
  </VirtualHost>

4. Restart the web server.  Find the PID of the server, then kill it;

  # ps -aux | grep httpd
  nobody    91  0.0  2.1  1112   660  ?  S  May  3   0:00 /usr/sbin/httpd 
  nobody    92  0.0  2.1  1124   660  ?  S  May  3   0:00 /usr/sbin/httpd 
  nobody    93  0.0  2.1  1124   660  ?  S  May  3   0:00 /usr/sbin/httpd 
  nobody    94  0.0  2.0  1112   652  ?  S  May  3   0:00 /usr/sbin/httpd 
  root      88  0.0  1.8  1100   580  ?  S  May  3   0:00 /usr/sbin/httpd 
  # kill -HUP 88

The process that you want to restart usually has the lowest PID and is owned by root as opposed to nobody.

5. Link the domains' web directories to the virtual directory that you created in step 2 and specified in the DocumentRoot line in step 3. For example, if the web pages for virtdomain1.com are in the public_html directory of the user virt1;

  ln -s ~virt1/public_html /www/virtual/www.virtdomain1.com

By doing this, if you decide to move the web pages for this domain to another location, you only have to change the link instead of editing httpd.conf and restarting the server.

 

1.3 Configuring Sendmail

1. Create a file called /etc/domainalias.  This file should contain aliases similar to the following;

  *@virtdomain1.com         virt1@realdomain.com
  root@virtdomain2.com      virt2@realdomain.com
  user1@virtdomain2.com	    root
  user2@virtdomain2.com	    root
  webmaster@virtdomain1.com auser@anotherhost.com

All mail for virtdomain1.com will go to user virt1 at realdomain.com.  root@virtdomain2.com will go to the user virt2 at realdomain.com.   The next two lines cause user1 and user2 to go to root@virdomain2.com which then goes to the user virt2 at realdomain.com.  By doing this, if a client wants all of the mail for their virtual domain to go to a different place, you only have to change one line.  The last line is an example of forwarding mail to a user at another domain.

2. Compile the domain aliases file into a database

  makemap btree /etc/domainalias.db  < /etc/domainalias
3.	Create a file called sendmail.cw with a list of
all virtual domains, one entry per line, like this;
  virtdomain1.com
  virtdomain2.com

4. Edit /etc/sendmail.cf.  Add a line to enable the sendmail.cw file if it is not already there;

  # file containing names of hosts for which we receive email
  Fw/etc/sendmail.cw

Add a line to bring in the domain aliases;

  Kdomainalias btree /etc/domainalias.db

Add or Change Ruleset 98;

  ###################################################################
  ###  Ruleset 98 -- local part of ruleset zero (can be null)     ###
  ###################################################################
  S98
  R$+ < $+ . >      $1 < $2 >
  R$+ < $+ >        $: < > $(domainalias $1$2 $) 
  R< > $+ @ $*      $: < $1 > $(domainalias * @ $2 $) 
  R< $+ > * $*      $: < > $1 $2
  R < $+ > $+       $: < > $2
  R< > $*           $: $>3 $1 

5. Test the sendmail configuration to verify the changes to sendmail.cf with the following;

  sendmail -v -bv webmaster@virtdomain1.com

The final destination entered in the domainalias file should be displayed.

6. Once you are satisfied that the changes are working correctly, restart sendmail.

 

2.0 Adding a new Domain

1. Register the domain with Internic
2. Add a new user account for the domain with a public_html directory
3. Add a primary domain entry to /etc/named.boot;

  primary    newdomain.com              newdomain.com.zone

4. Create a new zone file for the domain in /etc/named.data;

  $ORIGIN com.
  newdomain         IN  SOA    newdomain.com. root.newdomain.com. (
                       10018                  ; serial   
                       43200                  ; refresh
                       3600                   ; retry
                       3600000                ; expire
                       2592000                ; minimum
                       )
                    IN  NS     ns.realdomain.com.
                    IN  MX     10  newdomain.com.
  ;
  ; define hosts in this zone
  ;       
  newdomain.com.    IN  A      192.168.1.1
  $ORIGIN newdomain.com.
  www               IN  CNAME  newdomain.com.
  ftp               IN  CNAME  newdomain.com.

5.      Restart the named deamon with the following command;                     

  named.restart

6. Add a new virtual host entry to /www/conf/httpd.conf;

  <VirtualHost 192.168.1.1>
  ServerName www.newdomain.com
  ServerAdmin webmaster@newdomain.com
  DocumentRoot /www/virtual/www.newdomain1.com
  ErrorLog logs/www.newdomain1.com-error_log
  TransferLog logs/www.newdomain1.com-error_log
  </VirtualHost>

7.      Restart the web server.
8. Create a link in /www/virtual to the user's web directory;

  ln -s ~newuser/public_html /www/virtual/www.newdomain.com
9.	Add the domain to /etc/sendmail.cw.
10.	Restart sendmail.
11.	Add domain aliases to /etc/domainalias.
12.	Compile the domain aliases;	
  makemap btree /etc/domainalias.db  < /etc/domainalias

3.0 References

This HOWTO drew heavily from the work of Dan Pancamo's VirtualWeb-Mini-HOWTO and uses his sendmail ruleset 98 verbatim.  If you are setting up virtual domains with multiple IP addresses, his work is still the best source of information.

Linux VirtualWeb-Mini-HOWTO
Dan Pancamo, pancamo@infocom.net

DNS HOWTO
Nicolai Langfeldt, janl@math.uio.no

WWW HOWTO
Wayne Leister, n3mtr@qis.net 

Mail HOWTO
Guylhem Aznar, guylhem@danmark.linux.eu.org

TCP/IP Network Administration, 2nd Edition
Craig Hunt
O'Reilly & Associates Inc.

Sendmail, 2nd Edition
Bryan Costales with Eric Allman
O'Reilly & Associates Inc.

DNS and BIND, 2nd Edition
Paul Albitz and Cricket Liu
O'Reilly & Associates Inc.

The Linux Network Administrators' Guide, version 1.0
Olaf Kirch

Apache Documentation and FAQ