Apache支持用戶認證
為了服務器的安全,通常用戶在請求訪問某個文件夾的時候,Apache可以要求用戶輸入有效的用戶名和登錄密碼
1、創建一個測試目錄
| [root@localhost cgi-bin]# mkdir /var/www/html/wj |
2、開啟認證功能,修改配置文件httpd.conf如下,(將html目錄的配置中none改為all)
| [root@localhost ~]# gedit /etc/httpd/conf/httpd.conf <Directory "/var/www/html"> # 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 All //默認是none,這里改為all # # Controls who can get stuff from this server. # Order allow,deny Allow from all </Directory> |
3、給需要認證的目錄添加配置,修改httpd.conf文件,添加如下內容
| <Directory "/var/www/html/wj"> //wj就是我們要認證的目錄 AllowOverride AuthConfig //這里必須使用AuthConfig Order allow,deny Allow from all </Directory> |
4、 給測試目錄設置用戶名和密碼
| [root@localhost wj]# htpasswd -c /var/www/html/wj/.htpasswd david //david就是創建的用戶名 New password: //這里需要輸入密碼,下面的是確認密碼 Re-type new password: Adding password for user david //創建成功 [root@localhost wj]# |
5、創建htaccess文件,並且增加內容
| [root@localhost wj]# vim .htaccess AuthUserFile /var/www/html/wj/.htpasswd AuthName "david" AuthType Basic require valid-user |
6、重啟Apache服務
| [root@localhost wj]# service httpd restart |
7、測試,在瀏覽器輸入“127.0.0.1/wj”,可以看到需要輸入密碼


做了一個Linux學習的平台,目前出來一個雛形,各位可以參考使用
鏈接:https://pan.baidu.com/s/1GOLVU2CbpBNGtunztVpaCQ 密碼:n7bk
