Category: Apache General

Virtual hosts on Apache 2.2

Creating virtual hosts on Apache 2.2
Please also refer to the official documentation on the Apache site.

Apache 2.2 adopts a modular approach to its main configuration file, 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 filed 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. 

Because of the permissions issue, I recommend creating a top-level folder to hold all virtual hosts in your local development environment. The following instructions assume that all virtual hosts are located in a folder called C:vhosts.

Open C:Program FilesApache Software FoundationApache2.2confhttpd.conf in a text editor, 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 FilesApache Software FoundationApache2.2confextrahttpd-vhosts.conf in Notepad or a text editor. The main section looks like this:
Position your cursor in the blank space shown on line 15 in the preceding screenshot, and insert the following four lines of code:

Order Deny,Allow
Allow from all

This sets the correct permissions for the folder that contains the sites you want to treat as virtual hosts. If you chose a location other than 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 directive for each one.
The code shown on lines 27 through 42 in the preceding screenshot shows examples of how to define virtual hosts. It shows all the commands that can be used, but only DocumentRoot and ServerName 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 tags, using the location of the site’s web files as the value for DocumentRoot, and the name of the virtual host for ServerName. Again, use forward slashes, and if the path contains any spaces, enclose the whole path in quotes. If your server root is located, at C:htdocs, and you are adding phpdw as a virtual host in C:vhosts, change the code shown on lines 27 through 42 so they look like this:

DocumentRoot c:/htdocs
ServerName localhost
DocumentRoot c:/vhosts/phpdw
ServerName phpdw

Save httpd-vhosts.conf, and restart apache. All sites in the server root will continue to be accessible through http://localhost/sitename/. Anything in a virtual host will be accessible through a direct address, such as http://phpdw/.
If you still have difficulty accessing your virtual hosts, make sure that you have added index.php to the DirectoryIndex directive in httpd.conf.

Tags:

Apache

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.