Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1137509
  • 博文数量: 153
  • 博客积分: 10576
  • 博客等级: 上将
  • 技术积分: 2137
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-02 21:01
文章分类

全部博文(153)

文章存档

2009年(43)

2008年(110)

分类: LINUX

2009-04-26 14:25:38

[原创]Apache2.2下的的MySQL用户认证模块的安装,配置详解
Apache来实现基本的用户身份认证有很多种方式,比如最常见的文本方式,但在负载很重的server上性能很差。本文旨在提供一
种利用mysql数据库的高性能的认证方式。
步骤一 安装Mysql
1、建立msyql用户组
[root@localhost ~]# grep mysql /etc/group
#查询系统中是否有mysql这个用户组,没有则添加。
[root@localhost ~]# groupadd mysql
#增加一个名为mysql的用户组
2、建立mysql用户
[root@localhost ~]# grep mysql /etc/passwd
#查询系统中是否有mysql这个用户,没有则添加。
[root@localhost ~]# useradd mysql -g mysql -M -s /sbin/nologin
#增加一个名为mysql的用户。
-g:指定新用户所属的用户组(group)
-M:不建立根目录
-s:定义其使用的shell,/sbin/nologin代表用户不能登录系统。
3、下载:mysql-5.0.56
可到mysql官方网站下载
[root@localhost ~] tar zfvx mysql-5.0.56.tar.gz
[root@localhost ~] cd mysql-5.0.56
4、设置编译器的编译参数
[root@localhost mysql-5.0.56]# ./configure --prefix=/usr/local/mysql --with-unix-socket-path=/tmp/mysql.sock --
localstatedir=/usr/local/mysql/data --with-charset=gbk --without-debug --enable-assembler --without-isam --with-
client-ldflags=-all-static --with-mysqld-ldflags=-all-static
#这些设置告诉编译器如何编译apache:
--prefix=/usr/local/mysql 
指定msyql安装目录
--with-unix-socket-path=/tmp/mysql.sock
这个是指定mysql服务器启动后,联机套接字文件所处的位置和文件名。
--localstatedir=/usr/local/mysql/data
指定mysql的数据库目录
--with-charset=gbk 
添加gbk中文字符支持
--without-debug
去除debug模式
--enable-assembler
使用一些字符函数的汇编版本
--without-isam
去掉isam表类型支持,现在很少用了,isam表是一种依赖平台的表
--with-client-ldflags=-all-static
--with-mysqld-ldflags=-all-static
4、编译和安装
[root@localhost mysql-5.0.56]# make
#“make”命令把源文件编译成可执行的二进制文件
[root@localhost mysql-5.0.56]# make install
#“make install”把二进制文件和配置文件安装在合适的目录下
6、初始化系统库
[root@localhost mysql-5.0.56]# ./scripts/mysql_install_db
7、其它设置
[root@localhost mysql-5.0.56]# cp support-files/my-medium.cnf /etc/my.cnf -fv
#注解:
-f,删除目标中同名的文件或目录,并且不给任何提示。
#还有一些其它的my.cnf配置文件。适合不同的应用规模。型式如my-*.cnf。根据自己的需要cp。 
[root@localhost mysql-5.0.56]# cp support-files/mysql.server /etc/init.d/mysqld
#添加到起动脚本
[root@localhost mysql-5.0.56]# chmod 700 /etc/init.d/mysqld
[root@localhost mysql-5.0.56]# cd /usr/local
[root@localhost local]# chmod 750 mysql -R    
#注解:
-R 递归改变目录及其内容的权限。
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql
#设置所有者,mysql起动时会以mysql用户的身份运行,这样可以提高系统的安全性。
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql/data
[root@localhost local]# cd /usr/local/mysql/libexec
[root@localhost libexec]# cp mysqld mysqld.old
[root@localhost libexec]# strip mysqld
[root@localhost libexec]# chkconfig --add mysqld
#添加一个服务
[root@localhost libexec]# chkconfig --level 345 mysqld on
#设置开机时起动
[root@localhost libexec]# service mysqld start
#启动mysql服务
[root@localhost libexec]# ln -s /usr/local/mysql/bin/mysql /sbin/mysql
[root@localhost libexec]# ln -s /usr/local/mysql/bin/mysqladmin /sbin/mysqladmin
[root@localhost libexec]# ln -s /usr/local/mysql/bin/mysqldump /sbin/mysqldump
[root@localhost libexec]# mysqladmin -uroot password "youpassword"
#设置root帐户的密码
[root@localhost libexec]# mysql -uroot -p
# 输入你设置的密码
mysql>use mysql;
mysql>delete from user where password="";
#删除用于本机匿名连接的空密码帐号
mysql>flush privileges;
mysql> show databases;
+----------+
| Database |
+----------+
| mysql    |
| test     |
+----------+
2 rows in set (0.08 sec)
#显示一下数据库,测试mysql是否安装正常。
mysql> quit
Bye

