Thursday, November 27, 2008

Setting up a virtual network with Apache HTTPD

            In this post you will get to know about how to setup a virtual network using apache features. By setting up a virtual network you can organise your various domain and customer domain in a clean and order manner so that you can publish your website in a few seconds to the internet.
            Before we start, make sure you have installed the apache web server and If you have not installed apache web server then read my previous post 'install apache web server'. Once you have a working apache web server installed, we can get straight to setting up virtual network in not more than 5 minutes.

            create a file 'myvirtualnetwork-hosts.conf' inside the directory 'conf'. You can find conf folder under the folder where you have installed apache 

e.g, C:\Program Files\Apache Software Foundation\Apache2.2\conf

            copy the following code into the file 'myvirtualnetwork-hosts.conf'....

------------------------------------------------------  
# Use name-based virtual hosting.
< directory >
  Order Deny,Allow
  Allow from all
< /directory >

NameVirtualHost 127.0.0.1
 
< virtualhost >
  DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
  ServerName localhost
  CustomLog "logs/access.log" common
  ErrorLog "logs/error.log"
< /virtualhost >
 
< virtualhost >
  DocumentRoot "C:\www\domain1.com\site\web"
  ServerName subdomain.domain1.com
  CustomLog logs/subdomain.domain1.com.access.log combined
  ErrorLog logs/subdomain.domain1.com.error.log
< /virtualhost >

< virtualhost >
  DocumentRoot "C:\www\domain2.com\site\web"
  ServerName subdomain.domain2.com
  CustomLog logs/subdomain.domain2.com.access.log combined
  ErrorLog logs/subdomain.domain2.com.error.log
< /virtualhost >
-----------------------------------------------------  

            You can rename the subdomain.domain1/subdomain.domain2 to your wish. Make sure you give name relavent to your actual domain but not the actual domain name itself. If you give the actual domain name then the browser will not load the domain on remote host but from your local host itself.

  The path 'C:\www\domain1.com\site\web' should exist. The path 'C:\www\domain1.com\site\web' can also be 'C:\www\domain1.com'. I have used it to give a logical structure, resembling my domain. 

            Once you are done with the previous step you can add the file name 'myvirtualnetwork-hosts.conf' to 'httpd.conf'. The file 'httpd.conf' will be found in the same directory, where you have created the file 'myvirtualnetwork-hosts.conf'.

            Add the following line.... 

 ------------------------------------------------------  
 Include conf/myvirtualnetwork-hosts.conf 
 ------------------------------------------------------  

            Final step is to add the following code to the file 'hosts'. The file can be found under the path 'C:\WINDOWS\System32\drivers\etc'. You should have access to write to that file.  

            copy the following code at the end of the file.

 ------------------------------------------------------  
 127.0.0.1 localhost 

 127.0.0.1 subdomain.domain1.com # site description 
 127.0.0.1 subdomain.domain2.com # site description 

 ------------------------------------------------------  
Restart apache. Feel the joy......


No comments: