Apache 2.2 adopts a modular approach to its main configuration file,
The other change is that Apache 2.2 imposes stricter permissions than previous series, so you need to add an extra command to the virtual hosts definition to prevent getting the following message when accessing a virtual host:
NOTE: Security restrictions on Windows Vista and Windows 7 prevent you from saving edits to the files referred to in these instructions, even if you are logged in as an administrator. To get around this restriction, open Notepad or your script editor from the Start menu by right-clicking the program name and selecting "Run as Administrator" from the context menu. Then open the relevant files by using File > Open inside the program you have just launched. By default, Notepad shows only files with a
httpd.conf
.
Although you can still put everything in the one big file, it's more
efficient to use external files, and include only those that you need to
implement. Consequently, it's no longer recommended to define virtual
hosts at the bottom of httpd.conf
. Instead, you include an external file called httpd-vhosts.conf
.The other change is that Apache 2.2 imposes stricter permissions than previous series, so you need to add an extra command to the virtual hosts definition to prevent getting the following message when accessing a virtual host:
- Forbidden
You don't have permission to access /index.php on this server.
C:\vhosts
.NOTE: Security restrictions on Windows Vista and Windows 7 prevent you from saving edits to the files referred to in these instructions, even if you are logged in as an administrator. To get around this restriction, open Notepad or your script editor from the Start menu by right-clicking the program name and selecting "Run as Administrator" from the context menu. Then open the relevant files by using File > Open inside the program you have just launched. By default, Notepad shows only files with a
.txt
file name
extension, so you need to select the option to view All Files (*.*) in
the Open dialog box. You can then save the files normally after editing
them. (If you're using XAMPP, this restriction applies only to steps 2–4.)-
Create a subfolder inside
C:\vhosts
for each virtual host that you want to add to your Apache server.
-
Open
C:\WINDOWS\system32\drivers\etc\hosts
in Notepad or a script editor. Look for the following line at the bottom:
127.0.0.1 localhost
-
On a separate line, enter
127.0.0.1
, followed by some space and the name of the virtual host you want to register. For instance, to set up a virtual host calledphpdw
, enter the following:
127.0.0.1 phpdw
-
Add any further virtual hosts, each one on a separate line and pointing to the same IP address (
127.0.0.1
). Save thehosts
file, and close it.
-
Open
C:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf
in a text editor. If you're using XAMPP, the file is located atC:\xampp\apache\conf\httpd.conf
. Scroll down to the Supplemental configuration section at the end, and locate the following section (around line 460):
#Virtual hosts #Include conf/extra/httpd-vhosts.conf
-
Remove the # from the second line so the section now looks like this:
#Virtual hosts Include conf/extra/httpd-vhosts.conf
-
Save
httpd.conf
and close it.
-
Open
C:\Program Files\Apache Software Foundation\Apache2.2\conf\extra\httpd-vhosts.conf
in Notepad or a text editor. If you're using XAMPP, the location isC:\xampp\apache\conf\extra\httpd-vhosts.conf
. The main section looks like this:
Note: In XAMPP, all lines are commented out. You must remove the hash mark from the beginning of the line that contains the following directive:
NameVirtualHost *.80
-
Position your cursor in the blank space shown on line 15 in
the preceding screenshot, and insert the following four lines of code:
<Directory C:/vhosts> Order Deny,Allow Allow from all </Directory>
C:\vhosts
as the top-level folder, replace the pathname in the first line. The pathname must use forward slashes in place of the Windows convention of backward slashes. Also surround the pathname in quotes if it contains any spaces.
As long as all your virtual hosts are in subfolders of this top-level folder, this directive sets the correct permissions for all of them. However, if they are in different top-level folders, create a separate<Directory>
directive for each one. -
The code shown on lines 27 through 42 in the preceding
screenshot shows examples of how to define virtual hosts (in XAMPP,
they're commented out). It shows all the commands that can be used, but
only
DocumentRoot
andServerName
are required.
When you enable virtual hosting, Apache disables the main server root, so the first definition needs to reproduce the original server root. You then add each new virtual host within a pair of<VirtualHost>
tags, using the location of the site’s web files as the value forDocumentRoot
, and the name of the virtual host forServerName
. Again, use forward slashes, and if the path contains any spaces, enclose the whole path in quotes. If your server root is located, like mine, atC:\htdocs
, and you are addingphpdw
as a virtual host inC:\vhosts
, change the code shown on lines 27 through 42 so they look like this (in XAMPP, just add these new directives at the bottom of the file, and set theDocumentRoot
forlocalhost
toC:/xampp/htdocs
):
<VirtualHost *:80> DocumentRoot c:/htdocs ServerName localhost </VirtualHost> <VirtualHost *:80> DocumentRoot c:/vhosts/phpdw ServerName phpdw</VirtualHost>
-
Save
httpd-vhosts.conf
, and restart your computer. All sites in the server root will continue to be accessible throughhttp://localhost/sitename/
. Anything in a virtual host will be accessible through a direct address, such ashttp://phpdw/
.
-
If you still have difficulty accessing your virtual hosts, make sure that you have added
index.php
to theDirectoryIndex
directive inhttpd.conf
.