Name Based Virtual Hosting with Apache 2.2
(A Quickie)

Posted by – 2010-07-18

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.

  • Share/Bookmark
2 Comments on Name Based Virtual Hosting with Apache 2.2
(A Quickie)

Respond

  1. Rama says:

    Suggested Topics:

    1) Databases in Cloud
    2) Hypertable, Google’s Bigtable, Apache’s HBase and Amazon’s Dynamo
    3) Web OS – Your Desktop on Cloud ( ex. http://eyeos.org/ )

    • jeff says:

      Thanks Rama.

      I went to a seminar back in 2005ish on Big Table the jist was logarithmic hierarchical breakdown of your data set over a vastly distributed network of computers. It actually wasn’t as interesting as I expected. I like the topic of map-reduce a little more. I’ll consider these other topics but I prefer to write on subjects that I have experience with. While I’m familiar with all of these topics I don’t have first hand experience to fall back on and the last thing I want to do is disseminate false information.

      I’ve considered investigating #1 so look out for that topic at some point, but I still find building, optimizing, and fine-tuning systems to be pretty fun.

      I think a writeup on CDNs like Akamai might be in order as well if traffic continues to grow.

      Thanks a lot!

      -J

Respond

Comments

Comments