apache 用户认证
1.更改虚拟主机配置文件
vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
在<VirtualHost *:80>中添加字段
<Directory /data/www/abc.com/test> #加密目录
AllowOverride AuthConfig
AuthName " " #引号内为身份验证时的提示信息
AuthType Basic
AuthUserFile /data/.htpasswd #用户密码保存文件
require valid-user
</Directory>
2.创建目录和测试文件
mkdir -pv /data/www.abc.com/test/
echo “123” > /data/www/abc.com/test/1.txt
3.创建身份认证用户
htpasswd -c /data/.htpasswd user1 并输入密码
,第一次使用时加-c
htpasswd /data/.htpasswd user2
htpasswd命令基于包:httpd-tools-2.2.15-47.el6.centos.1.i686
4.重读配置文件
/usr/local/apache2.4/bin/apachectl –t
# 检查配置文件
/usr/local/apache2.4/bin/apachectl graceful
# 重新加载配置文件
5.测试:
# curl -x192.168.127.128:80 www.abc.com/test/1.txt
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>
[root@localhost apache2.4]# curl -x192.168.127.128:80 -uuser1:123456 www.abc.com/test/1.txt
“123”
还可以针对单个文件进行认证
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
<FilesMatch admin.php>
AllowOverride AuthConfig
AuthName "123.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</FilesMatch>
</VirtualHost>
最后更新于
这有帮助吗?