In this tutorial, we will discuss how to setup virtual hosts for your development environment (or production, if you wish) on Windows XP. This will allow you to use URLs such as http://mywebsite/ instead of http://localhost/mywebsite/, which is much more realistic.
- First, document where your source code is. This will be our DocumentRoot setting.
- Go to the Start menu and click Run. Type notepad “C:Windowssystem32driversetchosts” and press enter.
- At the end of the file, type 127.0.0.1, tab or space, then the virtual host name. Using the hostname mywebsite, the hosts file would look something like this:
127.0.0.1 localhost 127.0.0.1 mywebsite
- Next, open your Apache configuration file (httpd.conf). This will be located in one of a few different places.
- Zend Server CE: C:Program FilesZend ServerApache2confhttpd.conf
- Apache (default): C:Program FilesApache Software Foundationconfhttpd.conf
- Apache (often suggested): C:apacheconfhttpd.conf
- Find the following lines and uncomment the one starting with Include. (Note: To uncomment the line, remove the # character.)
# Virtual hosts Include conf/extra/httpd-vhosts.conf
- Open /conf/extra/httpd-vhosts.conf. Delete the dummy example code, and replace it with the following:
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot "C:htdocsmywebsitepublic" ServerName mywebsite # Optionally, turn on logging ErrorLog "logs/mywebsite.error.log" CustomLog "logs/mywebsite.access.log" common </VirtualHost>
- After turning on the VirtualHost feature, the main document root will be disabled, so we have to add it as a VirtualHost.
<VirtualHost *:80> DocumentRoot "C:htdocs" ServerName localhost </VirtualHost>
- Next, restart your Apache server instance. This can be done via the Apache Service Monitor or the Administrative Tools > Services section of Control Panel.
- It is possible that we will still not be able to view our newly created virtual host; however, we have a way around this. Go to the Start menu, and click Run. Type cmd and press enter.
- At the Windows Command Prompt, type ipconfig /flushdns. This will purge the DNS Resolver cache.
- Open http://mywebsite/ in your favorite browser.
If you have any trouble, feel free to comment and we’ll do our best to help out.