最近想把web开发目录从/var/www/转移到/www目录,并配置apache2虚拟主机,结果遇到各种403错误,非常郁闷。最终在大佬DISHY的文章的帮助下(参考资料有),终于找到问题所在,于是便写了这篇文章。
解决方法
大家都非常着急这里我就直接说解决方法了,如果想知道原因可以往下看。
只需要加入
<Directory "你的网站根目录">
AllowOverride None
Require all granted
</Directory>
到apache2虚拟主机的配置文件就行了!
比如说这是我的配置文件:
错误分析
初步分析
首先出现这个状况的原因是我们更换了apache2虚拟机的根目录,所以我们应该从为什么更换网站根目录后会报错 403 入手 .....
我们再看 403 状态码是什么?
我们可以简单的理解为没有权限访问此站。该状态表示服务器理解了本次请求但是拒绝执行该任务,该请求不该重发给服务器。
显而易见我们在更改网站根目录后的报错是 403 Forbidden。代表了我们应该从服务器的内部寻找原因
错误日志
“在没有错误日志的情况下诊断任何问题无异于闭眼开车。”
在确定是服务器内部的问题以后接下来就是要看 apache2 的错误日志了!
在 ubutnu 系统下,apache2 的默认错误文件在 /var/log/apache2/error.log ,具体的位置还是要看你虚拟机配置文件指定的错误日志位置。
打开 /var/log/apache2/error.log (这里我展示了本错误的日志内容)
[Tue Feb 28 04:12:09.647427 2023] [authz_core:error] [pid 17315] [client 127.0.0.1:39748] AH01630: client denied by server configuration: /www/xuanyu.info/wiki./robots.txt
[Tue Feb 28 04:12:21.360982 2023] [authz_core:error] [pid 17316] [client 127.0.0.1:46440] AH01630: client denied by server configuration: /www/xuanyu.info/wiki./
[Tue Feb 28 04:47:47.482179 2023] [authz_core:error] [pid 17317] [client 127.0.0.1:36100] AH01630: client denied by server configuration: /www/xuanyu.info/wiki./index.php
[Tue Feb 28 04:47:47.862668 2023] [authz_core:error] [pid 17317] [client 127.0.0.1:36100] AH01630: client denied by server configuration: /www/xuanyu.info/wiki./favicon.ico, referer: http://wiki.xuanyu.info/index.php/%E9%A6%96%E9%A1%B5
[Tue Feb 28 04:47:53.685101 2023] [authz_core:error] [pid 17318] [client 127.0.0.1:55302] AH01630: client denied by server configuration: /www/xuanyu.info/wiki./
[Tue Feb 28 04:47:54.106327 2023] [authz_core:error] [pid 17318] [client 127.0.0.1:55302] AH01630: client denied by server configuration: /www/xuanyu.info/wiki./favicon.ico, referer: http://wiki.xuanyu.info/
如果还不知道 apache2 的错误文件怎么看的话可以去 apache2 的官方文档中查看,这里不再论述。
我们可以看到错误日志的 第五部分——内容表示错误的详细信息 中我们可以看到错误代码:AH01630 (服务器配置拒绝客户端)我们就可以推断出这是由于虚拟机配置文件的配置错误而导致的错误。
配置文件
这是我的配置文件(apache2.conf)内容:
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#<Directory /srv/>
# Options Indexes FollowSymLinks
# AllowOverride None
# Require all granted
#</Directory>
ps:由于apache.conf 中的内容较多所以我只选取了对解决本次问题有关系的一段,即 153~180 行
这是 ubuntu 安装 apache2 后的默认虚拟机配置,我仅仅是更改了端口和网站的根目录
在仔细的查看配置文件后,我发现了以上的内容,主要看注释部分:
注释已经说了,这是防止访问根目录以外的目录做的特殊安全设定,默认情况下除了 /usr/share 和 /var/www 外,其他目录都apache服务器都不能访问。要使 /www 可以访问,就要增加一个类似的访问请求项目:
<Directory /home/user/webroot>
AllowOverride None
Require all granted
</Directory>
OK,再次访问,问题解决!
但是这还没完,难道增加一个虚拟主机就得修改一次apache2.conf文件?
答案是肯定的,其实还是 000-default.conf 配置问题,需要增加一个配置项目:Require all granted。
完整的配置如下:
<VirtualHost *:8082>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /www/xuanyu.info/wiki.
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
至此,问题全部解决!
作者:玄予(xuanyu)
原文链接: www.xuanyu.info/p/650.html
最后更新时间:2023年3月3日
本文支持转载,但请将此块内容一同转载,以保证文章内容的时效性。