说明 |
使用基本认证 |
状态 |
基本(B) |
模块名 |
auth_basic_module |
源文件 |
mod_auth_basic.c |
兼容性 |
仅在 Apache 2.1 及以后的版本中可用 |
apache用户认证访问机制
1)编辑httpd.conf
添加修改一下黄色代码
<Directory "/home/test">
Options FollowSymLinks
AllowOverride ALL
AuthName "teddy's website" #描述信息,自定义吧
AuthType Basic #使用基本认证
AuthUserFile /home/test/htpasswd #指定用户名密码文件的路径(最好放到网站目录下,否则会提示500错误)
require valid-user #允许有效额用户访问
Order allow,deny
Allow from all
</Directory>
2)创建用户名密码文件
/application/apache/bin/htpasswd -bc /root/htpasswd frank 123456
[root@NFS ~]# cat /root/htpasswd
frank:$apr1$HEIaEsHU$q5EqLeNfDFv4kEP2tH2U0/
3)重启apache
[root@NFS ~]# /application/apache/bin/apachectl graceful
4)看效果
提示:为了服务器的性能,一般不推荐使用AllowOverride AuthConfig或者AllowOverride ALL,因为这会使服务器会不断的去寻找.htaccess,从而影响服务器的效能,一般我们把一些后台管理界面或者其他特殊目录可能需要加验证这个需求。
实现目录用户认证访问机制
在主配置文件中加入下面的代码,从而实现当用户要访问guestbook目录的时候,需要用用用户名密码
<Directory "/home/test/guestbook">
Options FollowSymLinks
AllowOverride ALL
AuthName "teddy's guestbook"
AuthType Basic
AuthUserFile /home/test/.htpasswd
require valid-user
Order allow,deny
Allow from all
</Directory>