本例中设定/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
apache使用.htaccess文件实现用户认证
最新推荐文章于 2021-04-03 16:33:19 发布