首页 > 代码库 > WWW服务器虚拟主机配置
WWW服务器虚拟主机配置
准备工作
先搭建一台DNS,创建3个域名
[root@localhost ~]# yum install bind*
修改named.conf主配置文件
listen-on port 53 { any; };
allow-query { any; };
zone "ccav.cn" IN {
type master;
file "ccav.cn.zone";
};
zone "ccav1.cn" IN {
type master;
file "ccav1.cn.zone";
};
zone "ccav2.cn" IN {
type master;
file "ccav2.cn.zone";
};
修改zone文件内容
ccav.cn.zone文件内容
$TTL 1D
@ IN SOA server.ccav.cn. root.ccav.cn. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
@ NS server.ccav.cn.
server.ccav.cn. A 172.19.34.185
www.ccav.cn. A 172.19.34.185
ccav1.cn.zone文件内容
$TTL 1D
@ IN SOA server.ccav1.cn. root.ccav1.cn. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS server.ccav1.cn.
server.ccav1.cn. A 172.19.34.185
www.ccav1.cn. A 172.19.34.185
ccav2.cn.zone文件内容
$TTL 1D
@ IN SOA server.ccav2.cn. root.ccav2.cn. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS server.ccav2.cn.
server.ccav2.cn. A 172.19.34.185
www.ccav2.cn. A 172.19.34.185
更改zone文件所属组
[root@localhost named]# chown named:named ccav.cn.zone ccav1.cn.zone ccav2.cn.zone
关闭防火墙
service iptables stop
关闭selinux
getenforce 0
重启named服务
[root@localhost named]# service named restart
Stopping named: . [ OK ]
Starting named: [ OK ]
将本机IP设为DNS
[root@localhost named]# cat /etc/resolv.conf
nameserver 172.19.34.185
解析域名
[root@localhost named]# host www.ccav.cn
www.ccav.cn has address 172.19.34.185
[root@localhost named]# host www.ccav1.cn
www.ccav1.cn has address 172.19.34.185
[root@localhost named]# host www.ccav2.cn
www.ccav2.cn has address 172.19.34.185
DNS域名搭建完成
-------------------------------------------------------------------------------------------
http服务安装
[root@localhost named]#yum install httpd* -y
-------------------------------------------------------------------------------------------
基于主机名的虚拟主机
修改httpd.conf主配置文件
[root@localhost named]# vi /etc/httpd/conf/httpd.conf
NameVirtualHost *:80 去掉前面的#
<VirtualHost *:80>
DocumentRoot /var/http/www 指定网页路径
ServerName www.ccav.cn 服务器名称
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /ccav1
ServerName www.ccav1.cn
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /ccav2
ServerName www.ccav2.cn
</VirtualHost>
新建ccav1,ccav2,默认/var/http/www是存在的,所以不用添加
[root@localhost named]#mkdir /ccav1 /ccav2
加一点内容到index.html里面去
[root@localhost named]# echo "This is www.ccav.cn‘s home" > /var/www/html/index.html
[root@localhost named]# echo "This is www.ccav1.cn‘s home" > /ccav1/index.html
[root@localhost named]# echo "This is www.ccav2.cn‘s home" > /ccav2/index.html
改变上下文
[root@localhost named]#chcon -R --reference=/var/www/html /ccav1 /ccav2
测试
打开浏览器,输入www.ccav.cn;www.ccav1.cn;www.ccav.cn查看是否可以看到刚才办输入的内容
-------------------------------------------------------------------------------------------
基于IP地址的虚拟主机
首先在机器上创建2个I虚拟IP地址
[root@localhost named]# ip addr add 172.19.34.186/24 dev eth0 label eth0:1
[root@localhost named]# ip addr add 172.19.34.187/24 dev eth0 label eth0:2
修改网卡文件
[root@localhost named]#cp ifcfg-eth0 ifcfg-eth0:1
DEVICE=eth0:1 修改名字
HWADDR=00:0C:29:08:3A:56
TYPE=Ethernet
UUID=5d789fd9-45a5-4e87-b152-6cbd1a6f4d64
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=STATIC
IPADDR=172.19.34.186 修改IP址址
NETMASK=255.255.255.0
GATEWAY=172.19.34.254
DNS1=172.19.34.185
cp ifcfg-eth0 ifcfg-eth0:2
DEVICE=eth0:2 修改名字
HWADDR=00:0C:29:08:3A:56
TYPE=Ethernet
UUID=5d789fd9-45a5-4e87-b152-6cbd1a6f4d64
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=STATIC
IPADDR=172.19.34.187 修改IP地址
NETMASK=255.255.255.0
GATEWAY=172.19.34.254
DNS1=172.19.34.185
修改完并重启
[root@localhost named]#service network restart
修改DNS zone文件A记录
ccav1.cn.zone
$TTL 1D
@ IN SOA server.ccav1.cn. root.ccav1.cn. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS server.ccav1.cn.
server.ccav1.cn. A 172.19.34.186
www.ccav1.cn. A 172.19.34.186
ccav2.cn.zone
$TTL 1D
@ IN SOA server.ccav2.cn. root.ccav2.cn. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS server.ccav2.cn.
server.ccav2.cn. A 172.19.34.187
www.ccav2.cn. A 172.19.34.187
修改httpd主要配置文件
vi /etc/httpd/conf/httpd.conf
#NameVirtualHost *:80#必须注释掉
将*换成相应的IP地址
<VirtualHost 172.19.34.185:80>
DocumentRoot /var/www/html
ServerName www.ccav.cn
</VirtualHost>
<VirtualHost 172.19.34.186:80>
DocumentRoot /ccav1
ServerName www.ccav1.cn
</VirtualHost>
<VirtualHost 172.19.34.187:80>
DocumentRoot /ccav2
ServerName www.ccav2.cn
</VirtualHost>
重启httpd
[root@localhost named]#service httpd restart
测试效果
在IE打开IP,或网页均可访问
本文出自 “zhufaqing” 博客,请务必保留此出处http://19851225.blog.51cto.com/1088033/1583217
WWW服务器虚拟主机配置