Using Virtual Hosts

You can use the Apache HTTP Server's virtual hosts capability to run different servers for different IP addresses, different host names, or different ports on the same server. If you are interested in using virtual hosts, complete information is provided in the Apache documentation on your machine or on the Web at http://httpd.apache.org/docs-2.0/vhosts/.

NoteNote
 

You cannot use name-based virtual hosts with your Red Hat Linux Advanced Server, because the SSL handshake occurs before the HTTP request which identifies the appropriate name-based virtual host. If you want to use name-based virtual hosts, they will only work with your non-secure Web server.

Virtual hosts are configured within the httpd.conf file, as described in the Section called Configuration Directives in httpd.conf. Please review that section before you start to change the virtual hosts configuration on your machine.

The Secure Web Server Virtual Host

The default configuration of your Web server runs a non-secure and a secure server. Both servers use the same IP address and host name, but they listen on different ports, and the secure server is a virtual host configured. This configuration enables you to serve both secure and non-secure documents in an manner. Setting up the secure HTTP transmission is very resource intensive, so generally you will be able to serve far fewer pages per second with a secure server. You need to consider this when you decide what information to include on the secure server and the non-secure server.

The configuration directives for your secure server are contained within virtual host tags in the /etc/httpd/conf.d/ssl.conf file. If you need to change anything about the configuration of your secure server, you will need to change the configuration directives inside the virtual host tags.

By default, both the secure and the non-secure Web servers share the same DocumentRoot. To change the DocumentRoot so that it is no longer shared by both the secure server and the non-secure server, change one of the DocumentRoot directives. The DocumentRoot either inside or outside of the virtual host tags in httpd.conf defines the DocumentRoot for the non-secure Web server. The DocumentRoot within the virtual host tags in conf.d/ssl.conf define the document root for the secure server.

The secure the Apache HTTP Server server listens on port 443, while your non-secure Web server listens on port 80. To stop the non-secure Web server from accepting connections find the line which reads:

Then comment out any line in httpd.conf which reads Listen 80.

Setting Up Virtual Hosts

To create a virtual host, you will need to alter the virtual host lines, provided as an example in httpd.conf or create your own virtual host section.

The virtual host example lines read as follows:

#<VirtualHost  *>
#    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
#</VirtualHost>

Uncomment all of the lines, and add the correct information for the virtual host.

In the first line, change * to your server's IP address. Change the ServerName to a valid DNS name to use for the virtual host.

You will also need to uncomment one of the NameVirtualHost lines below:

NameVirtualHost *

Next change the IP address to the IP address, and port if necessary, for the virtual host. When finished it will look similar to the following example:

NameVirtualHost 192.168.1.1:80 

If you set up a virtual host and want it to listen on a non-default port, you will need to set up a virtual host for that port and add a Listen directive for corresponding to that port.

Then add the port number to the first line of the virtual host configuration as in the following example:

<VirtualHost ip_address_of_your_server:12331>

This line would create a virtual host that listens on port 12331.

You must restart httpd to start a new virtual host. See the Section called Starting and Stopping httpd for instructions on how to start and stop httpd.

Comprehensive information about creating and configuring both name-based and IP address-based virtual hosts is provided on the Web at http://httpd.apache.org/docs-2.0/vhosts/.