如何在 Apache 2.4 中使用自定义文档根目录显示目录索引



我在 Ubuntu 2.4 的 Apache 13.10 中遇到问题。 我尝试将文档根目录更改为/home/fandi/public_html 而且一切正常。但是我尝试在我的public_html中创建文件夹/我收到这样的错误:

[Sat Jan 25 10:59:50.149441 2014] [autoindex:error] [pid 1093] [client 127.0.0.1:39901] AH01276: Cannot serve directory /home/fandi/public_html/report_php/: No matching DirectoryIndex (index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found, and server-generated directory index forbidden by Options directive

我必须创建文件index.htmlindex.php和其他index.xxx文件。

默认情况下,它必须显示目录索引。 如何启用目录索引?

这是我的文件000-default.conf

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /home/fandi/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory "/home/fandi/public_html">
Options All
AllowOverride All
Require all granted
Options Indexes FollowSymLinks
</Directory>
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

请帮忙,谢谢^^

事实证明,您需要在Apache 2.4中禁用DirectoryIndex才能获得自动索引。

DirectoryIndex disabled
Options Indexes

如果未禁用 DirectoryIndex,则自动索引不起作用,并且如果您使用 fastcgi/php-fpm,apache 会发送 403 禁止或 404 文件。

以下是相应的错误日志行(用于搜索目的):

[authz_core:error] client denied by server configuration:
[proxy_fcgi:error] Got error 'Primary script unknownn'
Options All <--turn on all options
Options Indexes FollowSymLinks   <--- replace previously set options with these two

第二行是多余的,因为您已经用第一行打开了所有选项,并且由于这两个选项没有前缀+,它们实际上将启用的整个选项列表替换为仅使用这两个单独的选项All

我在 Centos 7.2 和 apache 2.4 上遇到了同样的问题。

在新安装中,问题很可能是由在每个位置禁用选项索引welcome.conf引起的:

<LocationMatch "^/+$">
Options -Indexes
ErrorDocument 403 /.noindex.html
</LocationMatch>

此文件在每次 Apache 升级时都会恢复,然后您应该注释或删除以前的行。

我设法让它工作

基本上,Apache2.4 似乎不会将设置从 DocumentRoot 转移到您的虚拟主机,除非虚拟主机是 DocumentRoot 的子文件夹,就像以前的版本一样。哪种是有道理的,但应该记录更改,但事实并非如此。

我的意思是,在你的httpd.conf中,你会有(这是一个OS X):

DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
Options +Indexes +FollowSymLinks
# etc
</Directory>

然后在你的extra/httpd-vhosts.conf中

<VirtualHost *:80>
DocumentRoot "/pth/to/somewhere/completely/different"
ServerName my-virtual-host.dev
ErrorLog "/private/var/log/apache2/my-virtual-host.dev-error_log"
CustomLog "/private/var/log/apache2/my-virtual-host.dev-access_log" common
</VirtualHost>

VH 用于继承所有设置 - 如果它不是子文件夹,则不再继承。因此,您需要做的是将设置复制并粘贴到VH中(或者,如果在同一位置有很多VH,则可以创建另一个<directory)

<VirtualHost *:80>
DocumentRoot "/pth/to/somewhere/completely/different"
ServerName my-virtual-host.dev
ErrorLog "/private/var/log/apache2/my-virtual-host.dev-error_log"
CustomLog "/private/var/log/apache2/my-virtual-host.dev-access_log" common
<Directory "/pth/to/somewhere/completely/different">
Options +Indexes
</Directory>
</VirtualHost>

是+索引发挥了魔力。

在日志中,您可以找到错误

[周日 12 月 03 日 17:38:17.649269 2017] [自动索引:错误] [PID 4806] [客户端 ::1:57323] AH01276:无法提供目录/etc/httpd/conf/htdocs/:找不到匹配的目录索引 (),并且服务器生成的目录索引被选项指令禁止

修复它:-

然后你必须删除/etc/httpd/conf.d/welcome.conf 中的行

低于现有配置:-

<LocationMatch "^/+$">
Options -Indexes
ErrorDocument 403 /.noindex.html
</LocationMatch>

使用以下配置解决:- 注释掉了一行。

<LocationMatch "^/+$">
#Options -Indexes
ErrorDocument 403 /.noindex.html
</LocationMatch>

对于将来的人,如果您遵循上述所有内容并且问题仍然出现,请尝试以下操作:

httpd.conf(make sure belows are open):
LoadModule alias_module modules/mod_alias.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule autoindex_module modules/mod_autoindex.so
Include conf/extra/httpd-autoindex.conf

extra/httpd-autoindex.conf:

<Directory "change to your directory">

最新更新