shallow丿ovephp
将第二个虚拟主机修改成以下html
[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建立密码文件apache
[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
若要添加用户,则不须要再次建立-ccurl
[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.comide
[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.comui
输入用户和密码就能访问111.com了url
而Linux访问的用户认证则须要加-u指定用户和密码则就能够访问成功code
[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
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下的浏览器访问也一样能够