Apache2.4多虚拟主机VirtualHost三种实现
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache2.4多虚拟主机VirtualHost三种实现相关的知识,希望对你有一定的参考价值。
Apache httpd
- httpd是Apache基金会的HTTP服务器项目:HTTP Server project
- Apache httpd的安装详见:Linux源码编译安装apache httpd2.4.48
- https://blog.csdn.net/timonium/article/details/117326413
Apache httpd虚拟主机
- httpd 支持在一台物理主机上添加多个虚拟主机VirtualHost
- 通过添加多个个虚拟主机可以实现同服务器多实例网站
网站的唯一标识:
- IP相同,但端口不同
- IP不同,但端口均为默认端口
- FQDN不同, IP和端口都相同
多虚拟主机有三种实现方案:
- 基于ip:为每个虚拟主机准备至少一个ip地址
- 基于port:为每个虚拟主机使用至少一个独立的port
- 基于FQDN:为每个虚拟主机使用至少一个FQDN,请求报文中首部
注意:httpd 2.4版本中,基于FQDN的虚拟主机不再需要NameVirutalHost指令
虚拟主机的基本配置方法:
- 在配置文件中添加VirtualHost
<VirtualHost IP:PORT>
ServerName FQDN
DocumentRoot "/path"
</VirtualHost>
- 上述配置应该存放在独立的配置文件中
- 配套设置
ServerAlias:虚拟主机的别名;可多次使用
ErrorLog: 错误日志
CustomLog:访问日志
<Directory "/path"> </Directory>
实现多虚拟主机VirtualHost
创建虚拟主机存放文件夹
[ -d /webDB ] || mkdir -pv /webDB/{logs,vhost,vhostslist}
[root@C8-192 ~]# [ -d /webDB ] || mkdir -pv /webDB/{vhost,vhostslist}
mkdir: created directory '/logs'
mkdir: created directory '/webDB'
mkdir: created directory '/webDB/vhost'
mkdir: created directory '/webDB/vhostslist'
- 赋权
chown -R apache.apache /webDB
创建测试网页
mkdir -pv /webDB/vhosts/website{1..3}
for i in {1..3};do echo "I am host $i" > /webDB/vhosts/website$i/index.html done
- 示例
[root@C8-192 ~]# mkdir -pv /webDB/vhosts/website{1..3}
mkdir: created directory '/webDB/vhosts'
mkdir: created directory '/webDB/vhosts/website1'
mkdir: created directory '/webDB/vhosts/website2'
mkdir: created directory '/webDB/vhosts/website3'
[root@C8-192 ~]# for i in {1..3};do echo "I am host $i" > /webDB/vhosts/website$i/index.html
> done
[root@C8-192 ~]# cat /webDB/vhosts/website1/index.html
I am host 1
[root@C8-192 ~]# tree /webDB/
/webDB/
├── logs
├── vhost
├── vhosts
│ ├── website1
│ │ └── index.html
│ ├── website2
│ │ └── index.html
│ └── website3
│ └── index.html
└── vhostslist
6 directories, 3 files
修改主配置文件添加虚拟主机配置文件列表配置文件
- 一下方法二选一都可以实现
取消配置文件中关于虚拟主机配置文件的注释
sed -ri.bak 's%#(Include conf/extra/httpd-vhosts.conf)%\\1%g' /apps/httpd24/conf/httpd.conf
自定义虚拟主机配置文件位置
sed -ri.bak '/# Virtual hosts/a\\Include /webDB/vhostslist/httpd-vhosts.conf' /apps/httpd24/conf/httpd.conf
实现基于IP的虚拟主机
- 服务器配置多个IP地址
- 若配置公网地址则成本高
主配置文件添加虚拟主机配置
sed -ri.bak '/# Virtual hosts/a\\Include /webDB/vhostslist/httpd-vhosts-ip.conf' /apps/httpd24/conf/httpd.conf
添加虚拟主机配置文件
cat > /webDB/vhostslist/httpd-vhosts-ip.conf << SUN
<virtualhost 10.0.0.81:80>
documentroot /webDB/vhosts/website1/
CustomLog /webDB/logs/website1_access.log combined
<directory /webDB/vhosts/website1>
require all granted
</directory>
</virtualhost>
<virtualhost 10.0.0.82:80>
documentroot /webDB/vhosts/website2/
CustomLog /webDB/logs/website2_access.log combined
<directory /webDB/vhosts/website2>
require all granted
</directory>
</virtualhost>
<virtualhost 10.0.0.83:80>
documentroot /webDB/vhosts/website3/
CustomLog /webDB/logs/website3_access.log combined
<directory /webDB/vhosts/website3>
require all granted
</directory>
</virtualhost>
SUN
实现基于port端口的虚拟主机
- 用户访问时需要添加端口
- 主配置文件添加虚拟主机配置
sed -ri.bak '/# Virtual hosts/a\\Include /webDB/vhostslist/httpd-vhosts-port.conf' /apps/httpd24/conf/httpd.conf
- 添加虚拟主机配置文件
cat > /webDB/vhostslist/httpd-vhosts-port.conf << SUN
listen 8001
listen 8002
listen 8003
<virtualhost *:8001>
documentroot /webDB/vhosts/website1
CustomLog /webDB/logs/website1_access.log combined
<directory /webDB/vhosts/website1>
require all granted
</directory>
</virtualhost>
<virtualhost *:8002>
documentroot /webDB/vhosts/website2
CustomLog /webDB/logs/website2_access.log combined
<directory /webDB/vhosts/website2>
require all granted
</directory>
</virtualhost>
<virtualhost *:8003>
documentroot /webDB/vhosts/website3
CustomLog /webDB/logs/website3_access.log combined
<directory /webDB/vhosts/website3>
require all granted
</directory>
</virtualhost>
SUN
实现基于FQDN域名的虚拟主机
- 基于主机头实现
- 添加servername字段
- 添加文件路径授权
- 主配置文件添加虚拟主机配置
sed -ri.bak '/# Virtual hosts/a\\Include /webDB/vhostslist/httpd-vhosts-dns.conf' /apps/httpd24/conf/httpd.conf
- 添加虚拟主机配置文件
cat > /webDB/vhostslist/httpd-vhosts-dns.conf << SUN
<VirtualHost *:80>
ServerName www.a.co
DocumentRoot "/webDB/vhosts/website1"
<Directory "/webDB/vhosts/website1">
Options None
## AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.b.cp
DocumentRoot "/webDB/vhosts/website2"
<Directory "/webDB/vhosts/website2">
Options None
## AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.c.cq
DocumentRoot "/webDB/vhosts/website3"
<Directory "/webDB/vhosts/website3">
Options None
## AllowOverride None
Require all granted
</Directory>
</VirtualHost>
SUN
测试
重启httpd服务器
systemctl restart httpd.service
查看端口
[root@C8-192 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:8001 *:*
LISTEN 0 128 *:8002 *:*
LISTEN 0 128 *:8003 *:*
给网卡添加临时IP用于测试
for i in {1..3};do $(ip a a 10.0.0.8$i/24 dev eth0 label eth0:$i); done
- 示例
[root@C8-192 ~]# for i in {1..3};do $(ip a a 10.0.0.8$i/24 dev eth0 label eth0:$i); done
[root@C8-192 ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:cb:ca:bf brd ff:ff:ff:ff:ff:ff
inet 10.0.0.192/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 10.0.0.81/24 scope global secondary eth0:1
valid_lft forever preferred_lft forever
inet 10.0.0.82/24 scope global secondary eth0:2
valid_lft forever preferred_lft forever
inet 10.0.0.83/24 scope global secondary eth0:3
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fecb:cabf/64 scope link
valid_lft forever preferred_lft forever
添加本地域名用于测试
- 修改/etc/hosts文件模拟DNS解析域名到本地
sed -ri.bak '/^127.0.0.1/a\\127.0.0.1 www.a.co\\n127.0.0.1 www.b.cp\\n127.0.0.1 www.c.cq' /etc/hosts
- 示例
[root@C8-192 ~]# sed -ri.bak '/^127.0.0.1/a\\127.0.0.1 www.a.co\\n127.0.0.1 www.b.cp\\n127.0.0.1 www.c.cq' /etc/hosts
[root@C8-192 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
127.0.0.1 www.a.co
127.0.0.1 www.b.cp
127.0.0.1 www.c.cq
::1 localhost localhost.localdomain localhost6 localhost6.localdomain
[root@C8-192 ~]# fping -A www.a.co www.b.cp www.c.cq
127.0.0.1 is alive
127.0.0.1 is alive
127.0.0.1 is alive
使用IP地址访问
[root@C8-192 ~]# curl 10.0.0.81
I am host 1
[root@C8-192 ~]# curl 10.0.0.82
I am host 2
[root@C8-192 ~]# curl 10.0.0.83
I am host 3
使用port端口访问
[root@C8-192 ~]# curl 10.0.0.192:80
I am host 1
[root@C8-192 ~]# curl 10.0.0.192:81
curl: (7) Failed to connect to 10.0.0.192 port 81: Connection refused
[root@C8-192 ~]# curl 10.0.0.192:8001
I am host 1
[root@C8-192 ~]# curl 10.0.0.192:8002
I am host 2
[root@C8-192 ~]# curl 10.0.0.192:8003
I am host 3
使用domanname域名访问
[root@C8-192 ~]# curl www.a.co
I am host 1
[root@C8-192 ~]# curl www.b.cp
I am host 2
[root@C8-192 ~]# curl www.c.cq
I am host 3
至此,配置Apache httpd 多虚拟主机实现完成!
重启后排错
- 第一次重启后,果不出所料报错了
[root@C8-192 ~]# systemctl restart httpd.service
Job for httpd.service failed because the control process exited with error code.
See "systemctl status httpd.service" and "journalctl -xe" for details.
[root@C8-192 ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2021-05-28 00:07:05 CST; 20s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 50967 ExecStop=/apps/httpd24/bin/apachectl stop (code=exited, status=1/FAILURE)
Process: 50974 ExecStart=/apps/httpd24/bin/apachectl start (code=exited, status=1/FAILURE)
Main PID: 50583 (code=killed, signal=KILL)
May 28 00:07:05 C8-192 systemd[1]: Starting The Apache HTTP Server...
May 28 00:07:05 C8-192 apachectl[50974]: AH00526: Syntax error on line 5 of /webDB/vhostslist/httpd-vhosts-dns.conf:
May 28 00:07:05 C8-192 apachectl[50974]: ErrorLog not allowed in <Directory> context
May 28 00:07:05 C8-192 systemd[1]: httpd.service: Control process exited, code=exited status=1
May 28 00:07:05 C8-192 systemd[1]: httpd.service: Failed with result 'exit-code'.
May 28 00:07:05 C8-192 systemd[1]: Failed to start The Apache HTTP Server.
[root@C8-192 ~]# httpd -t
AH00526: Syntax error on line 5 of /webDB/vhostslist/httpd-vhosts-dns.conf:
ErrorLog not allowed in <Directory> context
原因
- httpd-vhosts-dns.conf的日志文件和错误日志所在自定义文件夹没有授权
排错
- 删除自定义日志段后成功启动
以上是关于Apache2.4多虚拟主机VirtualHost三种实现的主要内容,如果未能解决你的问题,请参考以下文章