首先用htpasswd创建一个密码文件:比如文件名叫做my.passwd
/usr/apache/bin/htpasswd -c -b my.passwd myusername mypassword
如果增加帐号:
/home/apache/bin/htpasswd -b my.passwd anotherusername anotherpassword
将my.passwd上传到一个非发布路径下。
比如你的物理WEB根目录的上一级
/usr/lcoal/apache/conf/my.passwd
创建 .htaccess
AuthName "My Authorization Directory"
AuthType Basic
AuthUserFile /home/apache/conf/my.passwd
Require valid-user
将这个文件上传到你需要进行用户认证的目录:
/usr/local/apache/htdocs/admin/.htaccess
这样这个目录的访问就需要认证了。
结合虚拟目录例如:
vi /usr/local/apache/conf/httpd.conf
<VirtualHost *:80>
<Directory /home/test1>
Options FollowSymLinks
AllowOverride all //无目录身份验证为none
Order allow,deny
allow from all
</Directory>
DocumentRoot /home/test1
ServerName www.test1.com
</VirtualHost>
touch /home/test1/.htaccess //创建.htacces文件
AuthName "My Authorization Directory"
AuthType Basic
AuthUserFile /home/apache/conf/my.passwd //制定生成的文件位置
Require valid-user
生成.htaccess的过程是一样的
/usr/lcoal/apache/bin/htpasswd -c -b yourename yourpasswd