nginx 认证 mysql_nginx基于mysql的身份验证

需要安装nginx、 ngx_http_auth_pam_module、pam-mysql、mysql

安装nginx和ngx_http_auth_pam_module

编译安装(具体编译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

该错误是因为没安装pam-dev

apt-getinstall libpam0g-dev

然后重新编译安装就可以了。

安装mysql和mysql pam

apt-getinstall mysql-server-5.1apt-getinstall libpam-mysql

下面在mysql中创建数据库,表及添加帐号密码数据

create database pam;

user pam;

create table user (userid varchar(16),passwd varchar(50),primary key (userid))type=innodbdefaultcharset=utf8;

GRANT SELECT ON pam.*TO pamuser@localhost IDENTIFIED BY'123456';

insert into user values ('abc',password('12345'));

数据库:pam

表:user

字段:userid, passwd

数据库访问账号:pamuser

数据库访问密码:123456nginx访问帐号: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=123456host=localhost db=pam table=user usercolumn=userid passwdcolumn=passwd crypt=2account required/lib/security/pam_mysql.so user=pamuser passwd=123456host=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";

}

重启nginx,就通过mysql身份验证可以访问了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值