Apache Virtual host is divided into three categories: IP-based, port-based, domain-based. The virtual host and physical host cannot be used at the same time, and the following line should be commented out to open the virtual host:
DocumentRoot "/var/www/html"
One, IP-based
1. Assume that the server has an IP address of 192.168.1.10, use Ifconfig to bind 3 IPs on the same network interface eth0:
[Email protected] root]# ifconfig eth0:1 192.168.1.11[[email protected] root]# ifconfig eth0:2 192.168.1.12[[email prote CTED] root]# ifconfig eth0:3 192.168.1.13
2. Modify the Hosts file to add three domain names to each:
192.168.1.11 www.test1.com
192.168.1.12 www.test2.com
192.168.1.13 www.test3.com
3. Set up the root directory of Web pages, such as Test1, Test2, test3 folders in the/www directory, which store 1.html, 2.html, 3.html respectively
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html
4. Include the additional configuration file httpd-vhosts.conf in httpd.conf, and then write the following configuration in httpd-vhosts.conf:
<VirtualHost 192.168.1.11:80> servername www.test1.com documentroot /www/test1/< directory "/www/test1" > options indexes followsymlinks allowoverride none &nbs P;order allow,deny allow from all </Directory></VirtualHost>< virtualhost 192.168.1.12:80> servername www.test1.com documentroot /www/test2/<Directory "/www/test2" > options indexes followsymlinks allowoverride none &NBSP;ORDER&N Bsp;allow,deny allow from all </directory></virtualhost><virtualhost 192.168.1.13:80> servername www.test1.com documentroot /www/test3/<Directory "/www/ Test3 "> options indexes followsymlinks allowoverride none order allow,de NY allow from all </directory></vIrtualhost>
5. You are done, test each virtual host, Access www.test1.com, www.test2.com, www.test3.com, respectively
Second, based on the host name
1. Set the domain name map to the same IP, modify the hosts:
192.168.1.10 www.test1.com
192.168.1.10 www.test2.com
192.168.1.10 www.test3.com
2. As above, establish the root directory of the Web page hosting the virtual host
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html
3. Include the additional configuration file httpd-vhosts.conf in httpd.conf, and then write the following configuration in httpd-vhosts.conf:
in order to use a domain-based virtual host, you must specify the server IP address (and possibly the port) to allow the host to accept the request. Can be configured with the namevirtualhost directive. If all IP addresses on the server are used, you can use * as Namevirtualhost parameters. specifying an IP address in the NAMEVIRTUALHOST directive does not cause the server to automatically listen for that IP address . The IP address set here must correspond to a network interface on the server.
The next step is for each virtual host Setup <VirtualHost> configuration block that you set up for,<virtualhost> the parameters of the Namevirtualhost directive are the same. In each <VirtualHost> definition block, there will be at least one ServerName instruction to specify which host and one documentroot instruction to indicate where the contents of this host exist in the filesystem.
If you add a virtual host to an existing Web server, you must also build a <VirtualHost> definition block for existing hosts. The contents of servername and DocumentRoot should be consistent with the global, and should be placed at the front of the configuration file, playing the role of the default host.
namevirtualhost *:80<virtualhost *:80> servername * DocumentRoot /www/ </VirtualHost><VirtualHost *:80> servername www.test1.com documentroot /www/ test1/<Directory "/www/test1" > Options indexes followsymlinks allowoverride none Orde R allow,deny Allow from all </directory></virtualhost> <virtualhost *:80> servername www.test2.com documentroot /www/test2/<Directory "/www/test2" > Options indexes followsymlinks Allowoverride none Order allow,deny Allow from all </Directory></VirtualHost><VirtualHost *:80> servername www.test3.com DocumentRoot /www/test3/<Directory "/www/test3" > Options indexes followsymlinks allowoverride none Order allow,deny Allow from all </directory></VirtualHost>
4. You are done, test each virtual host, Access www.test1.com, www.test2.com, www.test3.com, respectively
Third, Port-based
1. Modify the configuration file
The original
Listen 80
Switch
Listen 80
Listen 8080
2. Change the virtual host settings:
<virtualhost 192.168.1.10:80> Documentroot/var/www/test1/servername Www.test1.com</VirtualHost><Vi Rtualhost 192.168.1.10:8080> documentroot/var/www/test2 ServerName www.test2.com</virtualhost>
650) this.width=650; "alt=" Copy Code "src=" Http://common.cnblogs.com/images/copycode.gif "/>
HTTP Service (iv)--apache virtual host Configuration