摘要:hellopasswd
httpd的用户认证
vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf #把123.com虚拟主机编辑为
hellopasswd
将第二个虚拟主机修改为如下
[root@localhost ~]# vi /usr/local/apache2.4/conf/httpd.conf 31 <VirtualHost *:80> 32 DocumentRoot "/data/wwwroot/111.com" 33 ServerName 111.com 34 ServerAlias www.example.com 35 <Directory /data/wwwroot/111.com> 36 AllowOverride AuthConfig 37 AuthName "111.com user auth" 38 AuthType Basic 39 AuthUserFile /data/.htpasswd 40 require valid-user 41 </Directory> 42 ErrorLog "logs/111.com-error_log" 43 CustomLog "logs/111.com-access_log" common 44 </VirtualHost>
-c创建密码文件
[root@localhost ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd user New password: Re-type new password: Adding password for user user
-m为MD5
[root@localhost ~]# cat /data/.htpasswd user:$apr1$nz4nSFEa$xXz28yuiXSuuWb9LLAPzJ0
若要添加用户,则不需要再次创建-c
[root@localhost ~]# /usr/local/apache2.4/bin/htpasswd -m /data/.htpasswd admin New password: Re-type new password: Adding password for user admin [root@localhost ~]# cat /data/.htpasswd user:$apr1$nz4nSFEa$xXz28yuiXSuuWb9LLAPzJ0 admin:$apr1$fb45.Cr9$Wejc/XMUd10Yl3aPhRvJm0
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful
访问111.com
[root@localhost ~]# curl -x 127.0.0.1:80 111.com <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> <h1>Unauthorized</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn\'t understand how to supply the credentials required.</p> </body></html>
出现了401,表示访问页面需要做用户验证
[root@localhost ~]# curl -x 127.0.0.1:80 111.com -I HTTP/1.1 401 Unauthorized Date: Sat, 04 Nov 2017 04:10:05 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 WWW-Authenticate: Basic realm="111.com user auth" Content-Type: text/html; charset=iso-8859-1
Windows下在hosts文件添加111.com,然后浏览器访问111.com
输入用户和密码就能访问111.com了
而Linux访问的用户认证则需要加-u指定用户和密码则就可以访问成功
[root@localhost ~]# curl -x 127.0.0.1:80 111.com -u user:123 hello!111.com [root@localhost ~]# curl -x 127.0.0.1:80 111.com -u user:123 -I HTTP/1.1 200 OK Date: Sat, 04 Nov 2017 04:15:43 GMT Server: Apache/2.4.29 (Unix) PHP/5.6.30 X-Powered-By: PHP/5.6.30 Content-Type: text/html; charset=UTF-8 针对某一个页面作用户认证 vi /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/data/wwwwroot/www.123/com" ServerName www.123.com <FilesMatch user.php> AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FileMatch> </VirtualHost> 针对某一个页面作用户认证
31 <VirtualHost *:80> 32 DocumentRoot "/data/wwwroot/111.com" 33 ServerName 111.com 34 ServerAlias www.example.com 35 # <Directory /data/wwwroot/111.com> 36 <FilesMatch user.php> 37 AllowOverride AuthConfig 38 AuthName "111.com user auth" 39 AuthType Basic 40 AuthUserFile /data/.htpasswd 41 require valid-user 42 </FilesMatch> 43 # </Directory> 44 ErrorLog "logs/111.com-error_log" 45 CustomLog "logs/111.com-access_log" common 46 </VirtualHost>
[root@localhost ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@localhost ~]# /usr/local/apache2.4/bin/apachectl graceful [root@localhost ~]# vi /data/wwwroot/111.com/user.php <?php echo "hello!user" ?>
[root@localhost ~]# curl -x 127.0.0.1:80 111.com hello!111.com [root@localhost ~]# curl -x 127.0.0.1:80 111.com/user.php <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>401 Unauthorized</title> </head><body> <h1>Unauthorized</h1> <p>This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn\'t understand how to supply the credentials required.</p> </body></html> [root@localhost ~]# curl -x 127.0.0.1:80 -u user:123 111.com/user.php hello!user
而Windows下的浏览器访问也同样可以
相关文章推荐
虚拟主机的专业参数,分别都是什么意思?2022-09-09
中非域名注册规则是怎样的?注册域名有什么用处? 2022-01-10
HostEase新年活动促销 美国/香港主机全场低至五折2021-12-28
HostGator下载完整备份教程分享2021-12-28
Flink中有界数据与无界数据的示例分析2021-12-28