Before delving into a discussion on virtual hosts, you need to become familiar with the Virtual Hosts section in the httpd.conf file. The following section (Section 3) contains a few directives that you should be familiar with before you begin:
# Section 3: Virtual Hosts # # VirtualHost: If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at <URL:http://www.apache.org/docs/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # #NameVirtualHost * # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. # #<VirtualHost *> # ServerAdmin firstname.lastname@example.org # DocumentRoot /www/docs/dummy-host.example.com # ServerName dummy-host.example.com # ErrorLog logs/dummy-host.example.com-error_log # CustomLog logs/dummy-host.example.com-access_log common #</VirtualHost>
In the preceding section, note the following:
- You use the NameVirtualHost directive to specify that you will use name-based virtual hosts. The syntax of this directive is
NameVirtualHost address [:port]
where address is the IP address of the virtual host and port is the port number that the virtual host will listen to. This directive should be configured only for name-based virtual hosts.
Note I discuss name-based virtual hosts later in this chapter, in the section “Name-Based Virtual Hosts.”
- You use the <VirtualHost> and </VirtualHost> directives to specify a list of configuration directives for a virtual host. These directives are referred when a document is requested from the virtual host. The syntax for using this directive is
<VirtualHost address[:port]> â€¦add configuration directives </VirtualHost>
where address is either the IP address of the virtual host or a fully qualified domain name.
- Several directives are specified within the <VirtualHost> and </VirtualHost> section. These directives include, ServerAdmin, DocumentRoot, ServerName, ErrorLog, and CustomLog.
- You use the ErrorLog and CustomLog directives to maintain necessary logs for the virtual host configured on the Apache Web server.