How to use Apache Virtual Host to run multiple local websites on Windows
If you have installed Apache, PHP, and MySQL on Windows, you are ready to build your first dynamic, database-driven website. What if you later need to build a second website or third? With a little bit modifications in Apache's configuration file httpd.conf and Windows hosts file, you can set up and run multiple local websites like this:
To set up multiple local sites, we need to modify two files: Windows hosts file and Apache's configuration file.
Modify Windows hosts file When we set up Apache on Windows, I used local site at C:\geeksengine for illustration purpose. The site at C:\geeksengine can be accessed by either of the following URLs:
Wouldn't be nicer if I can access the site like this http://geeksengine (which can instantly tell me I run a local site rather than my internet site http://www.geeksengine.com)? To achieve this effect, we need to modify Windows hosts file.
What is Windows hosts file
Hosts file is a simple text file which contains the mappings of IP addresses to human-recognisable host names (e.g. domain names like http://www.geeksengine.com). It's similar to the structure of internet name servers (a.k.a domain name servers) that implements Domain Name System (DNS) protocol.
Windows hosts file is used by Microsoft TCP/IP for windows. Each entry in hosts file should be kept on an individual line. The IP address should be placed in the first column followed by the corresponding host name. The IP address and the host name should be separated by at least one space. In addition, Comments may be inserted on individual lines by prefix the line with a '#' symbol.
Content in a hosts file can be like this:
How hosts file works
Where to find hosts file
You can locate and open the hosts file by a text editor such as Notepad.
You may have noticed I have an entry in my hosts file for geeksengine. This entry maps from IP address 127.0.0.1 to fake host name geeksengine.
127.0.0.1 ==> geeksengine
Actually, the IP address can be any of your local loopback IPs such as 127.0.0.2, 127.0.0.3 or 192.168.0.1. This way, I can run geeksengine.com locally on my computer as http://geeksengine for development use.
You can edit your hosts file and map your local site(s) to whatever domain name you like. You can even map google.com to IP 127.0.0.1. If you do, everytime you go to http://www.google.com, your localhost website will be displayed.
Modify your hosts file
Now open your hosts file and add entry 127.0.0.1 local_website2 and save it.
Windows Vista and Windows 7 have stricter security mechanism that makes changing the hosts file a lot harder. Please refer to Microsoft Knowledge Base article http://support.microsoft.com/kb/923947 to see how to modify the Hosts file in Windows Vista and Windows 7.
If your computer uses a decent anti-virus software, it should warn you by popping up an alert window about the modifications made in hosts file. This is because hosts file can be the target of some malware or spyware such as browser hijacker.
Click the button on the alert window to accept the change.
Modify Apache configuration file In article How to install Apache on Windows, I set up GeeksEngine as the default local website for Apache. To achieve that, I added in two entries in Apache's httpd.conf file to set up DocumentRoot directory out of which we will serve our local default website:
Your site may be at a different directory. I normally use C drive as I ghost my system on regular basis to backup the whole computer.
Now, it's the fun part. To run a second site (e.g. http://local_website2) on your local Windows system, follow these steps:
Step #1: Create the test PHP page
Step #2: Add a virtual host in httpd.conf file
Step #3: Test the virtual host site
To test if our newly added site works, open your web browser and type http://local_website2 into address bar and hit Enter. If you can see the PHP configuration info as shown below, your setup for the second local website has worked. If it didn't work, restart your Apache server and try again.
I hope you are able to set up your second site running on the same computer using Apache Virtual Hosts.
You can add unlimited sites to your computer and one Apache serves them all.
Copyright © 2017 GeeksEngine.com. All Rights Reserved.
This website is hosted by HostGator.
No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.