ABHIONLINUX
Site useful for linux administration and web hosting

2009/07/30

Virtualhost ( Name based and IP based )

Virtaulhost is used to host multiple domains in the same server.  This can reduce the cost and administration workload for seperate servers for each domain. There are two types of  virtaulhost.
1. Name-based virtulahost
2. IP-based virtualhost

Name based Virtualhost:

Name-based virtaulhost mainly uses one instance of apche to host multiple domains. There is no need to setup multiple IP's in the server. To activate name-based virtualhost, we should use the directive
NameVirtualHost *

This allows apache to accept all the incoming requests.

NameVirtualHost *

ServerName abhionlinux.in
DocumentRoot /home/username/public_html
ServerAdmin admin@abhionlinux.in
ErrorLog /usr/local/apache/logs/www.abhionlinux.in-error_log
CustomLog /usr/local/apache/logs/www.abhionlinux.in-access_log common

ServerName www.example.com
DocumentRoot /home/www/htdocs/example.com
ServerAdmin webmaster@example.com
ErrorLog /usr/local/apache/logs/www.example.com-error_log
CustomLog /usr/local/apache/logs/www.example.com-access_log common

In NameVirtualHost * ,  * is used in the VirtualHost directives. Apache uses the host field in the HTTP header to connect the request to the virtual host. The request is forwarded to the virtual host whose ServerName matches the host name specified in this field.


IP-Based Virtual Hosts:

IP-Based Virtual Hosts requires multiple IP address for the machine. In this case, one instance of apache hosts multiple domains, each domains having different IP. To host multiple IP's, machine must accept request for multiple IP's. For this, IP aliasing should be enabled in the kernal. Once this is setup, you can use the commands ifconfig, route to add new IP's to machine.

Eg: /sbin/ifconfig eth0:0 192.168.1.20
    /sbin/ifconfig eth0:1 192.168.1.21

Once IP aliasing has been setup, you can use the seperate virtualhost entry for each domain.

You can use Multiple Instances of Apache  for IP-based virtaulhost. In this method use the Listen directive to specify the  IP handled by the respective apache instance.

For eg:
Listen 192.168.1.10:80
Listen 192.168.1.20:80
Listen 192.168.1.21:80

No comments:

Post a Comment