步骤二  安装Apache
1、下载:apache2.2.10
[root@localhost ~]# tar zfvx httpd-2.2.10.tar.gz
2  设置编译器的编译参数
[root@localhost ~]# cd httpd-2.2.10
[root@localhost httpd-2.2.10]# ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite=share --
enable-proxy=share --enable-proxy-ajp=share --enable-dav=share --enable-dav-fs
#注解:
--prefix=/usr/local/apache2 
设置Apache安装目录。这里设定安装在/usr/local/apache2下,今后如果要卸载或者升级Aapche时,直接删除这个目录即可。
--enable-so 
指定允许DSO(动态共享对像)
--enable-rewrite=share 
开启Rewrite支持,以实现URL静态化,建议开启。
--enable-dav-fs
开启WebDAV支持,svn服务器等需要。
#其它的额外设置请使用./configure --help来查看。
3  编译和安装:
[root@localhost httpd-2.2.10]# make; make install
#如果没有错误的话,那么Apache就已经安装在/usr/local/apache2目录中了
3、启动服务:
[root@localhost httpd-2.2.4]# /usr/local/apache2/bin/apachectl start
4、确定启动状:
[root@localhost apache2]# netstat -utl
tcp        0      0 *:http                      *:*                         LISTEN 
#看到上面这行就表示你的Apache已经启动。
#用浏览器访问,看到It works!,说明apache已经安装成功了.
步骤三  下载,编译,安装mod_auth_mysql模块
1  到下载最新版本,目前为3.0.0
[root@localhost ~]# tar zfvx mod_auth_mysql-3.0.0.tar.gz
[root@localhost ~]# cd mod_auth_mysql-3.0.0
接下来需要按照目录下BUILD文档里的说明组建模块。由于mysql使我们手动编译的,所以在此需要用参数告知apxs到哪里寻找库文件和头文件。
[root@localhost mod_auth_mysql-3.0.0]# /usr/local/apache2/bin/apxs -c -lmysqlclient -L /usr/local/mysql/lib/mysql
-I /usr/local/mysql/include/mysql -lm -lz mod_auth_mysql.c
回车执行命令发现提示错误如下:
mod_auth_mysql.c:518: error: (near initialization for 'mysql_auth_cmds[5].cmd_data')
mod_auth_mysql.c:522: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:522: error: initializer element is not constant
mod_auth_mysql.c:522: error: (near initialization for 'mysql_auth_cmds[6].cmd_data')
mod_auth_mysql.c:526: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:526: error: initializer element is not constant
mod_auth_mysql.c:526: error: (near initialization for 'mysql_auth_cmds[7].cmd_data')
mod_auth_mysql.c:530: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:530: error: initializer element is not constant
mod_auth_mysql.c:530: error: (near initialization for 'mysql_auth_cmds[8].cmd_data')
mod_auth_mysql.c:534: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:534: error: initializer element is not constant
mod_auth_mysql.c:534: error: (near initialization for 'mysql_auth_cmds[9].cmd_data')
mod_auth_mysql.c:538: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:538: error: initializer element is not constant
mod_auth_mysql.c:538: error: (near initialization for 'mysql_auth_cmds[10].cmd_data')
mod_auth_mysql.c:542: error: expected expression before 'mysql_auth_config_rec'
mod_auth_mysql.c:542: error: initializer element is not constant
mod_auth_mysql.c:542: error: (near initialization for 'mysql_auth_cmds[11].cmd_data')
(只列出部分错误信息)
苦苦的google,最后在上找到了一个补丁包。(?
func=detail&aid=1437139&group_id=60218&atid=493464)是mod_auth_mysql专门针对apache2.2 的一个补丁。
内容如下
 
 

