Thursday, June 16, 2011

Setting Up A Virtual Host in Apache

Yesturday I installed an web application that required a valid domain name, something like http://domain_name.com (http://localhost/my_site was not allowed).
The question was how can we provide a valid domain name to this application, which is installed on the local machine, assuming that we don't have bought an domain name?
The answer for this question is that we set up a virtual host which points to 127.0.0.1 which is the localhost.
If we presume that we run Apache server on Windows machine, here are the short steps of how to do this virtual host:

1. Configuring Apache
Locate and edit Apache httpd.conf file by adding the next lines at the end of the file (e.g. apache_home_folder/conf/httpd.conf):

NameVirtualHost 127.0.0.1
<virtualhost 1="">
  DocumentRoot "C:\My Sites\Site1"
  ServerName domain_name.com
</virtualhost>

Note 1: If the above path for DocumentRoot does not have any spaces in it, do not quote the path.

Note 2: Starting with Apache 2.2, the above modifications should be made in httpd-vhosts.conf file, which will be located in the extra folder (e.g. apache_home_folder/conf/extra/httpd-vhosts.conf). Also make sure that the line:

Include conf/extra/httpd-vhosts.conf

is uncommented in httpd.conf file.

It is also needed to add the next lines to httpd-vhosts.conf file:

<directory my="">
  Order Deny,Allow
  Allow from all
</directory>

then add the NameVirtualHost 127.0.0.1 ... lines.

2. Resolving the DNS issue
Locate the file

C:\WINNT\system32\drivers\etc\hosts

or

C:\Windows\system32\drivers\etc\hosts

At the end of this file add the following line:

127.0.0.1 domain_nalme.com

and save the file.

If you cannot save the file, run the editor with administrator permissions (right click on the editor and select Run as Administrator option) and then open the file in the editor.

3. Restart Apache server
All you have to do now is to restart the Apache server and test the virtual host in your browser: http://domain_name.com

Resoueces:
http://apptools.com/phptools/virtualhost.php

No comments:

Post a Comment