Intro

Recently, as I was configuring Apache for my domain, I discovered that there really isn't a good document out there that shows a comprehensive configuration for virtual hosts. Either that or my google searching skills stink to high heaven, either way, before I forget how I did it, I'm writing it up for posterity.

The Config file

First off. to locate the config file.. what, you don't know where it lives? Fear not.. it's easy.. In most setups, apache takes up residence in /usr/local/apache, chances are that's where it is, and if so, chances are your configuration file is /usr/local/apache/conf/httpd.conf. No? Well, gosh darn it, let's locate it.. Depending on the flavour of linux/unix/whateverix you're using, there are different utilities for locating files.. one really simple and quick utility is "whereis", it almost always works:

$ whereis apache
apache: /usr/local/apache

Neat, huh? If that didn't find it, well, you can always use find.. or another one of the thousand ways to do this.

$ find / -type f -name httpd
./usr/local/apache/bin/httpd

Now you can use your favourite editor to edit the whole mess. I typically use vi when configuring things, but my favourite editor is actually emacs (hey, I'm using it to type this up).

This is about virtual hosts, not general apache config, so I'll get right to the point. The apache config file has three sections, they're usually listed right on top, so just go read that. We're not interested in the first section, since that deals with the general server configuration. Section two is server default configuration, anything you do not configure under your virtual host directive takes its configuration from here. Section three is what really interests us. This is where you can play and setup your virtual host.

The configuration

The virtual host config starts with, tahdah, the virtual host tag! Amazing, I know.. Anything between the opening (<VirtualHost *>) and the closing (</VirtualHost>) tags is the configuration. No deep mysteries so far. Everything else is just as simple, the problem is getting it all in one place, that's why I wrote this up.

Here are some common setup examples for virtual hosts from apache documentation. My setup is a bit more involved than that

Without further ado, here's my server config with explanations, enjoy!

# 
# unix-girl.com setup
# 
# I like to put lots of comments in my setup, it serves me well later as I get
# senile and forget what it is that I was trying to do.  
# 
# The * in the VirtualHost tag, tells apache that any request with unix-girl.com
# in the http header falls under this configuration.  This is by far the simpler way to 
# configure virtual hosts on apache, but it is not compatible with some older browsers.
# Well, too bad, I don't have multiple IPs to use, so anyone using  a browser before
# Http 1.0 will just have to live without seeing my website, sorry, but damn, 
# get rid of that 8086 and upgrade your browser already! 
#
<VirtualHost *>

# 
# This should be self-explanatory, the name and port under which the server
# identifies itself.  If the port is 80 (default) it is not necessary to include it.
# 
# If this is not set to a valid DNS name, server-side redirects will fail.
#    
     ServerName        www.unix-girl.com

#
# This address shows up on some server-generated pages, so be nice and make it a 
# real one :)
#
    ServerAdmin        webmaster@unix-girl.com

# 
# Other names this server is known by.
#
    ServerAlias        unix-girl.com
    ServerAlias        kasia.unix-girl.com

#
# Place where the documents for this domain live.
# 
    DocumentRoot       /websites/unix-girl/webroot

#
# Directory specifications, this is all explained in the configuration
# file itself, and also in the apache documentation.
#

    <Directory />
         Options            FollowSymLinks
         AllowOverride      None
    </Directory>

    <Directory "/websites/unix-girl/webroot">
         Options            Indexes FollowSymLinks
         AllowOverride      None
         Order              allow,deny
         Allow              from all
    </Directory>

#
# Index files, can be more than one, I'm boring :)
#
    DirectoryIndex     index.html

#
# Access file - this is where your users can overwrite access for their
# directory (if you allow for them to overwrite, that is)
#
    AccessFileName     .htaccess


# 
# This whole section deals with logging. 
# Specify a separate location for this vritual host's logs and you won't have to 
# sift through a 500000000000K line log file to find who's linking to your collection
# of Al Bundy quotes. 
# 

# 
# Hostname lookups while fun and convenient do slow down the server a tad. I know how to 
# use nslookup (or dig for newer linux fans).
#
    HostnameLookups    Off
    ErrorLog           /websites/unix-girl/logs/error_log

#
# Unless you want your server to crawl.. leave it at warn
#
    LogLevel           warn

#
# These are actually the default apache log formats, but if I wanted to log it 
# differently I could do so easily with this section in here..
#
    LogFormat 	       "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat 	       "%h %l %u %t \"%r\" %>s %b" common
    LogFormat 	       "%{Referer}i -> %U" referer
    LogFormat 	       "%{User-agent}i" agent

# 
# With all info in one log, it's easier to read & parse intersting information out.
#
    CustomLog          /websites/unix-girl/logs/access_log combined

#
# This adds a line containing server version and host name to server-generated pages. 
# Why not? 
#
    ServerSignature    On

#
# That's where those pesky cgi scripts can live for my unix-girl site.
#
    ScriptAlias        /cgi-bin/  "/websites/unix-girl/cgi-bin/"

    <Directory "/websites/unix-girl/cgi-bin">
         AllowOverride      None
         Options            None
         Order              allow,deny
         Allow              from all
    </Directory>
#
# .cgi files are cgi scripts, yep!
#

    AddHandler         cgi-script .cgi

#
# This tells apache that files contained under ug_pubhtml directory in users' home 
# directories should be published under the unix-girl.com domain. 
# ug_pubhtml, get it? unix-girl public_html, I'm that witty.
#

    UserDir            ug_pubhtml

#
# This is the error handling part - as in place where pages live that display
# errors, by using this directive here I can customize the look of my error 
# pages, nifty, no? 
#

     <IfModule mod_negotiation.c>
     <IfModule mod_include.c>
	 Alias /error/ "/websites/unix-girl/error/"
     
	 <Directory "/websites/unix-girl/error">
	     AllowOverride None
	     Options IncludesNoExec
	     AddHandler type-map var
	     Order allow,deny
	     Allow from all
	     LanguagePriority en es de fr
	 </Directory>
     </IfModule>
     </IfModule>


# 
# The end.
#
</VirtualHost>

Now wasn't this nice and easy? Hope this helps someone, enjoy!