1:安裝htpasswd工具生成加密文件
安裝工具
# yum install httpd-tools
# htpasswd -cm /etc/httpd/passwd/password usera
New password:
Re-type new password:
Adding password for user usera
htpasswd命令選項介紹:
-c 創建一個加密文件 -n 不更新加密文件,只將apache htpasswd命令加密后的用戶名密碼顯示在屏幕上 -m 默認apache htpassswd命令采用MD5算法對密碼進行加密 -d apache htpassswd命令采用CRYPT算法對密碼進行加密 -p apache htpassswd命令不對密碼進行進行加密,即明文密碼 -s apache htpassswd命令采用SHA算法對密碼進行加密 -b 在apache htpassswd命令行中一並輸入用戶名和密碼而不是根據提示輸入密碼 -D 刪除指定的用戶
2:Apache 配置
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/xxx.com"
ServerName xxx.com
<FilesMatch admin.php> //admin.php就是單個文件的文件名
AllowOverride AuthConfig
AuthName "xxx.com user auth"
AuthType Basic
AuthUserFile /etc/httpd/passwd/password
require valid-user
### Require all granted // 2.4 注意要注釋掉
</FilesMatch>
</VirtualHost>
說明:
Require valid-user這個選項是所有passwd文件中的用戶都能進行認證,
如果不想所有用戶指向指定個別用戶,可以用命令 require user 用戶1 用戶2 用戶3
2:Nginx 配置
location / {
root /webroot;
index index.php index.html index.htm;
}
location /bbs {
alias /webroot/discurz;
index index.php index.html index.htm;
include /etc/nginx/conf.d/php;
auth_basic "Authorized user ? ";
auth_basic_user_file /etc/httpd/passwd/password;
}