Nowadays, domian name is a cheap resource to make your web space cool. After you purchase a domain, most of the domain suppliers allow you to add sub-domains. Then, it is a good idea to provide sub-domains for your friends or your web sites with different styles. In Apache, you can serve multiple domains and sub-domains using one IP address. In this article, I would like to share my experience on setting up virtual host in Apache.

Scenario: We would like to set up name-based virtual hosting. We have two domains (domain1.com, domain2.com) and more subdomains. We want to serve them with one IP address. Our DocumentRoot path is docrootpath (you must replace with your real path).

Steps

  • Remove the ServerName definition in the main section (the definitions outside of the VirtualHost section). If you see a definition like Servername www.domain1.com:80 outside of the VirtualHost section, put a ‘#’ character at the head of the line to hide it. We are going to configure all domains in the VirtualHost section. The ServerName definition in the main section may cause some domains not served correctly.
  • Configure VirtualHost. Now we configure one virtual host for every domain/sub-domain that we want to access.
    NameVirtualHost 111.22.33.44:80
    #
    # If you type http://www.domain1.com, the documents under /docrootpath/domain1 will be served
    <VirtualHost 111.22.33.44:80>
    ServerName www.domain1.com
    DocumentRoot /docrootpath/domain1
    </VirtualHost>
    #
    # If you type http://sub.domain1.com, the documents under /docrootpath/domain1/sub will be served
    <VirtualHost 111.22.33.44:80>
    ServerName sub.domain1.com
    DocumentRoot /docrootpath/domain1/sub
    </VirtualHost>
    #
    # If you type http://www.domain2.com, the documents under /docrootpath/domain2 will be served
    <VirtualHost 111.22.33.44:80>
    ServerName www.domain2.com
    DocumentRoot /docrootpath/domain2
    </VirtualHost>
    #
    # If you type http://sub.domain2.com, the documents under /docrootpath/domain2/sub will be served
    <VirtualHost 111.22.33.44:80>
    ServerName sub.domain2.com
    DocumentRoot /docrootpath/domain2/sub
    </VirtualHost>
  • Restart the Apache httpd server by typing $ sudo apachectl graceful

A little more explanation: When Apache gets a request, it searches ServerName in the VirtualHost sections to match the requested domain name. If no VirtualHost matches the requested domain name, the first VirtualHost will be chosen to serve the request.

Tags: ,

This entry was posted on Monday, July 21st, 2008 at 2:27 am and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Trackback/Ping

  1. Setup Name-Based Virtual Hosting for Multiple Domains in Apache | Domains Yahoo - Domain Name Yahoo, Domain Registration Yahoo, Domain Yahoo, Yahoo Web Hosting    Jul 21 2008 / 4am:

    [...] Read more: Setup Name-Based Virtual Hosting for Multiple Domains in Apache [...]

Leave a reply

Name
Mail (will not be published)
URI
Comment