限制網頁存取可用 Apache 身分驗證模組(mod_auth)配合 .htaccess 實作基本認證。
AuthUserFile /var/www/.htpasswd
AuthGroupFile /dev/null
AuthName "Browser dialog box query"
AuthType Basic
require valid-user
AuthUserFile:密碼檔絕對路徑(建議放同目錄,Apache 預設拒絕存取 .ht* 開頭檔案)。AuthGroupFile /dev/null:關閉群組功能。AuthName:對話框提示文字。AuthType Basic:基本認證(預設)。require valid-user:允許密碼檔中所有合法用戶。htpasswd -c .htpasswd sam
# 新增/修改密碼省略 -c
htpasswd .htpasswd another_user
修改 httpd.conf,將目錄的 AllowOverride None 改為 AllowOverride AuthConfig 或移除該行。Apache 會向上遞迴尋找 .htaccess,可能影響效能,效能敏感場景需權衡。