How to configure apache virtual host in Microsoft Windows for safer development
Typically for web development, one likes to simulate the web environment by using actual domain name as is instead of using some local IP address. It is very useful in case if you want to host WordPress and would like to copy your version of database as is from development server to the the actual hosting server. The applications like WordPress store domain names in the database while storing your posts as part of permalinks etc.
Another use for having real simulated environment is to ensure your ad slots like Google AdSense, DoubleClick etc appear without any issues. Otherwise the ads may not be served properly when you access your development server because of the mismatch of domain you are using and the domain name that you registered with the ad provider.
This article assumes that you already have Apache installed in your Windows PC and is working fine. Let us assume name of your website is www.techpulp.com for this exercise.
First of all, you need to stop the Windows from using Internet domain server to resolve www.techpulp.com. For that you should add a static host entry in C:\WINDOWS\system32\drivers\etc\hosts file. Open the file C:\WINDOWS\system32\drivers\etc\hosts using notepad and add following line at the end of the file.
The above line indicates Windows to assume IP address 127.0.0.1 (loopback) for the domain name www.techpulp.com. That means whenever you attempt to acess www.techpulp.com, your browser contact the Apache server running in the same system instead of contacting the live server in the Internet.
You can test if the new entry added in the hosts file is working or not by doing following:
- Press Start button and Select “Run…”. Otherwise press button “r” while Windows start button is held pressed. This opens the “Run Application” window.
- Type “ping www.techpulp.com” in the text box and press Enter key. This opens windows command line shell window.
- If you see “reply from 127.0.0.1″ being printed periodically for each second, that means you have edited hosts file properly.
- Close the window when you are done.
Because Apache is not yet configure to handle www.techpulp.com, Apache would present the default page.
Now its time to configure Apache with a new virtual host to specify a directory for www.techpulp.com. Add the following lines towards the end of apache configuration file C:\Program Files\Apache Group\Apache2\conf\httpd.conf file. This file may be located in different folder based on the packages like LAMP servers you installed for Apache server. In any case file is “conf\httpd.conf” in the Apache server’s base directory.
NameVirtualHost *:80 <VirtualHost *:80> ServerName www.techpulp.com DocumentRoot "C:/www/www.techpulp.com" CustomLog logs/www.techpulp.com.access.log combined ErrorLog logs/www.techpulp.com.error.log </VirtualHost>
The above configuration enables a new virtual host www.techpulp.com whose web content is located in the directory “C:/www/www.techpulp.com” and has its own log files. Place all your files of www.techpulp.com in the directory “C:/www/www.techpulp.com”.
You need to restart the Apache to get the new configuration to take effect. After restarting Apache, attempt access your website using browsers like Firefox, Internet Explorer, Google Chrome etc.