Share this page:
Apache can serve several web sites on a single server. Each site may have different configurations. Apache uses virtual hosts to group configuration settings for each site. Virtual host settings are enclosed in virtual host tags:
An IP address can be used instead of a wildcard. This determines which of the server's network interfaces the virtual host uses to listen for HTTP requests.
Suppose a server has two ethernet interfaces with IP addresses 192.168.1.23 and 192.168.1.24. The following virtual hosts would serve domain1.com on the first IP address and domain2.com on the second IP address:
The port number can be changed if you want Apache to use a different port.
Each virtual host should have a DocumentRoot directive, which specifies the directory where the server's content is stored. The default directory is /var/www, but different virtual hosts can have different root directories.
When a web browser requests a page from a server, it sends and HHTP request packet where the HTTP host field contains the domain name of the site being requested. When Apache receives a request, it checks the HTTP host field and looks for a virtual host with a matching ServerName. The ServerName corresponds to the domain name of a web site. The ServerName must not be confused with the server's host name.
The ServerAlias directive is used to match alternate domain names to a virtual host. For example, if you want a virtual host to match mydomain.com and www.mydomain.com, you would need to use the ServerAlias directive like this:
The Directory directive applies a set of rules to a directory. For example, the following code restricts access to a directory so that it can only be accessed from IP address 192.168.0.3:
A Location is similar to a Directory, but instead of applying to directories, they are sets of rules applied to a URL.
By default, Apache uses /usr/lib/cgi-bin to store CGI scripts. The ScriptAlias directive tells Apache that files in /usr/lib/cgi-bin/ should be handled as CGI scripts. URLs whose path starts with /cgi-bin/ will be mapped on to files in /usr/lib/cgi-bin/. For example, consider a site named mydomain.com that contains a script called myscript.cgi stored in /usr/lib/cgi-bin. This script could be reached through http://mydomain.com/cgi-bin/myscript.cgi.
The following code is used to define the CGI directory and allow CGI scripts to be executed:
The Options directive is used to enable CGI execution.
This is the default virtual host file installed with Apache 2.2:
Virtual host files are stored in /etc/apache2/sites-available, but Apache only looks for virtual host files in /etc/apache2/sites-enabled. The a2ensite command must be used to create a link from files in the sites-available directory to the sites-enabled directory.
If I had a virtual host file named mydomain.conf stored in /etc/apache2/sites-available, I would enable it with this command:
This command creates a file-system link making mydomain.conf visible in /etc/apache2/sites-enabled. When Apache starts up, it loads virtual host files from /etc/apache2/sites-enabled.
The site can be disabled with this command: