在Linux操作系統(tǒng)下為Apache目錄添加密碼
實(shí)例一:
允許一個(gè)用戶訪問(wèn)一個(gè)目錄
1.使用htpasswd建立用戶文件:
$ htpasswd -c /data/httpd/secr/.htpasswd user
此程序會(huì)詢問(wèn)用戶“user1”的口令,你輸入“passwd”,兩次生效。
2.建立.htaccess文件
用“vi /log/image/www/huodong/erge/admin/.htaccess”命令建立.htaccess文件,并寫(xiě)入以下幾行:
AuthName My Friend Only (注:這個(gè)名字是任取的)
AuthType Basic
AuthUserFile /data/httpd/secr/.htpasswd
require user user
***設(shè)置文件.htpasswd和.htaccess的文件權(quán)限,確保Apache用戶有讀的權(quán)限這樣就完成了網(wǎng)頁(yè)密碼設(shè)置的工作。
3.修改httpd.conf文件:
用"vi /data/httpd/conf/httpd.conf"文件,并添加一下幾行:
Options Indexes FollowSymLinks
AllowOverride authconfig
Order allow,deny
Allow from all
#
#AccessFileName .htpaccess
#AllowOverride ALL
#
實(shí)例二:
允許一組用戶訪問(wèn)一個(gè)目錄。myfriend組中的m1與m2兩個(gè)用戶分別能使用口令“m1pass”和“m2pass”訪問(wèn)/home/httpd/html/backup/目錄中的頁(yè)面。
實(shí)現(xiàn)步驟:
1.使用htpasswd建立用戶文件,并在提示信息分別輸入m1與m2兩個(gè)用戶的口令“m1pass”和“m2pass”:
htpasswd -c /home/httpd/secr/.htpasswd m1
htpasswd -c /home/httpd/secr/.htpasswd m2
2.建立組文件,用“vi /home/httpd/secr/.htgroup”命令建立.htgroup文件,并寫(xiě)入下面一行:
myfriend:m1 m2
3.建立.htaccess文件,用“vi /home/httpd/html/backup/.htaccess”命令,并寫(xiě)入以下幾行:
AuthName My Friend Only
AuthType Basic
AuthUserFile /home/httpd/secr/.htpasswd
AuthGroupFile /home/httpd/secr/.htgroup
require group myfriend