The term Virtual Host refers to the practice of maintaining more than one server on one machine, as differentiated by their apparent hostname. For example, it is often desirable for companies sharing a web server to have their own domains, with web servers accessible as www.company1.com and www.company2.com, without requiring the user to know any extra path information.
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.
IP-based virtual hosts use the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host. With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.
The following explains how to get name-based virtual hosts running on a single web server with Apache installed.
Log into the web server and back-up the Apache config file - always a good thing to do before editing..
cp /usr/local/apache/conf/httpd.conf /usr/local/apache/conf/httpd.conf.bak
..and then edit the original config file with your favourite text editor..
In httpd.conf, go to the section headed ### Section 3: Virtual Hosts. This is where most of the action takes place.
To add virtual hosts, firstly create a <VirtualHost *> block for the existing host. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName and DocumentRoot (copy from further up the file). List this virtual host first in the configuration file so that it will act as the default host.
For example, suppose that you are serving the domain www.smart.net and you wish to add the virtual host www.uber.net, which points at the same IP address. Then you simply add the following to httpd.conf..
ServerAlias unixzone.net *.unixzone.net
ServerAlias crapfest.net *.crapfest.net
The ServerAlias directive is not strictly necessary, but allows requests for all hosts in the domain.com domain to be served by the www.domain.com virtual host. The wildcard characters * and ? can be used to match names. Of course, you can't just make up names and place them in ServerName or ServerAlias. You must first have your DNS server properly configured to map those names to an IP address associated with your server.
Now when a web page request arrives, the server will first check if it is using an IP address that matches the NameVirtualHost. If it is, then it will look at each <VirtualHost> section with a matching IP address and try to find one where the ServerName or ServerAlias matches the requested hostname. If it finds one, then it uses the configuration for that server. If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a <VirtualHost> container and list it first in the configuration file.
Restart the Apache Web Server for the settings to take effect..
- et voilą! - multiple websites on the same web server!