Linux: 如何在Apache中禁用目录列表, Apache禁止显示目录列表, How to disable directory listing in Apache

 

目录清单ApacheAutoIndex模块提供。如果启用,并且Apache下特定路径或文件夹中没有DirectoryIndex文件,那么将会显示文件和文件夹列表。

Hosting:如何在cPanel中禁用Apache目录列表, How to disable Apache directory listing in cPanel

常见的示例DirectoryIndex文件是index.htmlindex.htmindex.phpwelcome.html

当然,一个简单方便的方法就是手动创建一个DirectoryIndex文件,但是出于安全原因或其他原因,您最好是能够禁用目录浏览。

Apache可以禁用mod_autoindex,在Apache配置文件中设置适当的选项或使用htaccess文件来禁用的 自动目录索引。

如果您在平台上托管cPanel,也可以使用特定于平台的方法来禁用Apache的目录列表。

禁用Apache中目录列表的方法:

  • 禁用mod_autoindex
  • 配置目录的选项指令
  • 配置.htaccess

 

通过禁用自动索引模块来禁用Apache目录列表

最简单的方法可能是autoindex完全禁用该模块,禁用该模块将影响服务器上托管的所有站点。

  1. 启动您的首选终端应用程序
  2. 禁用的autoindex模块Apache

    $ sudo a2dismod --force autoindex  #Ubuntu,Debian和SUSE

    模块自动索引已禁用。 要激活新配置,您需要运行:

    sudo systemctl restart apache2 # 或者 sudo service apache2 restart
    • 具有a2dismod支持的发行版可以简单地运行上面的命令,而无需手动禁用所需的模块。
    • LoadModuleautoindex需要通过删除或注释(#在开头添加)配置文件中的行来手动禁用相应模块的指令。

     

    Options Debian, Ubuntu openSUSE and SLES Fedora Core, CentOS, RHEL macOS homebrew xampp
    a2dismod support yes yes no no no no
    Modules to uninstall none
    Module name n/a autoindex
    Loadmodule directive n/a #LoadModule autoindex_module <module_locations>/mod_autoindex.so
  3. 重新启动Apache,以使更改生效。(方法见2)

     

通过Directory的Options指令禁用Apache目录列表

您可以在的配置文件中将指令添加-IndexesOptionsApache以完全禁用目录列表,或者在配置中添加相同的-Indexes选项Directory以禁用每个目录的功能。

  1. Apache使用您喜欢的文本编辑器打开的配置文件。

    $ sudo vi /etc/apache2/other/mysite.conf
    

    该配置可以全局设置,也可以从VirtualHost配置内部设置。

  2. 添加-IndexesOptions所需目录的指令。

    <Directory /var/www/mysite>
        Options -Indexes
    </Directory>

    请注意,是-Indexes不是+Indexes

  3. 重新启动Apache,以使更改生效。

     

使用.htaccess禁用Apache目录列表

如果您没有系统管理员权限,或者只想轻松地按目录管理目录列表,则可以Options -Indexeshtaccess文件中使用以上指令。

  1. htaccess使用首选的文本编辑器在您要禁用列表的目录上打开或创建文件。

    $ sudo vi /var/www/mysite/.htaccess
    
  2. 在文件中添加-IndexesOptions指令htaccess

    Options -Indexes

     

指南兼容性:

操作系统
Ubuntu 16.04 LTS(Xenial Xerus)
Ubuntu 16.10(Yakkety Yak)
Ubuntu 17.04(Zesty Zapus)
Ubuntu 17.10(Artful Aardvark)
Ubuntu 18.04 LTS(Bionic Beaver)
Ubuntu 18.10(Cosmic Cuttlefish)
Ubuntu 19.04(Disco Dingo)

 

 

本文:Linux: 如何在Apache中禁用目录列表, Apache禁止显示目录列表, How to disable directory listing in Apache

Loading

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.