1. Based on IP
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:
[root@localhost root]# ifconfig eth0:1 192.168.1.112. Modify the hosts file and add three domain names corresponding to one of them:
192.168.1.11 www.test1.com3. Establish the root directory where the virtual host stores web pages. For example, create test1, test2, and test3 folders in the /www directory, which store 1.html, 2.html, and 3.html respectively
/www/test1/1.html
4. Include the additional configuration file httpd-vhosts.conf in httpd.conf, and then write the following configuration in httpd-vhosts.conf:
5. You’re done, test each virtual host and visit www.test1.com, www.test2.com, www.test3.com respectively
2. Based on host name
1. Set the domain name to map to the same IP and modify hosts:
192.168.1.10 www.test1.com2. As above, create the root directory of the virtual host to store the web page
/www/test1/1.html3. 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 domain-based virtual hosting, the server IP address (and possibly port) must be specified for the host to accept requests. It can be configured using the NameVirtualHost directive. If all IP addresses on the server are used, you can use * as the parameter for NameVirtualHost. Specifying an IP address in the NameVirtualHost directive does not cause the server to automatically listen on that IP address. The IP address set here must correspond to a network interface on the server.
The next step is to set the
If you add a virtual host to an existing web server, you must also create a
ServerName *
DocumentRoot /www/
ServerName www.test1.com
DocumentRoot /www/test1/
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
ServerName www.test2.com
DocumentRoot /www/test2/
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
ServerName www.test3.com
DocumentRoot /www/test3/
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
4. 大功告成,测试下每个虚拟主机,分别访问www.test1.com、www.test2.com、www.test3.com
三、基于端口
1. 修改配置文件将原来的
Listen 80
改为
Listen 80
Listen 8080
2. 更改虚拟主机设置: