Configuring Apache – Intermediate
(Page 1 of 4 )
As a Web host, some key issues you will need to deal with when setting up a Web server include serving multiple domains on a single server, authorization, and authentication. In this second article on configuring Apache, Michael Swanson explains how to perform these tasks.
In the previous article in this series, we described how to set up a basic Apache Web server for simple, static HTML page hosting, and also explained how to create the virtual directory structure that Apache uses to serve files out on the Web. This article will address some of the more complex issues involved with configuring an Apache Web server. Specifically, we will address how to use virtual hosts to serve multiple domains on a single physical server, how to add modular functionality to handle specific types of files (server side pre-processing of files for things like PHP and Python), and how to use authentication and authorization schemes to control access to different parts of your website.
Often it is useful to be able to host more than one domain on a single server running a single instance of Apache. There are several possible ways to accomplish this without using the Virtual Hosts technique I will describe here. However, they involve fairly kludgey solutions, such as running multiple instances of Apache on a single machine with each listening to a different port, and then using DNS redirects to forward requests for each domain to the correct port. Apache has a much more elegant solution for this problem.
That solution is Virtual Hosts. These allow Apache to host a different site depending on several different criteria. First, if you have the luxury of multiple static IP addresses for a single machine, you can bind different Virtual Hosts to those separate IP addresses and define different “DocumentRoot” tags for requests coming in on different IP addresses. This is called IP-base Virtual Hosting.
The other option is called Name-based Virtual Hosting. This type of Virtual Hosting takes advantage of a header field set by most modern browsers that sends the name of the site the browser is requesting along with its actual request for content. This allows you to serve multiple different domain names on a single IP address. While most browsers support this type of HTTP request and do set this header field, it is important to note that if the “Name” header field is not sent along with the HTTP request coming from the browser, Name-based Virtual Hosting will not work correctly and the user will be served the default website. However, every modern browser in any sort of widespread use supports this feature, so you really only need to worry about this if for some reason supporting very old browsers is a specific need for you.
This feature is configured in the httpd.conf file by adding another sub-block to the configuration. To set up virtual hosting, you use the <VirtualHost> and </VirtualHost> tags to delineate the configuration of each virtual host. If you plan to use IP based virtual hosting, you must make sure that the “Listen” directive directs the server to listen to all of the IP addresses and ports to which you wish to serve files. To configure an IP based Virtual Host to listen to 126.96.36.199:80 you would use a Virtual Host container like the following:
This creates a virtual host that listens to 188.8.131.52 on port 80 and serves the folder /www/myserver/ as the root of the myserver.mydomain.com domain. You can easily define multiple different blocks of these configurations to handle many domains. To use name based hosting, you will need to invoke the “NameVirtualHost” directive on the IP address for which you will be using Name-based Virtual Hosts. This is done with the following code:
The above configuration will define two different hosts to be served on the 184.108.40.206 IP address. The “ServerName” directive in each “VirtualHost” block determines what files will be served based on the “Name” HTTP headers sent by the browser. This capability is very useful for small home-based Web serving environments where obtaining multiple actual WAN IP addresses would be difficult or impossible.
More Web Hosting How-Tos Articles
More By Michael Swanson