How to show Directory Index in Apache 2.4 with custom Document Root

有些话、适合烂在心里 提交于 2019-12-03 09:56:38

Turns out you need to disable DirectoryIndex in Apache 2.4 to get auto Indexes.

DirectoryIndex disabled
Options Indexes

When DirectoryIndex is not disabled, auto index does not work and apache sends either a 403 Forbidden or a 404 File not found if you use fastcgi/php-fpm.

Here are the corresponding error log lines (for search purposes):

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

The second line is redundant, because you've already turned on all the options with the first line, and since the two options aren't prefixed with +, they actually REPLACE the entire options list enabled set with All with just those two individual options.

I managed to get it to work

Basically it seems that Apache2.4 doesn't carry over the settings from DocumentRoot to your virtual hosts unless the virtual hosts are subfolders of DocumentRoot, like previous versions used to do. Which kind of makes sense, but the change should be documented and it wasn't.

What I mean is, in your httpd.conf you'll have (this is an OS X one):

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

And then in your 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>

The VH used to inherit all the settings - not anymore if it's not a subfolder. So what you need to do is copy and paste the settings in the VH (or you can probably create another <directory if you have a lot of VHs in the same place)

<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>

It's the +Indexes which does the magic.

balucio

I had the same problem with Centos 7.2 and apache 2.4.

In new installation, the problem is most likely caused by welcome.conf that disable Option Indexes in every location:

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

This file is restored on every Apache upgrade, then you should comment or delete previous lines.

viki

In the log you can find an error

[Sun Dec 03 17:38:17.649269 2017] [autoindex:error] [pid 4806] [client ::1:57323] AH01276: Cannot serve directory /etc/httpd/conf/htdocs/: No matching DirectoryIndex () found, and server-generated directory index forbidden by Options directive

to fix it:-

then you must remove the line in /etc/httpd/conf.d/welcome.conf

below existing configuration:-

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

solved with the below configuration,:- commented out a line.

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

Add this line to you vhost.conf file for the site

DirectoryIndex default.html

And you are all set

for future people,if you follow all above and problem still occur,try this:

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">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!