apache使用.htaccess文件实现用户认证

本例中设定/opt的虚拟目录,并采用.htaccess文件实现对/opt虚拟目录的用户认证

1.修改/etc/httpd/conf/httpd.conf

DocumentRoot "/backup"
Alias /html/    "/var/www/html"
Alias /opt      "/opt"                               #定义/opt虚拟目录
 
#
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the "default" to be a very restrictive set of
# features.
#
<Directory "/backup">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
 
<Directory "/opt">
    Options Indexes FollowSymLinks
    AllowOverride authconfig                    # allowoverride authconfig一行表示允许对/opt目录下的文件进行用户认证。 
    Order allow,deny                                 #先允许,再禁止
    Allow from all                                     #允许所有计算机访问该文件夹
</Directory>

2. 在限制访问的目录/opt下建立一个文件.htaccess,其内容如下:
[root@test7 opt]# more .htaccess
AuthName "test"
AuthType basic
AuthUserFile /etc/httpd/test.txt
require valid-user                                           # 在AuthUserFile指定的文件中任何用户都可以访问

3. 利用Apache附带的程序htpasswd,生成包含用户名和密码的文本文件:/etc/httpd/test.txt,系统自动生成每行内容格式为“用户名:密码”。
[root@test7 httpd]# htpasswd -c test.txt test
New password:
Re-type new password:
Adding password for user test
[root@test7 httpd]# more /etc/httpd/test.txt
test:wPJ8HcOHa21Fo

4.直接用浏览器访问http://ip地址/opt即可

参考url: http://www.phpe.net/articles/236.shtml
             https://support.iap.ac.cn/net/ApacheManual/zh-cn/programs/htpasswd.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值