实验:基于PORT的多虚拟主机
1.创建3个不同的网站文件根目录
mkdir /data/site{1,2,3} echo 'www.a.com' > /data/site1/index.html echo 'www.b.com' > /data/site2/index.html echo 'www.c.com' > /data/site3/index.html ![]
2.修改或在conf.d目录下创建一个.conf结尾的文件,并添加如下参数
vim /etc/httpd/conf.d/vhosts.conf
listen 8001
listen 8002
listen 8003
<virtualhost *:8001>
documentroot /data/site1
<directory /data/site1>
require all granted
</directory>
</virtualhost>
<virtualhost *:8002>
<directory /data/site2>
require all granted
</directory>
documentroot /data/site2
</virtualhost>
<virtualhost *:8003>
<directory /data/site3>
require all granted
</directory>
documentroot /data/site3
</virtualhost>
实验:基于ip的多虚拟主机
1.给单个网卡绑定多个ip地址
ip a a 192.168.36.8/24 dev eth0 ip a a 192.168.36.9/24 dev eth0
2.修改参数
vim /etc/httpd/conf.d/vhosts.conf
<virtualhost 192.168.36.7:80>
documentroot /data/site1
<directory /data/site1>
require all granted
</directory>
</virtualhost>
<virtualhost 192.168.36.8:80>
<directory /data/site2>
require all granted
</directory>
documentroot /data/site2
</virtualhost>
<virtualhost 192.168.36.9:80>
<directory /data/site3>
require all granted
</directory>
documentroot /data/site3
</virtualhost>
实验:基于主机头的多虚拟主机
1.修改配置文件
vim /etc/httpd/conf.d/vhosts.conf
<virtualhost *:80>
documentroot /data/site1
servername www.a.com
<directory /data/site1>
require all granted
</directory>
CustomLog "logs/a_access_log" combined
</virtualhost>
<virtualhost *:80>
<directory /data/site2>
require all granted
</directory>
documentroot /data/site2
servername www.b.com
CustomLog "logs/b_access_log" combined
</virtualhost>
<virtualhost *:80>
servername www.c.com
<directory /data/site3>
require all granted
</directory>
documentroot /data/site3
CustomLog "logs/c_access_log" combined
</virtualhost>
2.在测试机hosts文件中添加名词解析
vim /etc/hosts
192.168.36.7 www.a.com www.b.com www.c.com