需要安装nginx、 ngx_http_auth_pam_module、pam-mysql、mysql
安装nginx和ngx_http_auth_pam_module
下载nginx和pam模块(http://web.iti.upv.es/~sto/nginx/)
编译安装(具体编译nginx看以前的文章)
./configure --with-pcre=../pcre --with-zlib=../zlib --with-http_ssl_module --with-openssl=../openssl --add-module=../auth_pam_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_flv_module --with-debug
--add-module=../auth_pam_module ,添加了ngx_http_auth_pam_module,存放在/root/auth_pam_module文件中
然后make
make install
make时可能会报错,
-o objs/addon/auth_pam_module/ngx_http_auth_pam_module.o \
../auth_pam_module/ngx_http_auth_pam_module.c
../auth_pam_module/ngx_http_auth_pam_module.c:13: fatal error: security/pam_appl.h: No such file or directory
../auth_pam_module/ngx_http_auth_pam_module.c
../auth_pam_module/ngx_http_auth_pam_module.c:13: fatal error: security/pam_appl.h: No such file or directory
该错误是因为没安装pam-dev
apt-get install libpam0g-dev
然后重新编译安装就可以了。
安装mysql和mysql pam
apt-get install mysql-server-5.1
apt-get install libpam-mysql
apt-get install libpam-mysql
下面在mysql中创建数据库,表及添加帐号密码数据
create database pam;
user pam;
create table user (userid varchar(16),passwd varchar(50),primary key (userid))type=innodb default charset=utf8;
GRANT SELECT ON pam.* TO pamuser@localhost IDENTIFIED BY '123456';
insert into user values ('abc',password('12345'));
数据库:pam
表:user
字段:userid, passwd
数据库访问账号:pamuser
数据库访问密码:123456
nginx访问帐号:abc
user pam;
create table user (userid varchar(16),passwd varchar(50),primary key (userid))type=innodb default charset=utf8;
GRANT SELECT ON pam.* TO pamuser@localhost IDENTIFIED BY '123456';
insert into user values ('abc',password('12345'));
数据库:pam
表:user
字段:userid, passwd
数据库访问账号:pamuser
数据库访问密码:123456
nginx访问帐号:abc
nginx访问密码:12345
下面配置pam模块访问mysql
在/etc/pam.d/ 下建一个文件nginx-mysql
/etc/pam.d/nginx-mysql
auth required /lib/security/pam_mysql.so user=pamuser passwd=123456 host=localhost db=pam table=user usercolumn=userid passwdcolumn=passwd crypt=2
account required /lib/security/pam_mysql.so user=pamuser passwd=123456 host=localhost db=pam table=user usercolumn=userid passwdcolumn=passwd crypt=2
account required /lib/security/pam_mysql.so user=pamuser passwd=123456 host=localhost db=pam table=user usercolumn=userid passwdcolumn=passwd crypt=2
配置中的crypt:
0=plain: 明码
1=Y: crypt()函数
2=mysql: mysql的 password()函数
3=md5: mysql的 md5()函数
下面配置nginx使用pam模块
location / {
auth_pam "mysql pam";
auth_pam_service_name "nginx-mysql";
}
auth_pam "mysql pam";
auth_pam_service_name "nginx-mysql";
}
重启nginx,就通过mysql身份验证可以访问了。