Forever Learning

Feed Rss

Name Based Virtual Hosting with Apache 2.2 (A Quickie)

07.18.2010, Tech, by .


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


  1. Apache Http Server 2.0+  sudo apt-get install apache2 apache2-mpm-worker apache2-utils apache2.2-common (note: 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:

; [102453]
$TTL 86400
@	IN	SOA (
@		NS
@		NS
@		NS
@		NS
@		NS
@			MX	10
@			A
www			A
mail			A
redmine			A
xcache			A

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:

<VirtualHost *:80>
        DocumentRoot /var/www/

<VirtualHost *:80>
        DocumentRoot /var/www/redmine/

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.

Leave a Reply

Your email address will not be published. Required fields are marked *