轉載鏈接:http://blog.sina.com.cn/s/blog_505dd27f0100orae.html
在PHP網站開發中,基于WEB服務器和PHP網站程序代碼的安全考慮,我們需要對相關的目錄或者文件訪問權限進行控制,以防止意外情況的發生,那么我們如何來實現這種功能呢?我們可以通過Apache來實現禁止目錄訪問(禁止游覽列出的目錄或文件列表)、禁止或允許IP與域名訪問目錄的功能。
在Apache中配置禁止目錄訪問,即禁止游覽列出的目錄/文件列表的方法
訪問網站目錄時Apache默認配置為可列出目錄/文件列表,即當你訪問http://localhost時會列出相關的目錄和文件列表,我們可以通過修改Apache配置文件httpd.conf來實現禁止列出目錄/文件列表,方法如下:
1、打開apache配置文件httpd.conf
2、找到
<Directory />Options IndexesAllowOverride NoneOrder allow,denyAllow from all
</Directory>
只需要修改Options Indexes為Options None即可,注:根據PHP運行環境安裝包的不同,Options Indexes也有可能是Options Indexes FollowSymLinks,一并改為Options None即可。
還需注意的事:如果是自己重新設置了DocumentRoot,例如以下:修改的地方也就不同了
<Directory "D:/Apache2/webpage">#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.0/mod/core.html#options
# for more information.
#
# Options Indexes FollowSymLinks
Options FollowSymLinks#Indexes 的作用就是當該目錄下沒有 index.html 文件時,就顯示目錄結構,去掉 Indexes,Apache 就不會顯示該目錄的列表了。# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all</Directory>
3、保存httpd.conf,并重啟Apache即可,此時再訪問http://localhost時, 報apache http 403 禁止訪問錯誤信息
Forbidden
You don’t have permission to access / on this server.
另一種方法是在指定的web目錄下:http://jwc.jxatei.net:8080/ "index.html、index.php時",也可解決訪問目錄問題,但沒有解決根本問題。
至此,通過配置Apache服務器我們可以實現禁止目錄訪問(列出目錄或文件列表),這些功能對提高和優化WEB服務器的安全性能是必不可少的方法。