Home arrow Apache arrow Page 4 - Getting Started with Apache 2.0 Part II

One Server, One Hundred Websites - Apache

In this second article in a three-part series, you will learn how to customize the the log files generated by the Apache Web server, and much more.

  1. Getting Started with Apache 2.0 Part II
  2. The Apache Log Files
  3. Who Are You?
  4. One Server, One Hundred Websites
By: Harish Kamath
Rating: starstarstarstarstar / 16
March 21, 2005

print this article



In an earlier section, I introduced the "ServerName" directive that allows you to store the domain name or IP address of the machine hosting your Web server. Theoretically, every computer is assigned a unique IP address on the network. So, it would not wrong for you to assume that one can host only one website on a single machine - in fact, that's especially true, because IP addresses are slowly becoming a scarce commodity.

To be frank - the above conclusion is not correct, thanks to "Virtual Hosting," a feature that allows you to host more than one website on a single server. There are two possible mechanisms you can use to implement this: "IP-based" hosting (different IP addresses for the different host names) and "name-based" hosting (different host names on a single server). In this article, I'll focus on the second concept of "name-based" hosting. This feature gives you the ability to run several websites - each with its own unique URL - on a single Web server (with a single IP address).

A little caveat before I proceed - note that support for "Virtual Hosting" was introduced in version 1.1 of the HTTP protocol. Older clients that do not support this version may experience difficulties accessing such websites. But there is no reason to panic: take a look at this URL later - http://httpd.apache.org/docs-2.0/vhosts/name-based.html#compat - for a suitable work around.

Coming back to "name-based" hosting, consider the following scenario that I have often find myself in: a website project that I am working with has two different versions. The first version represents a "beta" version that I am constantly tweaking and the second is the "live" version, which contains code that has been tested and approved by the client. While it is recommended that one should host these two versions on different machines, it was not an option for my project because of infrastructure and budgetary constraints.

So, I came up with a solution that will implement two "Virtual Hosts," one for each version of the website, in order to achieve some semblance of isolation between the two. For example, I host the "beta" version at "http://beta.mysite.com" and the "live" version at "http://www.mysite.com."

Now, let me show you how this can be implemented using "name-based Virtual Hosting." Let's go back to the good old "httpd.conf" file - first, you need to specify the IP address of the machine serving requests for all "Virtual Hosts" as shown below:

#NameVirtualHost *:80

Above, I have used the "NameVirtualHost" directive to specify the local IP address for my Web server (i.e. 127.0.0.l) locally along with the default HTTP port (i.e. 80). Note that you must specify the port if you plan to configure other ports on the server differently; for example, if you have implemented SSL on port 443 and do not wish to use "Virtual Hosting" for SSL requests.

You could also opt for the "*" symbol (with the "NameVirtualHost" directive) in order to use "Virtual Hosting" for all IP addresses that your server is configured with.

Next, I have listed a sample Virtual Host included in the default version of the "httpd.conf" file:

#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common

Let me tweak with the above sample listing in order to implement the requirements of "beta" and "live" URLs that I spoke about earlier.

DocumentRoot /usr/local/apache/htdocs/beta
ServerName beta.mysite.com
ServerAdmin admin-beta@mysite.com 
ErrorLog logs/mysite-beta-error.log
CustomLog logs/mysite-beta-access.log common

DocumentRoot /usr/local/apache/htdocs/live
ServerName www.mysite.com
ServerAdmin admin-beta@mysite.com
ErrorLog logs/mysite-live-error.log
CustomLog logs/mysite-live-access.log common

For starters, I have set each "VirtualHost" block to match the IP address listed in the "NameVirtualHost" directive. Note that this is only because I do not plan to use any other IP address on this server. For obvious reasons, the IP addresses would have been different for each block if I was implementing "IP-based Virtual Hosting" or the Web server had been configured to server more than one IP address.

Next, you'll notice that I have used different Apache directives such "ServerName," "DocumentRoot," "ErrorLog," "CustomLog" and so forth within each "VirtualHost" block. While you're already familiar with the functionality of each directive, the above listing highlights the ability to customize these directives for each "virtual" website.

There is one drawback to this exercise of defining multiple virtual hosts using the "NameVirtualHost" directive: the default configuration listed under the "Main server" section is null and void. So, if you would like to display the default website when there are no matching virtual hosts for a particular visitor request, you'll need to replicate the settings of the "main server" as another "virtual host" as shown below:


# settings for the default Web site
    DocumentRoot /usr/local/apache/htdocs/
    ServerName www.site.com
    ErrorLog logs/error.log
    CustomLog logs/access.log common

# settings for the http://beta.mysite.com
DocumentRoot /usr/local/apache/htdocs/beta
ServerName beta.mysite.com
ServerAdmin admin-beta@mysite.com 
ErrorLog logs/mysite-beta-error.log
CustomLog logs/mysite-beta-access.log common

# settings for the http://www.mysite.com

// snip

That was quick overview on how to configure "Virtual Hosts" on your Web server and I'll admit that I have only touched the tip of the iceberg above. So, if you have more complex requirements, the following URL should help: http://httpd.apache.org/docs-2.0/vhosts/.

End Game

That's about it for this part of the Apache series. Today, I started with a quick overview of the default server configuration "directives" that serves as a precursor to the section on "Virtual Hosting." Next, I spoke about the different log files generated by the Apache Web server and showed you how to customize these log files for your requirements. Finally, I explained how you could implement "name-based Virtual Hosting," an interesting feature that gives you the ability to host several websites (each with an unique URL) on a single Web server.

In the next part of this series, I shall show you how to configure the Apache server as proxy, talk a little about URL re-writing (a powerful feature that allows you to "re-write" requests to the Web server), configure user-specific directories on your Web server and much more.

Till then, ciao and take care!

Note: All examples in this article have been tested on Linux/i586 with Apache 2.0.52, MySQL 3.23 and PHP 5.0.3. Examples are illustrative only, and are definitely NOT meant for a production environment.

>>> More Apache Articles          >>> More By Harish Kamath

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Apache Unveils Cassandra 1.2
- Apache on ARM Chips? Dell and Calxeda Help M...
- The Down Side of Open Source Software
- VMware Unveils Serengeti for Apache Hadoop
- SAP Takes Steps to Improve Hadoop Integration
- Looking to Hone Apache Hadoop Skills?
- How to Install Joomla on WAMPP
- Working with XAMPP and Wordpress
- GUI Available for Apache Camel
- Reduce Server Load for Apache and PHP Websit...
- Creating a VAMP (Vista, Apache, MySQL, PHP) ...
- Putting Apache in Jail
- Containing Intrusions in Apache
- Server Limits for Apache Security
- Setting Permissions in Apache

Developer Shed Affiliates


Dev Shed Tutorial Topics: