基于同IP不同端口,同端口不同Ip的虚拟主机
[root@localhost html]# service NetworkManager stop[root@localhost html]# chkconfig NetworkManager off[root@localhost html]# chkconfig --list | grep NetworkManager[root@localhost html]# ip addr add 192.168.81.139/24 dev eth0 label eth0:0[root@localhost html]# ifconfigeth0 Link encap:Ethernet HWaddr 00:0C:29:97:D6:65 inet addr:192.168.81.134 Bcast:192.168.81.255 Mask:255.255.255.0eth0:0 Link encap:Ethernet HWaddr 00:0C:29:97:D6:65 inet addr:192.168.81.139 Bcast:0.0.0.0 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
修改配置文件
[root@localhost html]# vim /etc/httpd/conf/httpd.conf NameVirtualHost *:80 不要去除注释ServerName www.a.com DocumentRoot "/var/www/html/a.com" ServerName www.b.net DocumentRoot "/var/www/html/b.net" ServerName www.c.org DocumentRoot "/var/www/html/c.org"
检查
[root@localhost html]# httpd -tSyntax OK[root@localhost html]# service httpd restart
测试
[root@localhost html]# service httpd restart[root@localhost html]# curl 192.168.81.134a.com[root@localhost html]# curl 192.168.81.139b.net[root@localhost html]# curl 192.168.81.134:81c.org
基于FQDN的虚拟主机
修改配置文件
[root@localhost html]# vim /etc/httpd/conf/httpd.conf #DocumentRoot "/var/www/html" 添加注释ServerName www.example.com:80 去除注释NameVirtualHost *:80 去除注释ServerName www.a.com DocumentRoot "/var/www/html/a.com/doc/" ServerName www.b.net DocumentRoot "/var/www/html/b.net/doc"
检查并创建文件路径
[root@localhost html]# httpd -tSyntax OK[root@localhost html]# mkdir -pv /var/www/html/{a.com,b.net}/docmkdir: 已创建目录 "/var/www/html/a.com/doc"mkdir: 已创建目录 "/var/www/html/b.net/doc"[root@localhost html]# vim /var/www/html/a.com/doc/index.html[root@localhost html]# vim /var/www/html/b.net/doc/index.html
测试
【测试】,修改客户端浏览器hosts文件192.168.81.134 www.b.net192.168.81.134 www.a.com OK !