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

jed , 2006-11-15 15:24 , 服务器技术 , 评论(0) , 阅读(5790) , Via 本站原创 | |
本例中设定/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.
#

   Options Indexes FollowSymLinks
   AllowOverride None
   Order allow,deny
   Allow from all



   Options Indexes FollowSymLinks
   AllowOverride authconfig                    # allowoverride authconfig一行表示允许对/opt目录下的文件进行用户认证。
   Order allow,deny                                 #先允许,再禁止
   Allow from all                                     #允许所有计算机访问该文件夹


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即可
Tags:
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]