9月10日 設定目錄權限

在 XP 找到 apache/conf/http.conf 編輯
在 LINUX /usr/local/etc/apache22/httpd.conf 編輯


設定目錄權限

接下來的幾個區段中,您會看到 的設定。這個設定是用來指定某個目錄的存取權限,例如是否允許執行 CGI、是否要列出該目錄下的所有檔案等等。

我們以之前所設定的網頁根目錄「DocumentRoot」為例,當您修改了網頁根目錄後,您也必須將這個區段中的 改成您的網頁目錄。由於 Apache 將一般的網頁和 CGI 分別放在不同的目錄,所以您的 DocumentRoot 設定和這裡的設定稍有不同。但您可以將 及 DocumentRoot 都改成 /home/www。



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

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



我們後續再來說明一下 這個區段中每個項目的用途。

Options 設定目錄可以使用的功能

我們在 中指定了要設定哪一個目錄後,可以再使用 Option 來設定該目錄可以使用哪些功能。以下為功能的列表及說明:

* All:允許使用除了 MultiViews 以外的所有功能,此項目為預設值。
* ExecCGI:允許在此目錄中執行 CGI。
* FollowSymLinks:允許在目錄中使用 Symbolic links。使用者可以在此目錄中建一個 symbolic link 連結到網頁根目錄以外的地方。例如,如果你在網頁根目錄中建一個 symbolic link 到 /etc/master.passwd,則使用 http://192.168.0.1/master.passwd 就可以看到 /etc/master.passwd 的內容。建議您將這個項目移除,以避免安全性問題產生。
* Includes:允許使用 Server-side includes (SII)。
* IncludesNOEXEC:允許使用 SII,但不可以使用 #exec 去執行系統指令或是 CGI。
* Indexes:當目錄中沒有 DirectoryIndex 所設定的網頁時 (如 index.html),是否要由 Apache 自動產生目錄中的檔案列表。除非您的網頁是要開放改使用者下載檔案,並希望由 Apache 產生檔案列表,否則建議您關閉這項功能。
* MultiViews:允許使用 Content negotiated 的功能,自動經由伺服器和 Client 的內容協商,決定要使用哪一種語言的檔案。
* SymLinksIfOwnerMatch:如果 symbolic link 所指向的檔案擁有者符合 Apache 執行的使用者,則允許使用 symbolic link。

當我們對目錄設定 Option 的項目後,它的子目錄也會繼承該目錄的設定值。如果我們同時設定一個目錄及其子目錄的 Option 的設,會以最接近的目錄設定為主。例如,我們為 /home/www 及 /home/www/photo 設定權限如下:


Options Indexes FollowSymLinks



Options Includes


我們設定了 /home/www 可以使用 Indexes 及 FollowSymLinks,但 /home/www/photo 只能使用 Includes。雖然 /home/www/photo 是 /home/www 的子目錄,但是其目錄的設定只會以自己的 Options 為主,也就是不具有 Indexes 及FollowSymLinks,只有 Includes 的權限。

如果您希望子目錄可以繼承上一層的設定,可以使用 + 及 - 的符號表示。例如:


Options Indexes FollowSymLinks



Options +Includes -Indexes


我們在 /home/www/photo 的 Option 中使用了 + 及 -,所以它會繼承 /home/www 的設定,但是加上 Includes 的功能,並取消 Indexes,所以最後 /home/www/photo 的權限就是 Includes 及 FollowSymLinks。

AllowOverride 設定 .htaccess 中可以使用的項目

Apache 允許使用者對於目錄中使用 .htaccess 檔來控制權限,例如使用密碼保護、設定權限等。我們會在下一章中說明如何使用 .htaccess 來做密碼保護。

您可以將 AllowOverride 設成 All 或 None 來表示允許或不允許。或者,您也可以指定只允許下列某幾個功能:

* AuthConfig:允許設定不同的認證方式。
* FileInfo:允許設定檔案類型。
* Indexes:允許設定列出目錄索引時所要使用的選項。
* Limit:允許使用 Allow、Deny、Order 來設定存取權限。
* Options:允許設定目錄權限。

Order 設定 Allow 及 Deny 的順序

我們可以在目錄中設定允許 (Allow) 及拒絕 (Deny) 的規則先後順序,當二者抵觸時,以後者為主。以下列範例為例,我們先設定了 Order 為 Deny, Allow,再設定拒絕所有連線,但只允許 192.168.0.2 來存取 /home/www/mp3。


Order Deny,Allow
Deny from all
Allow from 192.168.0.2


UserDir 設定使用者個人網頁

在 mod_userdir 區段中,包含了使用者個人網頁的設定。使用者可以使用 http://www.example.com/~username 連到個人網頁。預設的設定是每個使用者都可以在自己的家目錄中建立一個名為 putblic_html 的目錄,該目錄就使用者的個人網頁目錄。UserDir 這個項目即定義了使用者個人網頁的路徑名稱。如果您覺得這個名稱太長,更改它的設定,但記得要修改 中的相對設定。

如果您不想讓使用者有個人網頁,可以將這個區段前面使用 # 註解起來。


UserDir public_html

UserDir disabled root toor daemon ..... pop www nobody mailnull smmsp

#
# Control access to UserDir directories. The following is an example
# for a site where these directories are restricted to read-only.
#

AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec

Order allow,deny
Allow from all


Order deny,allow
Deny from all



留言

這個網誌中的熱門文章

angular 如何Http 如何設定 CORS (Cross-Origin Resource Sharing)

Google Map 單車路徑計算坡度和角度小工具

Google URL Shortener API 快速教學