Hosting multiple websites from single apache server

One common question that often comes with developer is; how to hosting multiple websites from single apache server that too when the ip address of those sites are going to be same forever.

Few days back I too had this puzzle to be solved. Fortunately apache victual host helped me to archive this.

Virtual Hosts Diagram

Virtual Hosts Diagram

The term Virtual Host refers to the practice of running more than one website from same machine. And apache supports more than one type of Virtual Host techniques namely Name-Based Virtual Host, IP-Based Virtual Host and etc.

Website Name IP and port

 

TestSite1.com 127.0.0.1:80

myTestSite2.com 127.0.0.1:80

myTestsite3.com 127.0.0.1:80

I have used Name-based virtual hosting to run multiple website from same machine. Meaning I will use a unique name to represent the sites which will go to same machine and returns site that I am looking for possibly on same port .

Step 1 Enabling host for windows

By somehow you need tell windows that it is not only localhost are associated with this machine but also myTestSite1.com , myTestSite2.com.

For this open your windows host file at C:WindowsSystem32Driversetchosts. Depending on your operating system your host file location will different from what I am saying. Then add your host details to bottom of the file.127.0.0.1 localhost 127.0.0.1. myTestSite1.com 127.0.0.1 myTestSite2.com

Step 2 Tell Apache to be ever smarterTell apache to carefully listen to incoming host name, depending on request cleverly change the DocumentRoot path and return the appropriate site. For this you need to add VirtualHost blocks to httpd . conf file on apache. The next step is to create a VirtualHost block for each different host (sites) that you would like to serve.


NameVirtualHost *:80


<VirtualHost *:80>
DocumentRoot htdocs
ServerName localhost
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>


<VirtualHost *:80>
ServerName myTestSite1.com
ServerPath /stg
DocumentRoot htdocs/stg

<Directory />
Order Allow,Deny
Allow from All
</Directory>
</virtualHost>


<VirtualHost *:80>
ServerName myTestSite2.com
ServerPath /shakthydoss
DocumentRoot htdocs/shakthydoss

<Directory />
Order Allow,Deny
Allow from All
</Directory>
</virtualHost>

 

Step 3 Start Apache and test it.

Your successfully configured Apache to run multiple website from same machine.

Before testing you can also check and verify that whatever configuration you have added is understandable by apache.

On apache bin directory type.

httpd  -t

this will return any message if you made any error in the  configuration, if everything is fine it will return get Syntax Ok message.

Now start your apache

httpd –k restart

 

On your address bar hit

http://localhost
http://myTestSite1.com
http://myTestSite2.com


1 Comment

  1. gameser wrote
    at 3:18 PM - 1st October 2012 Permalink

    Excellent weblog here! Additionally it is very clean and neet

Post a Comment

Your email is never published nor shared. Required fields are marked *