有點時候我們需要對網站的某些指定目錄進行驗證,輸入用戶名和密碼纔可以訪問。對於apache 配置如下:
1.修改 httpd.conf 中的虛擬主機配置:添加
<Directory "/var/www/web/test">
Options Indexes FollowSymLinks
AllowOverride AuthConfig
Order allow,deny
Allow from all
</Directory>
2.到 /var/www/web/test 目錄下增加.htaccess文件
AuthName "password,please"
AuthType Basic
AuthUserFile /var/www/web/test/.htpasswd
require valid-user
彈出窗口提示password,please 解析密碼文件爲 /var/www/web/test/.htpasswd
用.ht打頭,apache是不允許web訪問到的 valid-user密碼文件中匹配的用戶都通過
如果是 require test,則只有test用戶通過
3. 建立密碼文件並創建用戶
htpasswd -c /var/www/web/test/.htpasswd test 輸入兩次密碼 test用戶就創建好了
如果以後增加用戶 htpasswd /var/www/web/test/.htpasswd test1
修改密碼 htpasswd -m /var/www/web/test/.htpasswd test
再重啓apache即可生效。