Recipe 22.10. Hosting Multiple Domains with Apache

 < Day Day Up > 

22.10.1 Problem

You want to host several different domains on a single Apache server, sharing a single IP address. You've already registered all your domain names and have DNS in place for each one.

22.10.2 Solution

Use Apache's VirtualHost directives to set up name-based virtual host support. Here is a sample httpd.conf entry for serving two different domains:

NameVirtualHost *:80    <VirtualHost *:80>  ServerName  www.tuxcomputing.com  ServerAlias  tuxcomputing.com  *.tuxcomputing.com  DocumentRoot /var/www/tuxcomputing  ServerAdmin  admin@tuxcomputing.com  </VirtualHost>    <VirtualHost *:80>  ServerName  www.bratgrrl.com  ServerAlias  bratgrrl.com  *.bratgrrl.com  DocumentRoot  /var/www/bratgrrl  ServerAdmin  admin@bratgrrl.com  </VirtualHost>

Each domain has its own separate root directory where the site files are stored. This allows you to easily set up subdomains, such as webmail.bratgrrl.com and wacko.games.tuxcomputing.com. However, this does not work by magic you need to create DNS A records for each domain and subdomain.

Once you start using virtual hosts, each of your domains must have a VirtualHost directive. If you start out with a single domain, you'll have to create a VirtuaHost entry for it. VirtualHost directives override the global directives in httpd.conf. Almost any httpd.conf directive can be used in your VirtualHost stanzas, so you can customize each virtual host as you need.


22.10.3 Discussion

Name-based virtual hosting is the easiest way to serve up multiple domains from a single Apache server. Configuring A records for a lot of subdomains can get a bit wearisome, but it's better than using a domain wildcard. A domain wildcard allows all traffic that includes your domain name to hit your servers. For example:

randomstuff.bratgrrl.com
really.weird.randomstuff.bratgrrl.com

Spammers will abuse a domain wildcard beyond belief, so you want to be careful to configure only your exact domain names in your DNS records. It is acceptable to use domain wildcards in your VirtualHost directives, because only domain names explicitly defined in DNS will ever see any traffic.

Here is what each directive does:


NameVirtualHost *:80

This tells Apache to listen for requests for these virtual hosts on all network interfaces, on port 80. It is best to specify an IP address or, as in this case, a wildcard. Don't use domain names, because the server will then have to do DNS lookups, which will slow it down. Never leave it blank. Any IP/port setting here must also match the Listen directive. For example:

Listen 80 Listen 192.168.1.5:8080

NameVirtualHost can use either of these. Remember that when you use a nonstandard port, such as 8080, users must specify the port in their URLs:

http://www.games.tuxcomputing.com:8080

<VirtualHost *:80>

This must match the NameVirtualHost values.


ServerName www.tuxcomputing.com

This should match a DNS A record.


ServerAlias tuxcomputing.com *.tuxcomputing.com

Here you can define other server names; users can now connect to www.tuxcomputing.com, or tuxcomputing.com, or .tuxcomputing.com"><anysubdomain>.tuxcomputing.com. Note that every subdomain must have a specific DNS A record pointing to it don't use DNS wildcards! This is asking for trouble with spammers and other loathsome subhumans who infest the Internet, looking for things like this to exploit.


DocumentRoot /var/www/tuxcomputing

This specifies the local directory where the site files are stored.


ServerAdmin admin@tuxcomputing.com

This provides a contact address to which users can report problems.

22.10.4 See Also

  • http://localhost/manual/vhosts/name-based.html

  • Chapter 4 of Apache: The Definitive Guide

     < Day Day Up > 


    Linux Cookbook
    Linux Cookbook
    ISBN: 0596006403
    EAN: 2147483647
    Year: 2004
    Pages: 434

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net