Tutorial on setting up virtual hosts in Apache 2.2

Changes to the way Apache handles virtual hosts in the 2.2 series affect the instructions for setting up virtual hosts in Windows on pages 86-7 of Foundation PHP for Dreamweaver 8. The most important difference is that Apache 2.2 applies stricter permissions than either the 1.3 or the 2.0 series, so you’re likely to see a “Forbidden” message rather than the website that you were expecting. The other change is that the main configuration file, httpd.conf no longer contains everything under the sun. Virtual hosts and other optional features are now controlled by external files.

I’ve created a tutorial showing how to set up virtual hosts on Apache 2.2 in Windows. Although it was originally intended as an update for my book, I decided to give full instructions, rather than just outline the differences. As a result, the tutorial can be used even if you don’t have a copy of Foundation PHP for Dreamweaver 8. Although why anybody should be without a copy is one of the great mysteries of the universe. 😉

If you haven’t experimented with virtual hosts in your local development environment, they’re well worth looking into – and they’re not difficult to set up. If you locate your testing sites in the Apache server root, localhost becomes the site root, and what should be the top level of the site goes in a subfolder. For simple sites, this doesn’t really matter, but it can make testing very difficult if you want to use links in PHP includes.

Root-relative links cannot be tested if the site is in a subfolder of the server root, but using a virtual host solves the problem. A virtual host gives you exactly the same structure in your testing environment as a live site because the top level folder of the virtual host becomes the testing site root. If you make extensive use of includes, virtual hosts are the way to go.

This entry was posted in Apache, Books, Dreamweaver. Bookmark the permalink.

13 Responses to Tutorial on setting up virtual hosts in Apache 2.2

  1. Joedank says:

    Hi david,

    You’ll probably want to erase this comment but I need your email address and can’t find it on your site.

  2. David says:

    Sorry, Joe. I don’t make my email address publicly available. I recognize your name from the friends of ED forum, so you can contact me through the forum or ask foED to forward an email to me.

  3. BB says:

    David, I’m going through your PHP for DW 8 book, and I’ve been googling all day to get virtual host permissions to work right with Apache 2.2. Should have known to come right to the source in the first place. Thanks.

  4. Pery says:

    I’ve tried your tutorial on Creating virtual hosts on Apache 2.2 with success but the tutorial is intended for use only in a local testing environment (locally everything is ok).
    What changes must i make in order to access them from internet? I’m getting the 403 error.
    Thanks

  5. David says:

    Sorry, Pery, I don’t know. The web server that this site runs on is Apache 2.0, so it’s not an issue that I have needed to face apart from in a local testing environment. I suggest that you ask in a dedicated Apache forum. I would hate to give you information that rendered your server insecure.

  6. PEry says:

    Maby you didn’t understand.
    Let me try to explain.
    I’m not very good with english.
    If i type localhost/something1 and localhost/something2 everything is working in my pc. I can access both sites.
    If i try to access them from internet i only see that http 403 error permission denied. http://something1 and http://something2 don’t work.
    Any sugestion?
    Thanks

  7. David says:

    What do you mean by “accessing them from internet”? Are you trying to access http://something1 from a different computer? If so, it won’t work.

  8. Damian says:

    David, I performed everything you mentioned in your tutorial but I get the Forbidden error msg when I try accessing my virtualhost’s root folder. I get this error in my error log:

    [error] [client 127.0.0.1] Directory index forbidden by Options directive: C:/vhosts/A2/

    A2 being my virtualhost. This is weird because I did set the C:/vhosts directive’s permissions like you stated, so any subfolders should inherit these permissions right?

  9. David says:

    No, Damian. Directory index is something different. What Apache is telling you is that you cannot list the contents of a folder. Try accessing a file inside the vhosts folder.

  10. Damian says:

    Oh OK. Well now my problem is that the file I request is a Perl script and my browser just shows its code. Thats what happens when the script is right in the DocumentRoot folder (C:/vhosts/A2/index2.cgi). So why does the script not run?

    Originally I had a folder called “cgi-bin” in my virtualhost’s DocumentRoot and in that folder was the Perl script (so C:/vhosts/A2/cgi-bin/index2.cgi). When trying to run the script in this location I got this error:

    [error] [client 127.0.0.1] script not found or unable to stat: C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin/index2.cgi

    It seems here that Apache is trying to search in the original cgi-bin folder… but why?

  11. Kim says:

    Dave
    I was wondering thought about this program (freeware) for installing Apache 2.x.x, PHP 5.x.x, MySQL 5.x.x, and PhpMyAdmin. It’s called vertrigo.

    http://sourceforge.net/projects/vertrigo

  12. Chang says:

    Until I found your virtual hosts tutorial, I had been settling for moving whole sites in and out of the Apache htdocs folder.

    I literally had spent hours trying to get virtual hosts working without 403 errors until I stumbled upon your tutorial. It was the first one that was correct!

    Thankyou!

  13. sundanse says:

    Thank you so much for creating the “virtual hosts in apache 2.2” tutorial. I was able to resolve the “forbidden” issue in minutes.

    Respect!