給目錄或者文件設置用戶驗證
再對應的主機配置文件中加入如下配置:
<Directory /data/www/admin.php>
AllowOverride AuthConfig
AuthName "The administrator authentication!"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</Directory>
Directory後的路徑爲要進行用戶驗證的目錄或文件,AuthName後跟自定義的驗證提示信息,AuthUserFile後跟驗證文件。
使用apache自帶的工具htpasswd創建驗證文件:
# /usr/local/apache2/bin/htpasswd -cm /data/.htpasswd username
New password:
Re-type new password:
Adding password for user username
# cat /data/.htpasswd
username:$apr1$wLVEgNIh$Ne.IU7FgjfPBZsP6SocZ11
參數-c表示創建一個新文件,-m表示對口令進行MD5加密,後面跟驗證文件的路徑,username是用戶名
# /usr/local/apache2/bin/apachectl -t
Syntax OK
檢查配置文件是正確
# /usr/local/apache2/bin/apachectl graceful
graceful表示重新加載配置文件