--- orig/mod_auth_mysql.c 2005-06-22 12:17:45.000000000 -0400 +++ mod_auth_mysql.c 2006-02-22 21:16:19.000000000 -0500 @@ -206,7 +206,7 @@ #define SNPRINTF apr_snprintf #define PSTRDUP apr_pstrdup #define PSTRNDUP apr_pstrndup - #define STRCAT ap_pstrcat + #define STRCAT apr_pstrcat #define POOL apr_pool_t #include "http_request.h" /* for ap_hook_(check_user_id | auth_checker)*/ #include "ap_compat.h" @@ -237,7 +237,7 @@ #define SNPRINTF ap_snprintf #define PSTRDUP ap_pstrdup #define PSTRNDUP ap_pstrndup - #define STRCAT ap_pstrcat + #define STRCAT apr_pstrcat #define POOL pool #include #include "ap_sha1.h" @@ -589,87 +589,87 @@ static command_rec mysql_auth_cmds[] = { AP_INIT_TAKE1("AuthMySQLHost", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlhost), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlhost), OR_AUTHCFG, "mysql server host name"), AP_INIT_TAKE1("AuthMySQLPort", ap_set_int_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlport), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlport), OR_AUTHCFG, "mysql server port number"), AP_INIT_TAKE1("AuthMySQLSocket", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlsocket), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlsocket), OR_AUTHCFG, "mysql server socket path"), AP_INIT_TAKE1("AuthMySQLUser", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqluser), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqluser), OR_AUTHCFG, "mysql server user name"), AP_INIT_TAKE1("AuthMySQLPassword", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlpasswd), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlpasswd), OR_AUTHCFG, "mysql server user password"), AP_INIT_TAKE1("AuthMySQLDB", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlDB), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlDB), OR_AUTHCFG, "mysql database name"), AP_INIT_TAKE1("AuthMySQLUserTable", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlpwtable), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlpwtable), OR_AUTHCFG, "mysql user table name"), AP_INIT_TAKE1("AuthMySQLGroupTable", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlgrptable), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlgrptable), OR_AUTHCFG, "mysql group table name"), AP_INIT_TAKE1("AuthMySQLNameField", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlNameField), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlNameField), OR_AUTHCFG, "mysql User ID field name within User table"), AP_INIT_TAKE1("AuthMySQLGroupField", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlGroupField), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlGroupField), OR_AUTHCFG, "mysql Group field name within table"), AP_INIT_TAKE1("AuthMySQLGroupUserNameField", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlGroupUserNameField), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlGroupUserNameField), OR_AUTHCFG, "mysql User ID field name within Group table"), AP_INIT_TAKE1("AuthMySQLPasswordField", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlPasswordField), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlPasswordField), OR_AUTHCFG, "mysql Password field name within table"), AP_INIT_TAKE1("AuthMySQLPwEncryption", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlEncryptionField), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlEncryptionField), OR_AUTHCFG, "mysql password encryption method"), AP_INIT_TAKE1("AuthMySQLSaltField", ap_set_string_slot, - (void*) APR_XtOffsetOf(mysql_auth_config_rec, mysqlSaltField), + (void*) APR_OFFSETOF(mysql_auth_config_rec, mysqlSaltField), OR_AUTHCFG, "mysql salfe field name within table"), /* AP_INIT_FLAG("AuthMySQLKeepAlive", ap_set_flag_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlKeepAlive), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlKeepAlive), OR_AUTHCFG, "mysql connection kept open across requests if On"), */ AP_INIT_FLAG("AuthMySQLAuthoritative", ap_set_flag_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlAuthoritative), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlAuthoritative), OR_AUTHCFG, "mysql lookup is authoritative if On"), AP_INIT_FLAG("AuthMySQLNoPasswd", ap_set_flag_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlNoPasswd), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlNoPasswd), OR_AUTHCFG, "If On, only check if user exists; ignore password"), AP_INIT_FLAG("AuthMySQLEnable", ap_set_flag_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlEnable), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlEnable), OR_AUTHCFG, "enable mysql authorization"), AP_INIT_TAKE1("AuthMySQLUserCondition", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlUserCondition), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlUserCondition), OR_AUTHCFG, "condition to add to user where-clause"), AP_INIT_TAKE1("AuthMySQLGroupCondition", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlGroupCondition), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlGroupCondition), OR_AUTHCFG, "condition to add to group where-clause"), AP_INIT_TAKE1("AuthMySQLCharacterSet", ap_set_string_slot, - (void *) APR_XtOffsetOf(mysql_auth_config_rec, mysqlCharacterSet), + (void *) APR_OFFSETOF(mysql_auth_config_rec, mysqlCharacterSet), OR_AUTHCFG, "mysql character set to be used"), { NULL }

