比如在www.test.com/abc/下的文件,要在網頁上展示,又只允許管理員才能夠查看,
我們就可以去做一個用戶認證,需要去輸入用戶名和密碼,只有用戶名和密碼正確之後才能夠去訪問。
[root@No ~]# cd /data/www
[root@No www]# cd test/
[root@No test]# cp /etc/passwd ./1.txt
打開網頁,輸入www.test.com/test/1.txt是可以訪問的。
假如1.txt這個文件很重要,我們要在網頁上展示,只允許自己查看,我們就來做一個用戶認證。
編輯虛擬主機文件:
[root@No test]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
# ServerAdmin [email protected]
DocumentRoot "/data/www"
ServerName www.test.com
ServerAlias www.aaa.com
# ErrorLog "logs/dummy-host.example.com-error_log"
# CustomLog "logs/dummy-host.example.com-access_log" common
<Directory /data/www/test>
AllowOverride AuthConfig
AuthName "zhanghaomima"
AuthType Basic
AuthUserFile /data/.htpasswd #密碼文件
require valid-user
</Directory>
</VirtualHost>
:wq
[root@No test]# vim /etc/profile.d/path.sh
修改爲:
#!/bin/bash
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/apache2/bin
:wq
[root@No test]# source /etc/profile.d/path.sh
這樣apache也可以快速啓動,不用再輸入過長的命令。
生成密碼文件:
[root@No test]# htpasswd -c /data/.htpasswd user1 //user1爲用戶名
New password: //密碼爲123456
Re-type new password:
Adding password for user user1
如果在添加一個新的用戶,就不能使用htpasswd -c /data/.htpasswd user2了。
要把-c去掉,否則會覆蓋掉之前的文件。
[root@No test]# apachectl -t
Syntax OK
[root@No test]# apachectl graceful
//重新加載。不需要重啓。
然後在網頁上刷新之前的頁面,會看到以下提示:
箭頭所指的就是我們虛擬主機裏面的AuthName "zhanghaomima"
輸入用戶名:user1
密碼:123456
就可以訪問了。