3-Apache服务的虚拟主机
1.基于IP的虚拟主机
环境准备:
eth0:10.1.1.2
eth1:192.168.0.1
需求:
IE:http:/10.1.1.2 --> this is 10.1.1.2 test page
http://192.168.0.1 --> this is 192.168.0.1 test page
步骤:
1.创建相应的数据目录及首页文件
mkdir /web{1,2}/data -p
echo "this is 10.1.1.2 test page" > /web1/data/index.html
echo "this is 192.168.0.1 test page" > /web2/data/index.html
2.发布网站
修改配置文件
[root@web-server conf]# vim httpd.conf
。。。
<VirtualHost 10.1.1.2:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /web1/data/
#ServerName dummy-host.example.com
ErrorLog logs/10.1.1.2-error_log
CustomLog logs/10.1.1.2-access_log common
</VirtualHost>
<VirtualHost 192.168.0.1:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /web2/data/
#ServerName dummy-host.example.com
ErrorLog logs/192.168.0.1-error_log
CustomLog logs/192.168.0.1-access_log common
</VirtualHost>
3.重启服务
4.测试验证
client:10.1.1.3
10.1.1.2成功,此处略过。
测试192.168.0.1
route add -net 192.168.0.0/24 dev eth0 //临时添加一条到192.168.0.0的路由
[root@client ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
安装一个elinks工具 yum -y install elinks
2.基于端口的虚拟主机
需求2:
IE:
http://10.1.1.2:80 this is 80 test page
http://10.1.1.2:10086 this is 10086 test page
步骤:
1.创建相应的数据目录及首页文件
mkdir /data/{80,10086} -p
echo "this is 80 test page" > /data/80/index.html
echo "this is 10086 test page" > /data/10086/index.html
2.发布虚拟主机
修改配置文件
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /data/80
#ServerName dummy-host.example.com
ErrorLog logs/80-error_log
CustomLog logs/80-access_log common
</VirtualHost>
<VirtualHost *:10086>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /data/10086
#ServerName dummy-host.example.com
ErrorLog logs/10086-error_log
CustomLog logs/10086-access_log common
</VirtualHost>
[root@web-server conf]# netstat -nltp|grep 80
tcp 0 0 :::80 :::* LISTEN 2663/httpd
[root@web-server conf]# netstat -nltp|grep 10086
在配置文件必须得添加一个
Listen 10086
3.重启服务
4.测试验证
3.基于域名的虚拟主机
需求3:
http://www.myweb.cc --> this is myweb.cc
http://ftp.test.net --> this is myftp
思路:
1.搭建DNS服务器
2.搭建web服务器
3.客户端指定DNS服务器测试验证
环境:
WEB-server:10.1.1.2
DNS-server:10.1.1.3
client:10.1.1.4
1.
[root@DNS-server ~]# vim /etc/named.conf 两个any,两个no
[root@DNS-server ~]# vim /etc/named.rfc1912.zones
zone "myweb.cc" IN {
type master;
file "myweb.cc.zone";
allow-update { none; };
};
zone "test.net" IN {
type master;
file "test.net.zone";
allow-update { none; };
};
在/var/named/里面添加两个文件myweb.cc.zone test.net.zone
修改完毕重启服务
2.搭建web服务
1)创建相应的数据目录和首页文件
[root@server ~]# echo "this is myweb.cc" > /data/myweb/index.html
[root@server ~]# echo "this is myftp" > /data/myftp/index.html
2)发布两个虚拟主机
打开NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /data/myweb
ServerName www.myweb.cc
ErrorLog logs/80-error_log
CustomLog logs/80-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot /data/myftp
ServerName ftp.test.net
ErrorLog logs/80-error_log
CustomLog logs/80-access_log common
</VirtualHost>
3.重启服务
4.客户端测试验证(如下图)
elinks http://ftp.test.net