httpd虚拟主机
# 一、配置基于域名的虚拟用户
# 1.创建虚拟用户的网页根目录
cd /usr/local/httpd/htdocs/
mkdir benetcom
cd benetcom
echo "<h1>benet</h1>" > index.html
cd ..
mkdir accpcom
cd accpcom
echo "<h1>accp</h1>" > index.html
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 2.修改虚拟用户的配置文件
cd /usr/local/httpd/conf/extra/
vim httpd-vhosts.conf # 更改配置文件
NameVirtualHost 192.168.25.105 # 更改为web服务器(本机)的IP地址
<VirtualHost 192.168.25.105>
DocumentRoot /usr/local/httpd/htdocs/benetcom/
ServerName www.benet.com
ErrorLog "logs/benet-error_log"
CustomLog "logs/benet-access_log" common
</VirtualHost>
<VirtualHost 192.168.25.105>
DocumentRoot /usr/local/httpd/htdocs/accpcom/
ServerName www.accp.com
ErrorLog "logs/accp-error_log"
CustomLog "logs/accp-access_log" common
</VirtualHost>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 3.修改主配置文件
还原一下配置httpd的原有配置。
cp httpd.conf httpd.conf.bak
1
vim /usr/local/httpd/conf/httpd.conf #在最后一行添加以下内容,加载外部的虚拟用户的配置文件
Include conf/extra/httpd-vhosts.conf
1
2
2
重启服务
service httpd restart
1
配置完虚拟主机后 重启报错:
[warn] NameVirtualHost *:80 has no VirtualHosts
1
解决:
vim httpd-vhosts.conf
1
将19行注释掉
#NameVirtualHost *:80
1
# 4.修改hosts文件
vim /etc/hosts #添加以下内容
192.168.3.50 www.benet.com
192.168.3.50 www.accp.com
1
2
3
2
3
# 5.测试虚拟用户配置
[root@CentOS6-node1 accpcom]# curl http://www.accp.com
<h1>accp</h1>
[root@CentOS6-node1 accpcom]# curl http://www.benet.com
<h1>benet</h1>
1
2
3
4
5
2
3
4
5
显示以上结果为虚拟用户配置成功
# 二、配置基于IP的虚拟主机
<VirtualHost 192.168.25.105>
DocumentRoot /usr/local/httpd/htdocs/benetcom/
ServerName www.benet.com
ErrorLog "logs/benet-error_log"
CustomLog "logs/benet-access_log" common
<VirtualHost 192.168.25.106>
DocumentRoot /usr/local/httpd/htdocs/benetcom/
ServerName www.accp.com
ErrorLog "logs/benet-error_log"
CustomLog "logs/benet-access_log" common
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 三、配置基于端口的虚拟主机
<VirtualHost 192.168.25.105:81>
……………
</VirtuealHost>
<VirtualHost 192.168.25.105:81>
……………….
</VirtuealHost>
1
2
3
4
5
6
2
3
4
5
6
在httpd.conf中加入以下配置
Include conf/extra/httpd-vhosts.conf
Listen 192.168.25.105:81
Listen 192.168.25.105:82
1
2
3
2
3
上次更新: 2022/06/12, 15:48:09