Virtual Hosts with Apache
This is somewhat related to my recent post on using Apache and IIS on the same machine at the same time. It is also something that I tend to have to search for whenever I set up a new PC (or rebuild one). So here it is, for posterity.
This is actually about Apache specifically; IIS doesn’t matter here.
Apache has a neat feature: virtual hosts. This feature allows you to designate multiple local ’sites’, and each has it’s own site root.
Normally, local in-development websites would be served by Apache on the default localhost URL…
http://localhost/Site1
Now what if you had multiple sites running on Apache? You would have a bunch of URLs like:
http://localhost/Site1/
http://localhost/Foo/
http://localhost/Test/
(replace the purely generic example names above with ones more applicable to you)
The localhost part gets a bit annoying after having to type it over and over. And it doesn’t really resemble the structure of a live site, does it? This is where Apache’s virtual hosts feature comes in useful. You are able to define virtual host for each site, such that the site can be accessed using a URL such as:
http://Site1/
http://Foo/
http://Test/
To make this work, you need to edit to separate configuration files, as follows.
Apache - HTTP.conf
In your Apache/conf folder, open the httpd.conf in a text editor such as Notepad. Scroll all the way to the bottom of the file. Add the following block of code at the end:
<VirtualHost Site1:81>
ServerName Site1
DocumentRoot "C:/public_html/Site1"
</VirtualHost>
This sample block essentially tells Apache you are defining a virtual site, with a specified physical location and URL (edit as needed).
Windows - hosts
For the second half of the solution, you need to navigate deeply into the internals of Windows to edit the hosts file. This file controls where the system will first look when given a URL in the URL field of a web browser. If you add your local site to this file, Windows will check this file first then the site is requested in a web browser, and send the local site to the browser instead of looking online.
The hosts file (there is no extension, just the filename) is located at WINDOWS\system32\drivers\etc\ (WINDOWS may be different based on your setup, could be Windows or WINNT). Just open the file in a text editor like Notepad, and add the following line at the bottom:
127.0.0.1 Site1
The first part is the IP address where the browser request should be routed, followed by (with at least one space in between) the basic URL you want to use for your local site (edit to match the ServerName defined in Apache, above). You can add multiple entries in this file, each pair separated by a new line.
Save and close the file.
Now restart Apache. Apache only reads it’s conf file on startup, so this step is necessary. If there are no errors, open a web browser and type your ServerName value in the address bar. The browser should display whatever site is located in the physical location associated with the virtual site defined in Apache.
If you have multiple local sites you want to use in this fashion, repeat the above steps - editing Apache’s conf file and the Windows hosts file - for each one. Remember to restart Apache before testing, so the changes will be applied.
Conclusion
This is a very useful technique to utilize when developing and testing multiple sites locally, as they each get their own unique URL and their own space.