Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

Multiple Site in Apache with Virtual Hosts

As you may know already, I have more than one site. When I use my linux system as the development server, I cannot use two different servers to run the two sites - I use one apache server with virtual hosting. This is a very useful site - it enables you to run multiple sites using just one server.

The term Virtual Host refers to the practice of running more than one web site (such as www.company1.com and www.company2.com) on a single machine. Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end user.

Apache manual

In this post I will show how to create multiple virtual hosts on your server. To keep matters as simple as possible, I will create two virtual hosts - 127.0.0.5 and 127.0.0.10. The first will point to the location /var/www/htdocs/binco/(Bin-Co Site) and the second to /var/www/htdocs/openjs/(OpenJS.Com). The IPs 127.0.0.5 and 127.0.0.10 should be safe to use as they are in the loopback address range(127.*.*.*).

First open the apache configuration file (usually at /etc/httpd/conf/httpd.conf). Next find the line that says '# Virtual hosts'. Look at the lines below this line. In my system they are...

<VirtualHost *>
  DocumentRoot /var/www/html/
  ServerSignature email
 DirectoryIndex index.php index.html index.htm index.shtml 
  LogLevel debug
  HostNameLookups off
</VirtualHost>

This is the default virtual host. To add two new virtual hosts, add these lines below it...

<VirtualHost 127.0.0.5>
 ServerAdmin binny@example.com
 DocumentRoot "/var/www/html/binco"
 DirectoryIndex index.php index.html index.htm index.shtml
 HostNameLookups off
</VirtualHost>

<VirtualHost 127.0.0.10>
 ServerAdmin binny@example.com
 DocumentRoot "/var/www/html/openjs"
 DirectoryIndex index.php index.html index.htm index.shtml
 HostNameLookups off
</VirtualHost>

This will create the virtual hosts - but as the IPs we have given (127.0.0.5 and 127.0.0.10) will be not recognized by apache and it will try to do a host name look up everytime apache is started. This will take a lot of time(and fail). To disable this 'feature' open the file /etc/hosts file and add the following lines to the end.

127.0.0.5  binco.localdomain binco
127.0.0.10  openjs.localdomain openjs

After doing all these steps, you should restart the server with the command service restart httpd. If everything went as planned, you can open Firefox(or your favorite browser), point it to http://127.0.0.5/ and see your site appear there. Of course, I don't have to say that you will have to use your own directory paths and names on your system.

The instructions I have given are for Linux OS running Apache 2 - if you using a different setup, you may have to do a few thing differently. I have done this in a Windows XP system also - only very small differnce between the two.

Filed Under...

0 Comments: