Akemi

Apache虚拟主机

2025/04/09

原理与nginx的虚拟主机基本相同,都是子配置文件,然后给每个虚拟主机分配不同的根目录,基本上能随便坐

系统为Centos9 stream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
yum -y install httpd

# 创建根目录
mkdir -p /var/www/ws.com
mkdir -p /var/www/xhy.com

# 写入测试内容
echo "This is ws" > /var/www/ws.com/index.html
echo "This is xhy" > /var/www/xhy.com/index.html

# 给不同网址创建配置文件
cat > /etc/httpd/conf.d/ws.com.conf <<EOF
<VirtualHost *:80>
ServerAdmin admin@ws.com
ServerName ws.com
ServerAlias www.ws.com
DocumentRoot /var/www/ws.com

<Directory /var/www/ws.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error_ws.com.log
CustomLog ${APACHE_LOG_DIR}/access_ws.com.log combined
</VirtualHost>
EOF

cat > /etc/httpd/conf.d/xhy.com.conf <<EOF
<VirtualHost *:80>
ServerAdmin admin@xhy.com
ServerName xhy.com
ServerAlias www.xhy.com
DocumentRoot /var/www/xhy.com

<Directory /var/www/xhy.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error_xhy.com.log
CustomLog ${APACHE_LOG_DIR}/access_xhy.com.log combined
</VirtualHost>
EOF

httpd -t
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::5200:ff:fe01:0%eth0. Set the 'ServerName' directive globally to suppress this message
Syntax OK
这告警无所谓,反正就是没有FQDN域名

systemctl disable firewalld --now
setenforce 0

systemctl enable httpd --now

ss -tunlp | grep 80
tcp LISTEN 0 511 *:80 *:* users:(("httpd",pid=14607,fd=4),("httpd",pid=14606,fd=4),("httpd",pid=14605,fd=4),("httpd",pid=14603,fd=4))

测试

为了方便我直接使用linux进行测试了,反正html不复杂的情况下curl和浏览器访问是一样的

另一台主机

1
2
3
4
5
6
7
8
9
10
11
echo "10.163.2.100 xhy.com www.xhy.com
10.163.2.100 ws.com www.ws.com" >> /etc/hosts

[root@localhost ~]# curl xhy.com
This is xhy
[root@localhost ~]# curl www.xhy.com
This is xhy
[root@localhost ~]# curl ws.com && curl www.ws.com
This is ws
This is ws

CATALOG
  1. 1. 测试