Tuesday, April 7, 2009

Running Multiple Websites or Domains with Different Domain Names or Host Names with Apache Web Server

If you have read the post on adding hosts names for development on IIS or Apache you may have already started wondering how can you serve or show different content using IIS or Apache.
apache_header 
Here we will show you just how easy it is to configure IIS or Apache web server to serve different sites when different hostnames are used.
You may be running any application server, like ColdFusion, PHP, Ruby on Rails, ASP.net, Tomcat, you will find the following information handy.

This article will help you configure your Apache web server to run and handle multiple domain names.

How to configure Apache Web Server to run multiple websites or domains or sub-domains:
  1. Open httpd.conf file. This file is located inside the installation directory under /conf folder. Usually it is placed under: C:\Program Files\Apache Software Foundation\Apache2.2\conf
  2. Scroll to the bottom of the text file and add the following text:
    NameVirtualHost *:80
  3. For each new website we wish to add we add a vhost. You can copy and add the following lines:

    <VirtualHost *:80>
        ServerName sub.domain.com
        DocumentRoot "C:/Apache2.2/htdocs/test/"
    </VirtualHost>

  4. Restart Apache and load sub.domain.com using the browser, if you’re lucky you will be able to access the pages inside the DocumentRoot specified in the previous step. You can also get an Access Denied or Directory listing denied error which will be fixed in the next step.
  5. Add this (only when you’re getting errors mentioned in Step 4) to make sure users can access the pages:
    <VirtualHost *:80>
        ServerName test.codecurry.com
        DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/"
    <Directory "C:/Apache2.2/htdocs/test">
        Options FollowSymLinks
        AllowOverride all
        Order deny,allow
        Deny from all
    </Directory>
    </VirtualHost>
  6. You can do much more than this. You can specify other names for this new domain:
    <VirtualHost *:80>
        ServerName test.codecurry.com
        ServerAlias www.sub.domain.com
        DocumentRoot "C:/Apache2.2/htdocs/test/"
    </VirtualHost>
  7. No just that, you can add more than one name separating each with a single space:
    <VirtualHost *:80>
        ServerName test.codecurry.com
        ServerAlias www.sub.domain.com sub2.domain.com sub.another-domain.com
        DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/test/"
    </VirtualHost>
  8. Now the final stroke adding one more site pointing to a separate domain:
    <VirtualHost *:80>
        ServerName sub.domain.com
        ServerAlias www.sub.domain.com
        DocumentRoot "C:/Apache2.2/htdocs/sub/"
    </VirtualHost>
    <VirtualHost *:80>
        ServerName localhost
        ServerAlias www.localhost.com localhost.com local local.com
        DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/"
    </VirtualHost>
    <VirtualHost *:80>
        ServerName dev.domain.com
        ServerAlias http://www.dev.domain.com/
        DocumentRoot "D:/wwwroot/Inetpub/dev/"
    </VirtualHost>

 

Explanations:

NameVirtualHost *:80 Tells Apache Web Server that we want to listen to all requests made to any IP address bind to Apache. A simple IP-based virtual host will use the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host. With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.
<VirtualHost *:80>
A new web site or virtual host defined.
ServerName sub.domain.comThe hostname for this virtual host.
ServerAlias www.sub.domain.comAnother hostname for this virtual host. More can be added by separating hostnames with a spaces.

 

Other cool examples:

Listen 8080

<VirtualHost 111.22.33.44:8080>
DocumentRoot /www/domain2
</VirtualHost>
  1. Makes Apache listen to port 8080 as well.
  2. Create a virtual host with specific IP address and specific port as well.
<VirtualHost 111.22.33.44:*>
DocumentRoot /www/domain2
</VirtualHost>
Listens to all ports on the specified IP address.
<VirtualHost *:8080>
DocumentRoot /www/domain2
</VirtualHost>
Listens to specific port number on all IP addresses of the server.
<VirtualHost _default_:*>
DocumentRoot /www/default
</VirtualHost>
Catching every request to any unspecified IP address and port, i.e., an address/port combination that is not used for any other virtual host.
<VirtualHost _default_:80>
DocumentRoot /www/default80 
</VirtualHost>
<VirtualHost _default_:*>
DocumentRoot /www/default 
</VirtualHost>
  1. Same as the previous one,
  2. But the server listens on several ports and
  3. We want to use a second _default_ vhost for port 80
Note: Some old browsers lacks the ability to send Hostname header in the requests and so websites that depend solely on ServerName or ServerAlias won’t work. A work around is to point such browsers to a directory using rewrite rules. Doing this will make your website available to all kinds of browsers:
<VirtualHost 111.22.33.44>
# primary vhost
DocumentRoot /www/subdomain
RewriteEngine On
RewriteRule ^/.* /www/subdomain/index.html 
</VirtualHost>

<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain/sub1
ServerName www.sub1.domain.tld
ServerPath /sub1/
RewriteEngine On
RewriteRule ^(/sub1/.*) /www/subdomain$1 
</VirtualHost>

<VirtualHost 111.22.33.44>
DocumentRoot /www/subdomain/sub2
ServerName
www.sub2.domain.tld
ServerPath /sub2/
RewriteEngine On
RewriteRule ^(/sub2/.*) /www/subdomain$1 
</VirtualHost>

Good Luck.

☛ Quick Links

♥ Recommended For You