A virtual host can coexist with multiple sites. When the site has little traffic, multiple sites can run on one physical host to save resources (IP addresses and physical hosts.
1. IP-based VM:
To implement a VM, you must first comment out the configuration of the central host, that is, disable the central host. You only need to comment out the line corresponding to the DocumentRoot command, and then define the VM.
IP-based virtual hosts require that one physical machine have multiple IP addresses, and each virtual host listens to different IP addresses, you can access the service either by using the name of the VM or by using an IP address in the browser interface. For example, for two virtual hosts www.a.org and www. B .net, our physical host has two IP addresses 192.168.1.20.and 192.168.1.134, www.a.org runs on the IP address of 192.168.1.20., and www. B .net runs on 192.168.1.134.
First, configure the second Nic of the Host:
# Ifconfig eth0: 0 192.168.1.134/24
Create the directory used by the VM:
# Mkdir/var/www/{Hosta, hostb}
Edit the default homepage used by the two VM instances, including www.a.org and www. B .net.
Add the following content to the httpd configuration file:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/45/53/wKioL1PnQaPg-F-1AADYnN1AQfo186.jpg "Title =" 2014-08-10 17_50_14-root @ sueking _~ -Xshell 4.jpg" alt = "wKioL1PnQaPg-F-1AADYnN1AQfo186.jpg"/>
After the configuration is complete, check the syntax and restart the service: # httpd-T # service httpd restart
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/45/52/wKiom1PnQEKy9t_yAAFYUlrUK-s198.jpg "Title =" 2014-08-10 17_48_42-http1__192.168.1.133_.jpg "alt =" wKiom1PnQEKy9t_yAAFYUlrUK-s198.jpg "/>
2. Port-based VM:
For Port-based virtual host implementation, set www.a.org to use port 80 of 192.168.1.20.and www. B .net to use port 8080 of 192.168.1.20.. the configuration file is as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/45/53/wKiom1PnRjeAmYvcAADhgx97Zpo141.jpg "Title =" 2014-08-10 18_14_26-root @ sueking _~ -Xshell 4.jpg" alt = "wkiom1pnrjeamyvcaadhgx97zpo141.jpg"/>
Note: because a new port is added to a VM based on different ports, you need to add the specified listening port:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/45/53/wKiom1PnRwzxFT9-AAA-Itq-AHI719.jpg "Title =" 2014-08-10 18_17_55-root @ sueking _~ -Xshell 4.jpg" alt = "wKiom1PnRwzxFT9-AAA-Itq-AHI719.jpg"/>
After you change the listening port, You need to restart the service to take effect. # service httpd restart.
In addition, if the web browser is used to test the Web Hosting www. B .net, you must enter http: // 192.168.1.20.: 8080 in the browser. You cannot simply enter 192.168.1.20.: 8080, otherwise, the webpage cannot be found. The normal test results are as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/45/53/wKioL1PnSN6APrwvAAFGOi6bZjw400.jpg "Title =" 2014-08-10 18_20_59-http%_192.168.1.133_.jpg "alt =" wkiol1pnsn6aprwvaafgoi6bz%400.jpg "/>
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/45/53/wKioL1PnSP_wuWmjAAFZcuICp2Q223.jpg "Title =" 2014-08-10 18_21_35-http1__192.168.1.133_8080_.jpg "alt =" wkiol1pnsp_wuwmjaafzcuicp2q223.jpg "/>
Iii. Virtual Host Based on Host Name
To implement a VM Based on the host name, the host name must be different. In addition to commenting out the line corresponding to the DocumentRoot command of the central host in the configuration file, you must also enable the line corresponding to the namevirtualhost command, by default, the name-based virtual host listens on port 80.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/45/53/wKioL1PnSbuQnlxlAAAtHX1nR_c052.jpg "Title =" 2014-08-10 18_24_35-root @ sueking _~ -Xshell 4.jpg" alt = "wkiol1pnsbuqnlxlaaathx1nr_c052.jpg"/>
Set our two virtual hosts www.a.org and www. B .net to listen on port 80 of 192.168.1.20.. the configuration is as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/45/53/wKiom1PnSbCxtv2ZAADCOM8-ZVk636.jpg "Title =" 2014-08-10 18_27_57-root @ sueking _~ -Xshell 4.jpg" alt = "wKiom1PnSbCxtv2ZAADCOM8-ZVk636.jpg"/>
When you configure a name-based Vm, If you specify all the addresses of the listener after the namevirtualhost command, you must ensure consistency in the definition of the VM. If you specify an IP address, in this case, the virtual machine also uses IP addresses instead of all IP addresses. Otherwise, an error will be reported, indicating that the two virtual hosts use the same IP addresses and ports and overlap, the specific error message is as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/45/53/wKiom1PnT5yDbGwIAAE5PjPbNJ8686.jpg "Title =" 2014-08-10 18_44_58-root @ sueking _~ -Xshell 4.jpg" alt = "wkiom1pnt5ydbgwiaae5pjpbnj8686.jpg"/>
We recommend that you use the specified IP address to explicitly indicate which virtual hosts are named.
To test in a Windows operating system, you must add the corresponding resolution entries in the Windows/system32/Drivers/etc/hosts file in the system directory, to parse the virtual host name. The test result is as follows:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/45/53/wKiom1PnT9yBBiofAAFj1x4QaOs433.jpg "Title =" 2014-08-10 18_55_18-http1__www.a.org_.jpg "alt =" wkiom1pnt9ybbiofaafj1x4qaos433.jpg "/>
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/45/53/wKioL1PnUQCQigGHAAFyRsKpBD0074.jpg "Title =" 2014-08-10 18_55_30-http%_www. B .net_.jpg "alt =" wkiol1pnuqcqigghaafyrskpbd0074.jpg "/>