Types of Apache Virtualhost

  1. Name-based Virtual host
  2. Address-based or IP based virtual host and.

Name-based Virtual Host

Name based virtual hosting is used to host multiple virtual sites on a single IP address. 
In order to configure name based virtual hosting, you have to set the IP address on which you are going to receive the Apache requests for all the desired websites.  You can do this by NameVirutalHost directive within the apache configuration i.e. httpd.conf/apache2.conf file.

Apache virtual host Example:

NameVirtualHost *:80
<VirtualHost 192.168.0.108:80>
ServerAdmin webmaster@example1.com
DocumentRoot /var/www/html/example1.com
ServerName www.example1.com </VirtualHost>
ServerAdmin admin@example2.com
<VirtualHost 192.168.0.108:80> DocumentRoot /var/www/html/example2.com
</VirtualHost>
ServerName www.example2.com
You can add as many virtual hosts, as per your requirement. You can check your web configuration files with:
[root@amsterdam ~]#httpd –t
Syntax OK
If the configuration file has some wrong syntax, it will throw an error
[root@115 conf.d]# httpd -t
Syntax error on line 978 of /etc/httpd/conf/httpd.conf:
Invalid command '*', perhaps misspelled or defined by a module not included in the server configuration

IP-based Virtual host

In order to setup IP based virtual hosting, you need more than one IP address configured on your server.  So, the number of vhost apache will depend onnumber of IP address configured on your server.  If your server has 10 IP addresses, you can create 10 IP based virtual hosts.


In the above diagram two websites example1.com and example2.com were assigned different IPs and are using IP-based virtual hosting.
Listen 192.168.0.100:80
<VirtualHost 192.168.10.108:80>
ServerAdmin webmaster@example1.com
DocumentRoot /var/www/html/example1.com
ServerName www.example1.com </VirtualHost>
ServerAdmin admin@example2.com
<VirtualHost 192.168.10.109:80> DocumentRoot /var/www/html/example2.com
</VirtualHost>
ServerName www.example2.com

No comments

Powered by Blogger.