如何在 Apache (Debian 8) 上使用 mod_authn_dbd 和 MySQL 密码保护目录
本教程适用于这些操作系统版本
- Debian 8(杰西)
- Debian 6(挤压)
在此页
- 1 条初步说明
- 2 安装 MySQL 或 MariaDB
- 3 配置 mod_authn_dbd
- 4 个链接
本指南解释了如何在 Debian 8 (Jessie) 服务器上的 Apache2 上使用 mod_authn_dbd 对 Web 目录(使用来自 MySQL 数据库的用户)进行密码保护。它是 mod_auth 提供的纯文本密码文件的替代方案,允许您使用普通的 SQL 语法来创建/修改删除用户。您还可以配置 mod_authn_dbd 以针对现有 MySQL 用户表进行身份验证。 apache mod_authn_dbd 是 mod_auth_mysql 的替代品。
1 初步说明
我在这里使用虚拟主机 http://www.example.com,虚拟主机配置文件 /etc/apache2/sites-available/example.com.vhost 和文档根 /var/www/www.example.com/web。我想在本教程中使用密码保护目录 /var/www/example.com/web/protecteddir(转换为 http://www.example.com/protecteddir/)。
如果您尚未安装 Apache,则可以将本教程用于基本 LAMP 服务器。
2 安装 MySQL 或 MariaDB
我将在这里使用 MariaDB,它是 MySQL 的一个分支,而不是 MySQL。但如果您愿意,MySQL 也能正常工作。要安装 MariaDB,我们运行:
apt-get -y install mariadb-server mariadb-client
系统将要求您提供 MySQL root 用户的密码:
MariaDB \root\ 用户的新密码:<-- yourrootsqlpassword
MariaDB \root\ 用户的重复密码:<-- yourrootsqlpassword
安装 DBD MySQL 模块:
apt-get install libaprutil1-dbd-mysql
然后,启用 mod_authn_dbd 模块:
a2enmod dbd
a2enmod authn_dbd
authn_socache
重启阿帕奇:
service apache2 restart
3 配置 mod_authn_dbd
您可以在此处的 Apache 文档中找到 mod_authn_dbd 的文档 http://httpd.apache.org/docs/current/mod/mod_authn_dbd.html。
读取这两个文件后,我们创建一个名为 examplecomdb 的 MySQL 数据库,我们将在其中创建包含我们的用户和密码的表 mysql_auth。除此之外,我们还创建了 MySQL 用户 examplecom_admin - 此用户稍后将由 mod_auth_mysql 用于连接到 MySQL:
mysqladmin -u root -p create examplecomdb
mysql -u root -p
GRANT SELECT, INSERT, UPDATE, DELETE ON examplecomdb.* TO 'examplecom_admin'@'localhost' IDENTIFIED BY 'examplecom_admin_password';
GRANT SELECT, INSERT, UPDATE, DELETE ON examplecomdb.* TO 'examplecom_admin'@'localhost.localdomain' IDENTIFIED BY 'examplecom_admin_password';
FLUSH PRIVILEGES;
(将 examplecom_admin_password 替换为您选择的密码。)
USE examplecomdb;
create table mysql_auth (
username varchar(255) not null,
passwd varchar(255),
groups varchar(255),
primary key (username)
);
(当然,您也可以使用保存用户凭据的现有表,也可以在表中添加其他字段,例如定义用户是否处于活动状态的字段。)
现在我们将用户 test 插入到我们的 mysql_auth 表中,密码为 test;此用户属于组 testgroup。
密码必须经过哈希处理,我将在这里使用 SHA1 哈希,可以在 Linux shell 上使用 htpasswd 命令创建哈希:
htpasswd -bns test test
结果是这样的:
test:{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=
第一部分是用户名 \test\,由 \:\ 分隔,然后是散列密码。我们需要散列密码 \{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=\ 只是为了将其插入我们的用户数据库。 MySQL查询是这样的:
INSERT INTO `mysql_auth` (`username`, `passwd`, `groups`) VALUES('test', '{SHA}qUqP5cyxm6YcTAhz05Hph5gvu9M=', 'testgroup');
然后我们离开 MySQL shell:
quit
mod_authn_dbd 的配置包含在 vhost 文件中,它可能不会添加到 .htaccess 文件中。因此我们编辑vhost文件,在文件末尾添加如下配置:
nano /etc/apache2/sites-available/example.com.vhost
[...]
# mod_dbd configuration
DBDriver mysql
DBDParams "dbname=examplecomdb user=examplecom_admin pass=examplecom_admin_password"
DBDMin 4
DBDKeep 8
DBDMax 20
DBDExptime 300
<Directory "/var/www/example.com/web/protecteddir">
# mod_authn_core and mod_auth_basic configuration
# for mod_authn_dbd
AuthType Basic
AuthName "My Server"
# To cache credentials, put socache ahead of dbd here
AuthBasicProvider socache dbd
# Also required for caching: tell the cache to cache dbd lookups!
AuthnCacheProvideFor dbd
AuthnCacheContext my-server
# mod_authz_core configuration
Require valid-user
# mod_authn_dbd SQL query to authenticate a user
AuthDBDUserPWQuery "SELECT passwd FROM mysql_auth WHERE username = %s"
</Directory>
重新加载阿帕奇:
service apache2 reload
如果您的 MySQL 表中有其他字段定义是否允许用户登录(例如,名为 active 的字段),您可以将其添加到 SQL 用户查询中,如下所示:
[...]
AuthDBDUserPWQuery "SELECT passwd FROM mysql_auth WHERE username = %s and active = 'yes'"
[...]
require valid-user 指令使得 mysql_auth 表中列出的每个用户只要提供正确的密码就可以登录。如果您只希望某些用户被允许登录,您可以使用类似
[...]
require user jane joe
[...]
反而。如果你只希望某些组的成员被允许登录,你可以使用这样的东西:
[...]
require group testgroup
[...]
就是这样!现在尝试访问 http://www.example.com/protecteddir/,你应该被要求输入用户名和密码:


4个链接
- 阿帕奇:http://httpd.apache.org/
- Debian:http://www.debian.org/
- mod_authn_dbd:http://httpd.apache.org/docs/current/mod/mod_authn_dbd.html