In some scenarios, the content of a site page needs to be viewed by a special authorized user. To implement this feature, you need to set up authentication users on Apache.
1. Edit the virtual host configuration
[[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. Increase user
[Email protected] ~]#/usr/local/apache2.4/bin/htpasswd-c-m/data/.htpasswd juispannew Password: # # "-C" =create "-M" =md5re-type New password: # # "/DATA/.HTPASSWD" = password storage path adding password For user Juispan[[email protected] ~]# cat/data/.htpasswd juispan: $apr 1$5uvkq8ux$8tkrftva0ueh7qtd6tzlz1
3. Check Reload
[Email protected] ~]#/usr/local/apache2.4/bin/apachectl-tsyntax ok[[email protected] ~]#/usr/local/apache2.4/bin/ Apachectl Graceful
4. Test verification
Native authentication:
[[email protected] ~]# curl -x127.0.0.1:80 abc.com<! doctype html public "-//ietf//dtd html 2.0//en" >
Remote Authentication:
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 "/>
Enter the correct username password to display the page content.
If you are not targeting an entire directory, but a single page, you can use FilesMatch to replace your directory, such as <filesmatch 1.php>.
▎ Reference configuration:
<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>
This article is from the "Gorilla grodd" blog, be sure to keep this source http://juispan.blog.51cto.com/943137/1952793
Lamp-apache user authentication