将内容保存的文件apache22.diff中,将apache22.diff复制到mod_auth_mysql.c同目录下,执行命令。

[root@localhost mod_auth_mysql-3.0.0]# patch -p0 < apache22.diff

打好补丁后,重新组建模块成功。

接下来安装模块。
[root@localhost mod_auth_mysql-3.0.0]# apxs -i mod_auth_mysql.la

然后把下面这行加入 httpd.conf
  LoadModule mysql_auth_module modules/mod_auth_mysql.so

步骤四  配置认证参数

1 添加测试用户

我们进入mysql,mysql>create database auth;

mysql>use auth;
mysql> create table mysql_auth (
-> user_name char(20) not null,
-> user_passwd char(25) );

再插入几条记录:

mysql> insert into mysql_auth values
('test',encrypt("123456"));
Query OK, 1 row affected (0.00 sec)
mysql> insert into mysql_auth values
('test2',encrypt("654321"));
Query OK, 1 row affected (0.00 sec)

2 修改httpd.conf

  找到需要认证的目录,将参数AllowOverride None,修改为AllowOverride authconfig

3 编辑.htaccess文件

  在需要被保护的目录里建立文件.htaccess
  修该内容如下

    AuthName "MySQL Auth Testing"
  AuthType Basic
  AuthUserFile /dev/null  #此处必须设置为/dev/null,否则提示错误(9)Bad file descriptor: Could not open password

file: (null)
  AuthBasicAuthoritative off
  AuthMySQLEnable On
  AuthMySQLHost localhost   #mysql服务器的地址
  AuthMySQLPort 3306        #mysql服务器端口
  AuthMySQLUser myuser      #连接mysql服务器的用户名
  AuthMySQLPassword mypass  #连接mysql服务器的密码
  AuthMySQLDB auth          #认证使用的数据库          
  AuthMySQLUserTable mysql_auth    #从指定表中查询数据
  AuthMySQLNameField user_name     #用户名字段
  AuthMySQLPasswordField user_passwd     #密码字段
    Require valid-user        #数据库中的所有用户都可以通过认证

保存文件,重启Apache server即可。

 

阅读(3404) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~