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下的瀏覽器訪問也一樣能夠