Let me start this off with an interesting quote from Dave Shea, in “Virtual Hosts for Dummies” on his website www.mezzoblue.com …
“Virtual hosts enable you to intelligently run multiple sites on a single server. The useful side effect is that with proper setup, you can point your browser to www.whatever.whatever and load a local copy. My development site is now www.mezzoblue.dev, which works exactly the same as the .com, just faster. I dont even need a connection to work on it, because its all local; all my PHP scripts and Movable Type templates work, and the local filesystem access is so much nicer than using FTP.”
I don’t mind saying that my complete ignorance about Apache in general and virtual hosting in particular cost me several days of grief while I tried to figure out why what I thought I was doing wouldn’t work. The good news is that I did figure it out, finally, and it works just like everyone says it does.
I’ve now got 10 virtual sites hosted on my personal copy of Apache Server and the original that Apache set up during the installation. I’ve not read anything anywhere that indicates limitations, so I’d guess that you could set up as many virtual hosts as you want to.
Preparing for Virtual Hosts
The configuration requires modifing the main configuration file, (optionally) adding another configuration file devoted to virtual hosting and changing (adding information to) a Windows system file. None of it is particularly difficult, but you do need to pay attention.
There are two types of virtual hosting that Apache supports: IP based and name based. Since I don’t know much about using IP addresses, I used name based virtual hosting. The guide I used for this back in 2007 was Installing and configuring Apache on Windows XP, but I’m sure there are probably many newer ones on the internet now that would help you understand what you’re doing and why you should do it.
The first thing you need to do is set up a location for your virtual sites. In my case, I wanted my virtual sites to be on my Web Development drive (drive D:), so I created a folder called ApacheVirtualSites there. Inside that folder I created folders named for each of my (proposed) virtual sites – in this case three new folders. Of course you can name your virtual sites anything you want, but I chose to name mine after a couple of actual sites we have – with modification. For instance, if my actual site was “www.mysite1.com”, I named my virtual site “mysite1.local”. The second one would be “mysite2.local” and the third one – named for this project – “myhelpfile.local”. So what I wound up with looked like this:
D:/Web Development/ ApacheVirtualSites/ MySite1.local/ MySite2.local/ MyHelpFile.local/
Then, inside each of the new virtual sites, I created new folders named “cgi-bin”, “httpdocs” and “logs”. “httpdocs” is the name of my DocumentRoot in each of my virtual websites. I chose that name, rather than the Apache default of “htdocs”, because the Hosting Company where our actual websites are hosted requires that as their root, and I thought things would be easier to keep up with if I kept everything as consistant as I could.
Changing the Apache Configuration File
Okay, now we’re ready to create the actual virtual host configuration files. You’re faced with a choice here: you can place everything within the main configuration file, or you can create a separate configuration file strictly for virtual hosting and link it into the the main configuration file. I chose the second course – mostly so I could find the configuration information quicker in case I screwed something up and needed to change it (a reasonable idea, as it turned out). So, using your word editor, create a file named “virtual-hosts.conf” and save it the “../Apache2.2/conf ” folder. In this file, we need to type in the information for the first virtual host:
# Use name-based virtual hosting.
NameVirtualHost *:80<VirtualHost *:80>
ServerName MySite1.local
DocumentRoot “D:/ApacheVirtualSites/MySite1.local/httpdocs”
</VirtualHost>
Now, copy the part of the file that begins with “<VirtualHost…” and ends with “</VirtualHost…”, and paste it into this file twice more. Then change the “ServerName…” and “DocumentName…” to reflect your other two virtual sites. You should wind up with something that looks similar to this:
# Use name-based virtual hosting.
NameVirtualHost *:80<VirtualHost *:80>
ServerName MySite1.local
DocumentRoot “D:/ApacheVirtualSites/MySite1.local/httpdocs”
</VirtualHost><VirtualHost *:80>
ServerName MySite2.local
DocumentRoot “D:/ApacheVirtualSites/MySite2.local/httpdocs”
</VirtualHost><VirtualHost *:80>
ServerName MyHelpFile.local
DocumentRoot “D:/ApacheVirtualSites/MyHelpFile.local/httpdocs”
</VirtualHost>
This the minimal amount of information Apaches needs to create the virtual sites. The only thing you need to be careful of is if you have any spaces anywhere in the addresses, you must enclose the entire address in quotes. You might notice that I’ve put all the “DocumentRoot” addresses in quotes – I saw a comment on one of the tutorials that suggested that be done on Windows installations. The manual doesn’t do that in their tutorial on virtual hosting, so I don’t know if it’s really necessary, but… I guess I go by a “better safe than sorry” approach, and, after all, what can it hurt? Also, here is where you make any configuration changes that concern your virtual sites. You can configure each virtual site differently from the others, so it’s easy to try things out 😉 Most tutorials recommend putting dedicated log files in each virtual site, so this is what that looks like:
# Use name-based virtual hosting.
NameVirtualHost *:80<VirtualHost *:80>
ServerName MySite1.local
DocumentRoot “D:/ApacheVirtualSites/MySite1.local/httpdocs”
CustomLog D:/ApacheVirtualSites/MySite1.local/logs/MySite1.local.access.log combined
ErrorLog D:/ApacheVirtualSites/MySite1.local/logs/MySite1.local.error.log
</VirtualHost><VirtualHost *:80>
ServerName MySite2.local
DocumentRoot “D:/ApacheVirtualSites/MySite2.local/httpdocs”
CustomLog D:/ApacheVirtualSites/MySite2.local/logs/MySite2.local.access.log combined
ErrorLog D:/ApacheVirtualSites/MySite2.local/logs/MySite2.local.error.log
</VirtualHost><VirtualHost *:80>
ServerName MyHelpFile.local
DocumentRoot “D:/ApacheVirtualSites/MyHelpFile.local/httpdocs”
CustomLog D:/ApacheVirtualSites/MyHelpFile.local/logs/MyHelpFile.local.access.log combined
ErrorLog D:/ApacheVirtualSites/MyHelpFile.local/logs/MyHelpFile.local.error.log
</VirtualHost>
Save this file and open up your main configuration file (httpd.conf) and scroll down almost to the bottom and find where it says #Virtual hosts, probably around line 464. Right below that is a commented-out include statement. You’re going to add an include statement, so you can un-comment and replace this one, or just add your new one below it. Anyway, you want to add:
Include conf/virtual-hosts.conf
This adds the virtual host configuration file you created earlier to the main Apache configuration. Save your main configuration file.
Changing the Windows “hosts” file
Now you need to find a file named “hosts” (no extensions) in your Operating System. In Windows XP Pro and Windows 7 it is usually at Windows > System32 > drivers > etc > hosts At the bottom of this file you should see something like: 127.0.0.1 localhost To that you want to add your other virtual sites. What you finish with should look like this:
127.0.0.1 localhost
127.0.0.1 MySite1.local
127.0.0.1 MySite2.local
127.0.0.1 MyHelpFile.local
All the tutorials I read ended at this point, but I couldn’t get virtual hosting to work. I kept getting a “Client denied by server configuration” message. After doing more research on the Internet, I found that one needed to go in and explicitly allow access to the virtual host sites. That required adding the following to the “virtual-hosts.conf” file:
<Directory E:/ApacheVirtualSites>
Options Indexes FollowSymLinks Includes
Order allow,deny
Allow from all
</Directory>
I added this just after the NameVirtualHost *:80 but before the first <VirtualHost… statement. Everything seems to work O.K., so I guess it’s right.
That should complete the enabling of virtual hosts on Apache. You’ll need to re-set Apache and re-start your browser (after changing the “hosts” file), but you should be in business. Enjoy!
As an afterword, I ran across a totally different way of using virtual hosting on Apache: see here: “Create an Apache Testing Server on Windows“. You can find the documentation for this in the Apache manual, so I have no reason to believe that it won’t work. I’m just anal enough to want to do it the “hard” way, I guess, but I’m offering the link as a possible alternative.