Tuesday, August 18, 2009

Linux: apache virtual server

Apache Virtual Host documentation

Available Languages: de | en | es | ja | ko | ru | tr

The term Virtual Host refers to the practice of running more than one web site (such as www.company1.com and www.company2.com) on a single machine. Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.

Apache was one of the first servers to support IP-based virtual hosts right out of the box. Versions 1.1 and later of Apache support both IP-based and name-based virtual hosts (vhosts). The latter variant of virtual hosts is sometimes also called host-based or non-IP virtual hosts.

Below is a list of documentation pages which explain all details of virtual host support in Apache version 1.3 and later.

* Virtual Host Support
* Configuration directives

See also

* mod_vhost_alias
* Name-based virtual hosts
* IP-based virtual hosts
* Virtual host examples
* File descriptor limits
* Mass virtual hosting
* Details of host matching

top
Virtual Host Support

* Name-based Virtual Hosts (More than one web site per IP address)
* IP-based Virtual Hosts (An IP address for each web site)
* Virtual Host examples for common setups
* File Descriptor Limits (or, Too many log files)
* Dynamically Configured Mass Virtual Hosting
* In-Depth Discussion of Virtual Host Matching

top
Configuration directives

*
* NameVirtualHost
* ServerName
* ServerAlias
* ServerPath

If you are trying to debug your virtual host configuration, you may find the Apache -S command line switch useful. That is, type the following command:

/usr/local/apache2/bin/httpd -S

This command will dump out a description of how Apache parsed the configuration file. Careful examination of the IP addresses and server names may help uncover configuration mistakes. (See the docs for the httpd program for other command line options)

Available Languages: de | en | es | ja | ko | ru | tr






File Descriptor Limits

Available Languages: en | ja | ko | tr

When using a large number of Virtual Hosts, Apache may run out of available file descriptors (sometimes called file handles) if each Virtual Host specifies different log files. The total number of file descriptors used by Apache is one for each distinct error log file, one for every other log file directive, plus 10-20 for internal use. Unix operating systems limit the number of file descriptors that may be used by a process; the limit is typically 64, and may usually be increased up to a large hard-limit.

Although Apache attempts to increase the limit as required, this may not work if:

1. Your system does not provide the setrlimit() system call.
2. The setrlimit(RLIMIT_NOFILE) call does not function on your system (such as Solaris 2.3)
3. The number of file descriptors required exceeds the hard limit.
4. Your system imposes other limits on file descriptors, such as a limit on stdio streams only using file descriptors below 256. (Solaris 2)

In the event of problems you can:

* Reduce the number of log files; don't specify log files in the sections, but only log to the main log files. (See Splitting up your log files, below, for more information on doing this.)
* If you system falls into 1 or 2 (above), then increase the file descriptor limit before starting Apache, using a script like

#!/bin/sh
ulimit -S -n 100
exec httpd

Please see the Descriptors and Apache document containing further details about file descriptor problems and how they can be solved on your operating system.
top
Splitting up your log files

If you want to log multiple virtual hosts to the same log file, you may want to split up the log files afterwards in order to run statistical analysis of the various virtual hosts. This can be accomplished in the following manner.

First, you will need to add the virtual host information to the log entries. This can be done using the LogFormat directive, and the %v variable. Add this to the beginning of your log format string:

LogFormat "%v %h %l %u %t \"%r\" %>s %b" vhost
CustomLog logs/multiple_vhost_log vhost

This will create a log file in the common log format, but with the canonical virtual host (whatever appears in the ServerName directive) prepended to each line. (See Custom Log Formats for more about customizing your log files.)

When you wish to split your log file into its component parts (one file per virtual host) you can use the program split-logfile to accomplish this. You'll find this program in the support directory of the Apache distribution.

Run this program with the command:

split-logfile < /logs/multiple_vhost_log

This program, when run with the name of your vhost log file, will generate one file for each virtual host that appears in your log file. Each file will be called hostname.log.

No comments:

Post a Comment