標籤:apache
在某些情境下,網站頁面的內容需要特殊授權使用者才能查看。要實現這個功能,需要在Apache上做設定認證使用者。
1、編輯虛擬機器主機配置
[[email protected] ~]# vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf<VirtualHost *:80> DocumentRoot "/data/www/abc.com" ServerName abc.com <Directory /data/www/abc.com> ##指定認證的目錄 AllowOverride AuthConfig ##開啟認證開關 AuthName "abc.com user auth" ##定義認證的名字 AuthType Basic ##指定認證的類型 AuthUserFile /data/.htpasswd ##指定密碼檔案所在的位置 require valid-user ##指定需要認證的使用者 </Directory></VirtualHost>
2、增加使用者
[[email protected] ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd juispanNew password: ##“-c”=create “-m”=md5Re-type new password: ##“/data/.htpasswd”=密碼存放路徑Adding password for user juispan[[email protected] ~]# cat /data/.htpasswd juispan:$apr1$5UVKQ8Ux$8tkRftVA0ueh7qtD6tzlz1
3、檢查重新載入
[[email protected] ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK[[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful
4、測實驗證
本機驗證:
[[email protected] ~]# curl -x127.0.0.1:80 abc.com<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>401 Unauthorized</title> ##401 未認證</head><body><h1>Unauthorized</h1><p>This server could not verify that youare authorized to access the documentrequested. Either you supplied the wrongcredentials (e.g., bad password), or yourbrowser doesn‘t understand how to supplythe credentials required.</p></body></html>[[email protected] ~]# curl -x127.0.0.1:80 -ujuispan:hao123.com abc.comabc.com
遠端驗證:
650) this.width=650;" src="https://s2.51cto.com/wyfs02/M00/9D/78/wKioL1mAgwnDUNsGAABAFbCNgGQ191.png-wh_500x0-wm_3-wmp_4-s_1465511015.png" title="1.png" alt="wKioL1mAgwnDUNsGAABAFbCNgGQ191.png-wh_50" />
輸入正確的使用者名稱口令後即可顯示網頁內容。
如果針對的不是整個目錄,而是單個網頁,可以使用FilesMatch替換Directory,如<FilesMatch 1.php> 。
▎參考配置:
<VirtualHost *:80> DocumentRoot "/data/www/abc.com" ServerName www.abc.com <FilesMatch 1.php> AllowOverride AuthConfig AuthName "abc.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FilesMatch></VirtualHost>
本文出自 “Gorilla Grodd” 部落格,請務必保留此出處http://juispan.blog.51cto.com/943137/1952793
LAMP-Apache使用者認證