Setting up Virtual Hosting in Apache on Windows
Setting up Virtual Hosting is easier than you think. You can ignore this step if you want to develop your applications using the http://localhost notation. But in a real case scenario you would like to setup a development environment on your machine to simulate real world conditions.
This tutorial will guide you through the step by step process of how to setup virtual hosting in Apache on Windows. For this example I will be configuring http://dev.sunilb.com as the domain for development purposes.
Step 1: Edit the hosts fileEdit the host file available in the Windows Root folder\system32\drivers\etc (C:\Windows or C:\Winnt - or wherever you have installed windows) .
E.g:
C:\WINDOWS\system32\drivers\etc
Locate the hosts file and create a backup of that file.
Open that file. You will see some entries like:
127.0.0.1 localhost
Type in the following line:
127.0.0.1 dev.sunilb.com
and, save the file.
Step 2: Update Apache Configuration File
The next step involves configuring Apache to know that it needs to accept requests for http://dev.sunilb.com
Open the Apache configuration file (httpd.conf). It will be available under the conf folder of your Apache Installation (ideally the location is C:\Apache Group\Apache2\conf).
Create a backup of that file.
Paste the following code towards the end of the file and restart the Apache Server (Start > Programs > Apache HTTP Server 2.2.6 > Control Apache Server > Restart)
<VirtualHost *:80>
ServerAdmin info@localhost.com
DocumentRoot /sunilbhatia/website/sunilb.com
ServerName dev.sunilb.com
</VirtualHost>
Explanation:
*:80 instructs Apache that it should listen to all requests coming to port 80 on any IP of that machin. So the * is indeed the IP.
ServerAdmin is the email address that will get printed when a server encounters an error. The users get a message stating that Server has encountered an error and you should contact this email address
DocumentRoot tells Apache the root folder of your website
ServerName tells Apache the domain for which Apache should listen to requests
What happens when you type a domain name in your browser
When you type a domain name in the browser, the Operating System first tries to resolve the domain against the IP list mentioned in the hosts file. If it finds the IP information in that file, then it tries to connect to that IP.
If the domain information is not available in the hosts file then the domain is searched for using DNS services.
Because you want your domain to work on your local machine you need to follow the above steps.
Step 3: Validate your changes Create a file index.php in the DocumentRoot folder. Open your browser and type in the domain that you configured and index.php.
e.g:
http://dev.sunilb.com/index.php
Filed under: Apache Tutorials