Virtual hosts using Apache on Windows
December 28th, 2006 at 10:44 am (Apache Articles, Server Administration)
How to set up virtual hosts using Apache on Windows platform.
Related to the other articles in this section, open up your httpd.conf file in a text editor, wordpad or notepad, and add a few lines. The file, from the default installation is found in the X:\Program Files\Apache Group\Apache2\conf folder.
To avoid having to continually editing this particular file, I add one line towards the bottom of the document.
Include conf/hosts.conf
Save the file. Next step is to create the afore mentioned hosts.conf file. Using your text editor create a new document and copy the virtual host example that is commented out in httpd.conf.
#NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
Uncomment the text, remove the #’s and change to fit your need. In the example below there are 3 settings on a made up domain name, mycomputer.pc.
- No subdomain
- Specific subdomain
- Wildcard subdomain
NameVirtualHost *:80
# (1) No subdomain: http://mycomputer.pc
<VirtualHost *:80>
ServerAdmin virtual@mycomputer.pc
DocumentRoot "X:/Program Files/Apache Group/Apache2/public_html"
ServerName mycomputer.pc
ServerAlias mycomputer.pc
</VirtualHost>
# (2) Specific subdomain: http://test.mycomputer.pc
<VirtualHost *:80>
ServerAdmin test@mycomputer.pc
DocumentRoot "X:/Program Files/Apache Group/Apache2/public_html/test"
ServerName test.mycomputer.pc
ServerAlias test.mycomputer.pc
</VirtualHost>
# (3) Wildcard subdomain: http://*.mycomputer.pc
<VirtualHost *:80>
ServerAdmin webmaster@mycomputer.pc
DocumentRoot "X:/Program Files/Apache Group/Apache2/public_html"
ServerName *.mycomputer.pc
ServerAlias *.mycomputer.pc
</VirtualHost>
The comments within the code are fairly self-explanatory, but (3) in particular allows you to put “anything” before the domain provided that the host exists in your HOSTS file. Save the file as hosts.conf in X:\Program Files\Apache Group\Apache2\conf.
The HOSTS file is usually located in %SystemRoot%\system32\drivers\etc folder. Open the file with wordpad and then add the following to match your virtual host settings above.
# PC (NETWORK, CHANGE IP TO MATCH NETWORK IP)
192.168.1.2 mycomputer.pc
192.168.1.2 www.mycomputer.pc
192.168.1.2 test.mycomputer.pc
192.168.1.2 blah.mycomputer.pc
# PC (LOCAL)
127.0.0.1 mycomputer.pc
127.0.0.1 www.mycomputer.pc
127.0.0.1 test.mycomputer.pc
127.0.0.1 blah.mycomputer.pc
Save the file and restart Apache. You should then be able to navigate to any of the URLs listed above. Try creating a phpinfo() page and see the differences.










romessa said,
December 29, 2006 at 11:36 am
thanks for the nice articles, can you please post some article on regex with apache modrewrite?
romessa
jr said,
July 29, 2008 at 9:08 pm
I read something about the possibility to declare the subdomains as ‘*’ in hosts :
127.0.0.1 *.mycomputer.pc
but i couldn’t make it work :/