### Apache HTTP Server Version 1.3

# Virtual Host Support

See Also: Non-IP based virtual hosts

## What are virtual hosts?

This is the ability of a single machine to be a web server for multiple domains. For example, an Internet service provider might have a machine called `www.serve.com` which provides Web space for several organizations including, say, smallco and baygroup. Ordinarily, these groups would be given parts of the Web tree on www.serve.com. So smallco's home page would have the URL

> http://www.serve.com/smallco/

and baygroup's home page would have the URL

> http://www.serve.com/baygroup/

For esthetic reasons, however, both organizations would rather their home pages appeared under their own names rather than that of the service provider's; but they do not want to set up their own Internet links and servers.

Virtual hosts are the solution to this problem. smallco and baygroup would have their own Internet name registrations, `www.smallco.com` and `www.baygroup.org` respectively. These hostnames would both correspond to the service provider's machine (www.serve.com). Thus smallco's home page would now have the URL

> http://www.smallco.com/

and baygroup's home page would would have the URL

> http://www.baygroup.org/

## System requirements

Due to limitations in the HTTP/1.0 protocol, the web server must have a different IP address for each virtual host. This can be achieved by the machine having several physical network connections, or by use of a virtual interface on some operating systems.

## How to set up Apache

There are two ways of configuring apache to support multiple hosts. Either by running a separate httpd daemon for each hostname, or by running a single daemon which supports all the virtual hosts.

Use multiple daemons when:

- The different virtual hosts need very different httpd configurations, such as different values for: ServerType, User, Group, TypesConfig or ServerRoot.
- The machine does not process a very high request rate.

Use a single daemon when:

- Sharing of the httpd configuration between virtual hosts is acceptable.
- The machine services a large number of requests, and so the performance loss in running separate daemons may be significant.

## Setting up multiple daemons

Create a separate httpd installation for each virtual host. For each installation, use the BindAddress directive in the configuration file to select which IP address (or virtual host) that daemon services. E.g.,

> `BindAddress www.smallco.com`

This hostname can also be given as an IP address.

## Setting up a single daemon

For this case, a single httpd will service requests for all the virtual hosts. The VirtualHost directive in the configuration file is used to set the values of ServerAdmin, ServerName, DocumentRoot, ErrorLog and TransferLog configuration directives to different values for each virtual host. E.g.,

> `<VirtualHost www.smallco.com>
> ServerAdmin webmaster@mail.smallco.com
> DocumentRoot /groups/smallco/www
> ServerName www.smallco.com
> ErrorLog /groups/smallco/logs/error_log
> TransferLog /groups/smallco/logs/access_log
> </VirtualHost>
> <VirtualHost www.baygroup.org>
> ServerAdmin webmaster@mail.baygroup.org
> DocumentRoot /groups/baygroup/www
> ServerName www.baygroup.org
> ErrorLog /groups/baygroup/logs/error_log
> TransferLog /groups/baygroup/logs/access_log
> </VirtualHost>
> `

This VirtualHost hostnames can also be given as IP addresses.

Almost ANY configuration directive can be put in the VirtualHost directive, with the exception of ServerType, User, Group, StartServers, MaxSpareServers, MinSpareServers, MaxRequestsPerChild, BindAddress, PidFile, TypesConfig, and ServerRoot.

SECURITY: When specifying where to write log files, be aware of some security risks which are present if anyone other than the user that starts Apache has write access to the directory where they are written. See the security tips document for details.

## File Handle/Resource Limits:

When using a large number of Virtual Hosts, Apache may run out of available file descriptors 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 VirtualHost sections, but only log to the main log files.
- 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`

The have been reports that Apache may start running out of resources allocated for the root process. This will exhibit itself as errors in the error log like "unable to fork". There are two ways you can bump this up:

1. Have a `csh` script wrapper around httpd which sets the "rlimit" to some large number, like 512.
2. Edit http_main.c to add calls to setrlimit() from main(), along the lines of

               struct rlimit rlp;

               rlp.rlim_cur = rlp.rlim_max = 512;
               if (setrlimit(RLIMIT_NPROC, &rlp)) {
                   fprintf(stderr, "setrlimit(RLIMIT_NPROC) failed.\n");
                   exit(1);
               }

   (thanks to "Aaron Gifford <agifford@InfoWest.COM>" for the patch)

The latter will probably manifest itself in a later version of Apache.

### Apache HTTP Server Version 1.3