What is Virtual Host?

Answer

An Apache web server can host multiple websites on the SAME server. You do not need separate server machine and apache software for each website. This can achieved using the concept of Virtual Host.

Any domain that you want to host on your web server will have a separate entry in apache configuration file.

 

There are two types of virtual hosts supported by Apache –

  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 websites 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.

 

NameVirtualHost *:80
 
<VirtualHost 192.168.0.108:80>
 
ServerAdmin webmaster@example1.com
 
DocumentRoot /var/www/html/example1.com     
 
ServerName www.example1.com
 
</VirtualHost>
 
<VirtualHost 192.168.0.108:80>
 
ServerAdmin admin@example2.com
 
DocumentRoot /var/www/html/example2.com
 
ServerName www.example2.com
 
</VirtualHost>

You can add as many virtual hosts, as per your requirement. You can check your web configuration files with:

1
2
[root@amsterdam ~]#httpd –t
Syntax OK

If the configuration file has some wrong syntax, it will throw an error

1
2
3
4
5
[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 virtual host 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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>
 
<VirtualHost 192.168.10.109:80>
 
ServerAdmin admin@example2.com
 
DocumentRoot /var/www/html/example2.com
 
ServerName www.example2.com
 
</VirtualHost>

All apache Questions

Ask your interview questions on apache

Write Your comment or Questions if you want the answers on apache from apache Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---