If you would like a more in-depth discussion of virtual hosts, refer to Apache Virtual Host Documentation
If you are using the Zend Framework and/or Ubuntu you may prefer to follow this article: Appendix A. Creating A Local Domain Using Apache Virtual Hosts from Surviving the Deep End!,a free book about the Zend Framework for the PHP language
Virtual hosts lets us create new domains for each project and removes the need to maintain everything as a set of subdirectories under localhost. 1
Step 1: Edit httpd.conf
In the httpd.conf file, located at c:\wamp\bin\apache\apache2.2.11\conf\httpd.conf, locate the following line and make sure it is uncommented, meaning there is no '#' at the beginning of the line. This is how it should look:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Step 2: Edit httpd-vhosts.conf
The httpd-vhosts.conf file is located at c:\wamp\bin\apache\apache2.2.11\ conf\extra\httpd-vhosts.conf
Here is a sample httpd-vhosts.conf file:
Any line that begins with '#' is ignored by Apache. This is a comment-line.
The NameVirtualHost directive specifies the IP-address (and possibly the port) that will be accepting requests for the hosts. '*' is a wild-card meaning that any and all IP-addresses on the web-server should be used. The colon ':' separates the IP-address from the port number. '80' means the web-server will accept requests on port 80.
Next, there are two <VirtualHost> blocks, one for each domain. The argument for the
If you are adding virtual hosts to an existing web server, you must also 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. List this virtual host first in the configuration file so that it will act as the default host.2
For the first block we are just making sure that localhost (existing host discussed above) is preserved:
The final part is to add a Directory configuration for the new Virtual Host's Document Root. This ensures Apache can serve requests from this directory by supplying appropriate permissions. Apache will not serve files from a directory without the correct access permissions. By default, options are severely limited to prevent unauthorised access from the web, so be sure to add this to allow files to be served from the Document Root. The options I'm using here are the usual ones Apache applies to localhost's Document Root which are fairly liberal and allow for the use of .htaccess files so users can access Apache configuration options at runtime.3>
Step 3: Configuring the Local HOSTS file.
The new domain must also be 'mapped' to the correct local IP. This is easily resolved by adding the new domain name to the local HOSTS file which recognises and maps local domains to IP addresses
In Windows this file is located at C:\Windows\System32\drivers\etc\hosts
This is what the file looks like:
Apache Virtual Host Documentation http://httpd.apache.org/docs/1.3/vhosts/
Apache Name-Based Virtual Hosts
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
Footnotes:
1 and 3 :From "Creating A Local Domain Using Apache Virtual Hosts"
The httpd-vhosts.conf file is located at c:\wamp\bin\apache\apache2.2.11\ conf\extra\httpd-vhosts.conf
Here is a sample httpd-vhosts.conf file:
# Setup Listening Port
NameVirtualHost *:80
# Ensure "localhost" is preserved
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:\wamp\www"
</VirtualHost>
# Setup "helloworld" Virtual Host
<VirtualHost *:80>
ServerName helloworld.tld
DocumentRoot "C:\projects\helloworld\public"
<Directory "C:\projects\helloworld\public">
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Explanation:Any line that begins with '#' is ignored by Apache. This is a comment-line.
The NameVirtualHost directive specifies the IP-address (and possibly the port) that will be accepting requests for the hosts. '*' is a wild-card meaning that any and all IP-addresses on the web-server should be used. The colon ':' separates the IP-address from the port number. '80' means the web-server will accept requests on port 80.
Next, there are two <VirtualHost> blocks, one for each domain. The argument for the
<VirtualHost>
directive should be the same as the argument to the NameVirtualHost
directive (ie, an IP address, or *
for all addresses). Inside each <VirtualHost>
block, you will need at minimum a ServerName directive to designate which host is served and a DocumentRoot directive to show where in the filesystem the content for that host lives. If you are adding virtual hosts to an existing web server, you must also 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. List this virtual host first in the configuration file so that it will act as the default host.2
For the first block we are just making sure that localhost (existing host discussed above) is preserved:
# Ensure "localhost" is preserved
<VirtualHost *:80>
ServerName localhost
DocumentRoot "C:\wamp\www"
</VirtualHost>
In the second <VirtualHost> block we create the "helloworld" virtual host:# Setup "helloworld" Virtual Host
<VirtualHost *:80>
ServerName helloworld.tld
DocumentRoot "C:\projects\helloworld\public"
<Directory "C:\projects\helloworld\public">
Options Indexes FollowSymLinks Includes
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
The final part is to add a Directory configuration for the new Virtual Host's Document Root. This ensures Apache can serve requests from this directory by supplying appropriate permissions. Apache will not serve files from a directory without the correct access permissions. By default, options are severely limited to prevent unauthorised access from the web, so be sure to add this to allow files to be served from the Document Root. The options I'm using here are the usual ones Apache applies to localhost's Document Root which are fairly liberal and allow for the use of .htaccess files so users can access Apache configuration options at runtime.3>
Step 3: Configuring the Local HOSTS file.
The new domain must also be 'mapped' to the correct local IP. This is easily resolved by adding the new domain name to the local HOSTS file which recognises and maps local domains to IP addresses
In Windows this file is located at C:\Windows\System32\drivers\etc\hosts
This is what the file looks like:
# Copyright (c) 1993-2006 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
127.0.0.1 localhost
127.0.0.1 helloworld.tld
Sources:Creating A Local Domain Using Apache Virtual Hosts http://www.survivethedeepend.com/zendframeworkbook/en/1.0/creating.a.local.domain.using.apache.virtual.hosts
Apache Name-Based Virtual Hosts
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
Footnotes:
1 and 3 :From "Creating A Local Domain Using Apache Virtual Hosts"