Virtual Hosts

This article shows how to set up virtual hosts on your PC using Apache 2.0.x. This should work on any Apache platform though.

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 *
# (1) No subdomain: http://mycomputer.pc
<VirtualHost *>
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 *>
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 *>
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.

Syndicate content