<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName www.111.com
<Directory /data/wwwroot/111.com> //指定認證的目錄
AllowOverride AuthConfig //這個至關於打開認證的開關
AuthName "111.com user auth" //自定義認證的名字,做用不大
AuthType Basic //認證的類型,通常爲Basic,其餘類型我沒用過
AuthUserFile /data/.htpasswd //指定密碼文件所在位置
require valid-user //指定須要認證的用戶爲所有可用用戶
</Directory>
</VirtualHost>php
[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf …… <VirtualHost *:80> # ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/data/wwwroot/abc.com" ServerName abc.com ServerAlias www.abc.com 123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtualHost *:80> # ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias 111.com <Directory /data/wwwroot/111.com> AllowOverride AuthConfig AuthName "111.com user auth" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </Directory> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost> [root@Dasoncheng ~]# /usr/local/apache2.4/bin/htpasswd -c -m /data/.htpasswd zhangsan New password: Re-type new password: Adding password for user zhangsan [root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@Dasoncheng ~]# curl -x192.168.60.11: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> [root@Dasoncheng ~]# curl -x192.168.60.11:80 111.com -I HTTP/1.1 401 Unauthorized ##遇到的第一個提示符401,須要驗證帳號; Date: Fri, 23 Feb 2018 06:31:34 GMT Server: Apache/2.4.27 (Unix) PHP/5.6.30 WWW-Authenticate: Basic realm="111.com user auth" Content-Type: text/html; charset=iso-8859-1 [root@Dasoncheng ~]# curl -x192.168.60.11:80 111.com -uzhangsan:p@ssw0rd 111.com [root@Dasoncheng ~]#
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName www.111.com
<FilesMatch admin.php>
AllowOverride AuthConfig
AuthName "111.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</FilesMatch>
</VirtualHost>html
[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf <VirtualHost *:80> # ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/data/wwwroot/111.com" ServerName www.111.com ServerAlias 111.com # <Directory /data/wwwroot/111.com> <FilesMatch admin.php> AllowOverride AuthConfig AuthName "Please pass the auth for this page" AuthType Basic AuthUserFile /data/.htpasswd require valid-user </FilesMatch> # </Directory> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" common </VirtualHost> [root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful [root@Dasoncheng ~]# vim /data/wwwroot/111.com/admin.php [root@Dasoncheng ~]# cat /data/wwwroot/111.com/admin.php <?php echo "Welcome to the page of admin\n" ?>
[root@Dasoncheng ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.60.11 www.abc.com www.111.com [root@Dasoncheng ~]# curl www.111.com 111.com [root@Dasoncheng ~]# curl www.111.com/admin.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@Dasoncheng ~]# curl www.111.com/admin.php -uzhangsan:p@ssw0rd Welcome to the page of admin