Virtual Hosting


Apache lets you host several domains with a single instance of the server, a process referred to as virtual hosting. This service can be a godsend for the system administrator overseeing an installation with limited resources, such as hardware, static IP addresses, or even time to monitor several server setups. Virtual hosting is implemented through several directives included in httpd.conf.

Note

The mod_vhost_alias module is another virtual hosting tool included with the core Apache package. It is designed for use by ISPs and other large sites.


There are two ways to implement virtual hosts in Apache: name-based and IP-address based hosting. This section covers both options.

In either case, you must first tell Apache the IP addresses that have multiple domains associated with them. You can either use a specific IP address or the * variable to indicate that Apache should consider any request it receives as a candidate for virtual hosting. Open httpd.conf and type

NameVirtualHost * 

Name-Based Virtual Hosting

According to the HTTP/1.1 specification, when a client seeks out a document from a server, it includes the expected domain in its request. Although various domains may resolve to the same IP address with a DNS query, the domain names in the HTTP requests will still be different. This is the basic principle of name-based virtual hosting.

This type of virtual hosting (also known as vhosting) is the easiest and most popular way to handle hosting multiple domains. With this method, you need only one IP address to host several domain names. Apache receives a request, examines it to identify the desired domain, and obliges with the proper page.

You'll need to add VirtualHost sections in the configuration file for each domain name after the NameVirtualHost directive. For example, if you want to host a personal site at www.YourName.com and a second site, www.susefan.com, this is how you would enter them in httpd.conf:

<VirtualHost *>     ServerName www.YourName.com     DocumentRoot /srv/www/htdocs/YourName.com     ServerAdmin webmaster@YourName.com     ErrorLog /var/log/apache2/www.YourName.com-error_log     CustomLog /var/log/apache2/www.YourName.com-access_log common </VirtualHost> <VirtualHost *>     ServerName www.susefan.com     DocumentRoot /srv/www/htdocs/susefan.com     ServerAdmin webmaster@susefan.com     ScriptAlias /srv/www/htdocs/susefan.com/cgi-bin     ErrorLog /var/log/apache2/www.susefan.com-error_log     CustomLog /var/log/apache2/www.susefan.com-access_log common </VirtualHost> 

Caution

Although SSL/TSL have not been discussed, it is still worth mentioning that Apache cannot distinguish different security certificates for name-based virtual hosts. This is because SSL/TSL operates at a socket level, and the request has already been encrypted by the time Apache receives it. This may cause warnings to be triggered by the client's browser, depending on the common name listed by the certificate. In these cases, IP-Address Based virtual hosting is recommended.


As the server receives the request, it will scan through the configuration file seeking a virtual host entry's ServerName or ServerAlias directive that matches the expected domain. If no matching entry can be found, the first virtual host is used. The virtual host first inherits the directives set earlier in the httpd.conf file and then overrides those explicitly set for the host.

In the preceding example, www.YourName.com will use whatever ScriptAlias value is defined in the base configuration, whereas www.susefan.com will use /srv/www/htdocs/susefan.com/cgi-bin.

IP-Address Based Virtual Hosting

Hosting multiple IP addresses is a little more complex than hosting multiple names, but it is more widely supported. All but a very few ancient browsers (that is, those browsers that support only HTTP/1.0) support name-based vhosts, but if you want to be universal, this is the way to go.

Note

Kernel support for multi-IP-hosting, called IP aliasing, must be enabled for these commands to work. This is the default setting for SUSE Linux.


Setting up IP-based vhosting is a two-step process. First, you must identify the addresses to use. As the SuperUser, run ifconfig to find the Internet address (listed here as inet addr) attached to eth0, your Ethernet connection. It should show up similar to the following:

inet addr:172.26.1.33 

Then use ifconfig to bind more addresses to the network interface:

/sbin/ifconfig eth0:0 172.26.1.40 /sbin/ifconfig eth0:1 172.16.1.41 

Now you can enter the same VirtualHost statements in httpd.conf as you did when using names, with only the VirtualHost statement changed:

<VirtualHost 172.26.1.40>     ServerName www.YourName.com     DocumentRoot /srv/www/htdocs/YourName.com     ServerAdmin webmaster@YourName.com     ErrorLog /var/log/apache2/www.YourName.com-error_log     CustomLog /var/log/apache2/www.YourName.com-access_log common </VirtualHost> <VirtualHost 172.26.1.41>     ServerName www.susefan.com     DocumentRoot /srv/www/htdocs/susefan.com     ServerAdmin webmaster@susefan.com     ScriptAlias /srv/www.htdocs/susefan.com/cgi-bin     ErrorLog /var/log/apache2/www.susefan.com-error_log     CustomLog /var/log/apache2/www.susefan.com-access_log common </VirtualHost> 



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net