Loading

Apache 三种虚拟主机配置方式

https://blog.csdn.net/wanwanperfect/article/details/78332374

Apache 三种虚拟主机配置方式

1. 基于端口

1.1 配置

mkdir /data/site{1,2,3}
for i in 1 2 3;do echo site$i >/data/site$i/index.html;done
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>

1.2 测试

for m in 1 2 3;do curl localhost:800$m;done

2. 基于IP

2.1 配置

# 给网卡添加虚拟IP
for ip in 1 2 3;do ip a a 192.168.36.$ip dev eth0;done

vim /etc/httpd/conf.d/vhosts.conf 
<virtualhost 192.168.36.1:80>
documentroot /data/site1
<directory /data/site1>
require all granted
</directory>
</virtualhost>

<virtualhost 192.168.36.2:80>
<directory /data/site2>
require all granted
</directory>
documentroot /data/site2
</virtualhost>

<virtualhost 192.168.36.3:80>                                                                                         
<directory /data/site3>
require all granted
</directory>
documentroot /data/site3
</virtualhost>

2.2 测试

for ip in 1 2 3; do curl 192.168.36.$ip;done

3. 基于域名

3.1 配置

vim /etc/httpd/conf.d/vhosts.conf 
<virtualhost *:80>
documentroot /data/site1
servername www.1.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.2.com
CustomLog "logs/b_access_log" combined
</virtualhost>

<virtualhost *:80>
servername www.2.com
<directory /data/site3>                                                                                               
require all granted
</directory>
documentroot /data/site3
CustomLog "logs/c_access_log" combined
</virtualhost>

3.2 测试

for n in 1 2 3; do echo 127.0.0.1 www.$n.com >>/etc/hosts;curl www.$n.com;done
posted @ 2021-01-25 11:01  Jas0n0ss  阅读(111)  评论(0编辑  收藏  举报