Forever Learning

Feed Rss

Name Based Virtual Hosting with Apache 2.2 (A Quickie)

07.18.2010, Tech, by .

Abstract

This quickie tells how to run multiple websites on one server with 1 ip address using Apache Http Server version 2.x.

Prerequisites

  1. Apache Http Server 2.0+  sudo apt-get install apache2 apache2-mpm-worker apache2-utils apache2.2-common (note: php.net recommends mpm-prefork for mod_php enabled servers)
  2. A domain (or more specifically 2, but we’ll quickly hit setting up subdomains)
  3. And two server roots with index files

DNS Setup

First you have to register your domain with your hosting provider’s name servers.  I use Linode and they have a pretty straightforward web utility for setting up your zone files.  For the purposes of this quickie you’re looking to add hostname entries to make your zone files look something like this:

; jeffreyjason.com [102453]
$TTL 86400
@	IN	SOA	ns1.linode.com. umassthrower.gmail.com. (
					2010071855
					14400
					14400
					1209600
					86400
				)
@		NS	ns1.linode.com.
@		NS	ns2.linode.com.
@		NS	ns3.linode.com.
@		NS	ns4.linode.com.
@		NS	ns5.linode.com.
@			MX	10	mail.jeffreyjason.com.
@			A	97.107.129.122
www			A	97.107.129.122
mail			A	97.107.129.122
redmine			A	97.107.129.122
xcache			A	97.107.129.122

httpd.conf setup

Inside your apache configuration file you need to add the following line to turn enable name based virtual hosting

NameVirtualHost *:80

Next you setup the virtual host configuration for your sites.  Here is a minimalist example of 2 of mine with the cruff stripped out:

www.jeffreyjason.com

<VirtualHost *:80>
        ServerName jeffreyjason.com
        DocumentRoot /var/www/jeffreyjason.com/
</VirtualHost>

redmine.jeffreyjason.com

<VirtualHost *:80>
        ServerName redmine.jeffreyjason.com
        DocumentRoot /var/www/redmine/
</VirtualHost>

Finally you restart apache and, assuming your subdomain entries have propagated, you should now be serving 2 sites from one server. The process is the same with 2 separate domains, you will just have 2 separate zone files pointing to the same IP. Before we go don’t forget to add ServerAlias directives to your virtual host configurations in order to have your visitors not end up with an error message when they inevitable try to access www.subdomain.domain.tld.