14、 三种虚拟主机
虚拟主机 是指在一台WWW服务器上 为多个单独的域名提供Web服务
Apache有3种方式支持 虚拟主机:
一、基于IP的虚拟主机: 一台服务器上配置多个IP地址,每个Ip地址都有特定的目录和配置。适合Ip地址充足的情况
1、[root@localhost root]# vi /etc/named.conf
zone "gr.org" IN { type master; file "gr.org.db"; allow-update { none; }; }; zone "170.168.192.in-addr.arpa" IN { type master; file "170.168.192.db"; };
2、正向区域数据库文件
[root@localhost root]# vi /var/named/gr.org.db
$TTL 86400 @ IN SOA ns.gr.org. root.gr.org.( 20140308 ;serial 8H ;refresh 4M ;retry 1W ;expiry 1D ) ;minimum @ IN NS ns.gr.org. ns IN A 192.168.170.1 host1 IN A 192.168.170.1 host2 IN A 192.168.170.2 host3 IN A 192.168.170.3
反向区域数据库文件
[root@localhost root]# vi /var/named/170.168.192.db
$TTL 86400 @ IN SOA ns.gr.org. root.gr.org.( 20150826 8H 4M 1W 1D) IN NS ns.gr.org. 1 IN PTR ns.gr.org. 1 IN PTR host1.gr.org. 2 IN PTR host2.gr.org. 3 IN PTR host3.gr.org.3、设置DNS客户端
[root@localhost root]# cat > /etc/resolv.conf
nameserver 192.168.170.4service named start (启动服务)
如果已经启动,就 rndc reload (重启服务)
4、配置虚拟主机
[root@localhost conf.d]# vi /etc/httpd/conf.d/host_ip.conf
<VirtualHost 192.168.170.1:80> DocumentRoot /var/www/host1 ServerName host1.gr.org </VirtualHost> <VirtualHost 192.168.170.2:80> DocumentRoot /var/www/host2 ServerName host2.gr.org </VirtualHost> <VirtualHost 192.168.170.3:80> DocumentRoot /var/www/host3 ServerName host3.gr.org </VirtualHost>5、添加子接口的IP地址
[root@localhost conf.d]# ifconfig eth0:1 192.168.170.1
[root@localhost conf.d]# ifconfig eth0:2 192.168.170.2
[root@localhost conf.d]# ifconfig eth0:3 192.168.170.3
6、创建虚拟主机目录和测试主页
mkdir /var/www/host{1,2,3}
cd /var/www
echo “this host1” > host1/index.html
echo “this host2” > host2/index.html
echo “this host3” > host3/index.html
7、重启服务
service httpd restart
结果如下:
二、基于端口的虚拟主机:
1、在/etc/httpd/conf.d目录下 创建host_port.conf文件,添加以下内容
Listen 192.168.170.1:8000 Listen 192.168.170.1:8888 <VirtualHost 192.168.170.1:8000> DocumentRoot /var/www/host4 ServerName host1.gr.org </VirtualHost> <VirtualHost 192.168.170.2:8888> DocumentRoot /var/www/host5 ServerName host1.gr.org </VirtualHost>
2、创建虚拟主机的工作目录和测试主页
mkdir /var/www/host{4,5} echo “this host1(8000)” > /var/www/host4/index.html echo “this host1(8888)” > /var/www/host5/index.html3、重启服务
service httpd restart
测试
三、基于名称的虚拟主机 (大多数用这种)
多个域名访问,都对应同一个IP地址
1、在 正向区域数据库文件 添加下面内容
music IN A 192.168.170.1 movie IN A 192.168.170.1
2、在 反向区域数据库文件 添加下面内容
1 IN PTR music.gr.org. 1 IN PTR movie.gr.org.
3、在/etc/httpd/conf.d目录下 创建host_domain.conf文件,添加以下内容
<VirtualHost music.gr.org:80> DocumentRoot /var/www/music ServerName music.gr.org </VirtualHost> <VirtualHost movie.gr.org:80> DocumentRoot /var/www/movie ServerName movie.gr.org </VirtualHost>
4、创建虚拟主机的工作目录和测试主页
cd /var/www mkdir music movie echo “this music” > music/index.html echo “this movie” > movie/index.html
5、重启服务
rndc reload 或者 service named restart
service httpd start
6、测试