Apache httpd 2.4.x基于域名的虚拟主机

apache httpd2.4.x基于域名的虚拟主机

vim /etc/httpd/httpd.conf
#DocumentRoot “/usr/local/apache/htdocs(关闭此功能)
Include /etc/httpd/extra/httpd-vhosts.conf(开启此功能)

vim /etc/httpd/extra/httpd-vhosts.conf
修改为如下内容
<VirtualHost *:80>
DocumentRoot "/www/a.com"
ServerName www.a.com
# <Directory "/www/a.com">
# Options none
# AllowOverride none
# Require all granted
# </Directory>
ErrorLog "/www/a.com/logs/www.a.com-error_log"
CustomLog "/www/a.com/logs/www.a.com-access_log" combined
</VirtualHost>

分别创建www a.com logs目录
mkdir -p /www/a.com
cd /www/a.com
mddir logs

测试语法正确:/usr/local/apache/bin/apachectl -t
没有报错,启动apache /usr/local/apache/bin/apachectl restart

改电脑中的hosts为192.168.88.107 www.a.com

在电脑中用浏览器访问www.a.com,报以下错误:

[Tue Oct 06 19:04:16.164447 2015] [authz_core:error] [pid 1746:tid 3064986480] [client 192.168.88.3:55913] AH01630: client denied by server configuration: /www/a.com/
[Tue Oct 06 19:04:17.557801 2015] [authz_core:error] [pid 1746:tid 3054496624] [client 192.168.88.3:55913] AH01630: client denied by server configuration: /www/a.com/
[Tue Oct 06 19:04:19.308120 2015] [authz_core:error] [pid 1746:tid 3044006768] [client 192.168.88.3:55913] AH01630: client denied by server configuration: /www/a.com/
[Tue Oct 06 19:04:20.267327 2015] [authz_core:error] [pid 1746:tid 3033516912] [client 192.168.88.3:55913] AH01630: client denied by server configuration: /www/a.com/

原因在于httpd2.4.x默认是不开访问权限的,在虚拟主机中把以下加入就OK了(也就上面中注释掉的部分)。
# <Directory "/www/a.com">
# Options none
# AllowOverride none
# Require all granted
# </Directory>

同理www.b.com虚拟中的配置如下:
<VirtualHost *:80>
ServerAdmin b@b.com
DocumentRoot "/www/b.com"
ServerName www.b.com
<Directory "/www/b.com">
Options none
AllowOverride none
Require all granted
ErrorLog "/www/b.com/logs/www.b.com-error_log"
CustomLog "/www/b.com/logs/www.b.com-access_log" combined
</VirtualHost>

注意:在apache http2.4以前版本中,虚拟主机配置文件中要在开头有下面这行:
NameVirtualHost *:80
而在apache http2.4.x版本中就不需要了。

此条目发表在linux分类目录,贴了, , , , , 标签。将固定链接加入收藏夹。