httpd搭建虚拟主机,基于FQDN为常用方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_43551152/article/details/85041483

准备工作
创建三个文件夹,分别对应三个网站

[root@centos7 ~]#mkdir /data/{a,b,c}site
[root@centos7 ~]#echo www.a.com > /data/asite/index.html
[root@centos7 ~]#echo www.b.com > /data/bsite/index.html 
[root@centos7 ~]#echo www.c.cn > /data/csite/index.html
添加一个IP地址
[root@centos7 ~]#ip a a 192.168.245.18 dev ens33
三个IP分别对应a,b,c三个网站
192.168.245.17
192.168.245.18
172.18.134.14

1>基于IP地址不同,创建三个虚拟主机

vim /etc/httpd/conf.d/test.conf 配置文件中加入授权及IP地址对应的目录,日志文件分别存放

<virtualhost 192.168.245.17:80>
DocumentRoot "/data/asite"
<Directory /data/asite>
Require all granted
</Directory>
</virtualhost>
customlog /var/log/httpd/access_a.log teselog

<virtualhost 192.168.245.18:80>
DocumentRoot "/data/bsite"
<Directory /data/bsite>
Require all granted
</Directory>                                                                                    
</virtualhost>
customlog /var/log/httpd/access_b.log teselog

<virtualhost 172.18.134.14:80>
DocumentRoot "/data/csite"
<Directory /data/csite>
Require all granted
</Directory>                                                                                    
</virtualhost>
customlog /var/log/httpd/access_c.log teselog

systemctl restart httpd 重启服务
分别输入三个地址
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
可以看到三个虚拟主机已分离

2>基于端口号不同,创建三个虚拟主机

vim /etc/httpd/conf.d/test.conf 配置文件中加入授权及IP地址对应的目录,日志文件分别存放

listen 81
listen 82
listen 83
<virtualhost *:81>
DocumentRoot "/data/asite"
<Directory /data/asite>
Require all granted
</Directory>
</virtualhost>
customlog /var/log/httpd/access_a.log teselog

<virtualhost *:82>
DocumentRoot "/data/bsite"
<Directory /data/bsite>
Require all granted
</Directory>                                                                                    
</virtualhost>
customlog /var/log/httpd/access_b.log teselog

<virtualhost *:83>                                                                              
DocumentRoot "/data/csite"
<Directory /data/csite>
Require all granted
</Directory>                                                                                    
</virtualhost>

systemctl restart httpd 重启服务
注:listen监听三个端口,用*代表所有IP地址,即此主机上的所有IP都可,用81,82,83三个端口号分别代表a,b,c三个网站
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
可以看到三个虚拟主机已基于端口号分离

3>基于FQDN不同,创建三个虚拟主机,基于域名解析,常用

vim /etc/httpd/conf.d/test.conf 配置文件中加入授权及IP地址对应的目录,日志文件分别存放

<virtualhost *:80>
servername www.a.com
DocumentRoot "/data/asite"
<Directory /data/asite>
Require all granted
</Directory>
</virtualhost>
customlog /var/log/httpd/access_a.log teselog

<virtualhost *:80>
servername www.b.com                                                                            
DocumentRoot "/data/bsite"
<Directory /data/bsite>
Require all granted
</Directory>                                                                                    
</virtualhost>
customlog /var/log/httpd/access_b.log teselog

<virtualhost *:80>
servername www.c.cn
DocumentRoot "/data/csite"
<Directory /data/csite>
Require all granted
</Directory>                                                                                    
</virtualhost>

systemctl restart httpd 重启服务
分别加入servername,去掉listen,把端口号统一

注:另外找一台虚拟机,如192.168.245.37,修改hosts,和DNS原理相同,模拟DNS解析
vim /etc/hosts 加入三个域名
192.168.245.17 www.a.com www.b.com www.c.cn

在192.168.245.37上curl

[root@centos7 ~]#curl www.a.com
www.a.com
[root@centos7 ~]#curl www.b.com
www.b.com
[root@centos7 ~]#curl www.c.cn
www.c.cn

在37主机上telnet连接测试,输入host后连续敲两次回车

[root@centos7 ~]#telnet 192.168.245.17 80
Trying 192.168.245.17...
Connected to 192.168.245.17.
Escape character is '^]'.
GET /index.html HTTP/1.1
HOST: WWW.C.CN

HTTP/1.1 200 OK
Date: Sun, 16 Dec 2018 20:59:10 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Sun, 16 Dec 2018 19:52:33 GMT
ETag: "9-57d2901560e36"
Accept-Ranges: bytes
Content-Length: 9
Content-Type: text/html; charset=UTF-8

www.c.cn
Connection closed by foreign host.

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43551152/article/details/85041483