​Linux网络服务之HTTP(2)
Linux网络服务之HTTP(2)
实验要求:
1、部署基于域名的虚拟主机,当用户访问www.baidu.com的时候访问baidu网站,当用户访问www.sina.com的时候访问sina网站,baidu和sina的ip地址一样,确保前述www.zhy.com访问还能访问
2、部署基于IP的虚拟主机
www.zhy.com192.168.1.253
www.baidu.com192.168.1.12
www.google.com192.168.1.11
实验步骤:
(1)基于域名的虚拟主机
1、安装软件包
略……
(1)基于域名的虚拟主机
配置DNS解析
[root@server ~]# cat /var/named/chroot/etc/named.rfc1912.zones
……
zone "com" IN {
type master;
file "zhy.com.zone";
……
[root@server ~]# cat /var/named/chroot/var/named/zhy.com.zone
$TTL 86400
@ IN SOA zhy.com. root.zhy.com. (
2014050201 ; Serial
28800 ; Refresh
14400 ; Retry
3600000 ; Expire
86400 ) ; Minimum
IN NS com.
dns1.zhy IN A 192.168.1.253
www.zhy IN A 192.168.1.253
bbs.zhy IN A 192.168.1.251
www.baidu IN A 192.168.1.253
www.sina IN A 192.168.1.253
修改配置文件
[root@server ~]# cat /etc/httpd/conf.d/virt.conf //虚拟主机配置文件
NameVirtualHost 192.168.1.253
<VirtualHost 192.168.1.253>
DocumentRoot /var/www/html/
ServerName www.zhy.com
ErrorLog logs/zhy.com-error_log
CustomLog logs/zhy.com-access_log common
</VirtualHost>
<VirtualHost 192.168.1.253>
DocumentRoot /web/sina/
ServerName www.sina.com
ErrorLog logs/sina.com-error_log
CustomLog logs/sina.com-access_log common
</VirtualHost>
<VirtualHost 192.168.1.253>
DocumentRoot /web/baidu/
ServerName www.baidu.com
ErrorLog logs/baidu.com-error_log
CustomLog logs/baidu.com-access_log common
</VirtualHost>
[root@server ~]# cat /web/sina/index.html //创建测试站点
This is sina.com!!!
[root@server ~]# cat /web/baidu/index.html
This is baidu.com!!!
(2)基于IP地址的虚拟主机
配置DNS实现解析
[root@server ~]# cat /var/named/chroot/var/named/zhy.com.zone
$TTL 86400
@ IN SOA zhy.com. root.zhy.com. (
2014050201 ; Serial
28800 ; Refresh
14400 ; Retry
3600000 ; Expire
86400 ) ; Minimum
IN NS com.
dns1.zhyIN A 192.168.1.253
www.zhyIN A 192.168.1.253
bbs.zhy IN A 192.168.1.251
www.baidu IN A 192.168.1.12
www.sina IN A 192.168.1.11
1、配置虚拟网卡,实现多个IP地址共存
[root@server ~]# cd /etc/sysconfig/network-scripts/
[root@server network-scripts]# cp -p ifcfg-eth0 ifcfg-eth0:0
[root@server network-scripts]# cp -p ifcfg-eth0 ifcfg-eth0:1
[root@server network-scripts]# cat ifcfg-eth0:0
# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
DEVICE=eth0:0
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.11
NETMASK=255.255.255.0
[root@server network-scripts]# cat ifcfg-eth0:1
# Intel Corporation 82545EM Gigabit Ethernet Controller (Copper)
DEVICE=eth0:1
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.1.12
NETMASK=255.255.255.0
2、修改虚拟主机配置文件
[root@server ~]# cat /etc/httpd/conf.d/virt.conf
<VirtualHost 192.168.1.253:80>
DocumentRoot /var/www/html/
ServerName www.zhy.com
ErrorLog logs/zhy.com-error_log
CustomLog logs/zhy.com-access_log common
</VirtualHost>
<VirtualHost 192.168.1.11:80>
DocumentRoot /web/sina/
ServerName www.sina.com
ErrorLog logs/sina.com-error_log
CustomLog logs/sina.com-access_log common
</VirtualHost>
<VirtualHost 192.168.1.12:80>
DocumentRoot /web/baidu/
ServerName www.baidu.com
ErrorLog logs/baidu.com-error_log
CustomLog logs/baidu.com-access_log common
</VirtualHost>
启动服务,设置服务开机自启
[root@server ~]#service httpd restart
[root@server named]# service network restart
正在关闭接口 eth0: [确定]
关闭环回接口: [确定]
弹出环回接口: [确定]
弹出界面 eth0: [确定]
[root@server named]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:63:58:FE
inet addr:192.168.1.253 Bcast:192.168.1.255 Mask:255.255.255.0
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:63:58:FE
inet addr:192.168.1.11 Bcast:192.168.1.255 Mask:255.255.255.0
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:63:58:FE
inet addr:192.168.1.12 Bcast:192.168.1.255 Mask:255.255.255.0
[root@server ~]#chkconfig httpd on
[root@server ~]#chkconfig network on
[root@server ~]#chkconfig named on
测试
注意:测试前需要清理一下浏览器的缓存,以防缓存导致实验测试失败!!
本文出自 “烟头网管” 博客,请务必保留此出处http://8838848.blog.51cto.com/8828848/1405763