参考:http://blogold.chinaunix.net/u3/95024/showart_1923130.html
http://www.cnblogs.com/BloodAndBone/archive/2011/02/28/1967136.html
http://bbs.linuxtone.org/forum-viewthread-tid-9366-highlight-ldap.html
环境:CentOS 5.4
db-5.1.25.tar.gz http://download.csdn.net/source/3325419
openldap-2.3.34.tgz http://download.csdn.net/source/3325379
httpd-2.2.15.tar.gz http://download.csdn.net/source/3325587
php-5.2.13.tar.gz http://download.csdn.net/source/3325572
ssl.ca-0.1.tar.gz http://download.csdn.net/source/3325546
phpldapadmin-1.2.0.5.tgz http://download.csdn.net/source/3325545
LDAP的工作原理:
LDAP是以服务器/客户端方式工作,目录服务将数据库软件的逻辑结构分为前端(客户端)和后端(服务端和仓库)
客户端------ 服务端 ------ 仓库
LDAP 逻辑结构图
系统自带以下两个db文件必须有,不然编译openldap-2.3.34还是会报错
db4-devel-4.3.29-10.el5
db4-4.3.29-10.el5
1、安装db:
tar xvf db-5.1.25.tar.gz
cd db-5.1.25
cd build_unix/
../dist/configure --prefix=/usr/local/BerkeleyDB
make
make install
vi /etc/ld.so.conf
/usr/local/Berkeley/lib
ldconfig
2、安装openldap
先设置环境不然安装openldap会报错
env CPPFLAGS="-I/usr/local/BerkeleyDB/include" LDFLAGS="-L/usr/lib -L/usr/local/BerkeleyDB/lib"
tar xvf openldap-2.3.34.tgz
cd openldap-2.3.34
./configure --prefix=/usr/local/openldap --enable-bdb
make depend
make
make test 可跳过
make install
3.安装apache
tar zxvf httpd-2.2.15.tar.gz
cd httpd-2.2.15
./configure --prefix=/usr/local/apache \
--with-included-apr \
--with-mpm=worker \
--with-ldap \
--enable-mods-static="deflate expires" \
--enable-mods-shared="ssl ldap authnz-ldap dav dav-fs dav-lock headers rewrite"
make && make install
开web账户
groupadd www
useradd -g www www -d /home/www -s /sbin/nologin
修改httpd.conf配置文件
cd /usr/local/apache/conf
vi httpd.conf
把用户改称www
启动apache
/usr/local/apache/bin/apachectl -k start
若是验证是否是否apache起来,则防火墙要放行
vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
重启防火墙
service iptables restart
浏览 出现 it works 就正确了
生成证书
tar -zxvf ssl.ca-0.1.tar.gz
mv ./ssl.ca-0.1 /usr/local/apache/conf/
cd /usr/local/apache/conf/ssl.ca-0.1
./new-root-ca.sh
下边输入密码其他的随便写生成了ca.key和ca.crt两个文件
下面还要为我们的服务器生成一个证书
./new-server-cert.sh server
这样就生成了server.csr和server.key这两个文件.
签署一下
./sign-server-cert.sh server
输入你之前设置的密码
然后y 就生成了server.crt
mv ./server.* ../ 把证书拷贝到conf的目录下
cd .. vi httpd.conf
去掉#Include conf/extra/httpd-ssl.conf的注释
如果证书放到其它地方去要修改httpd-ssl.com的配置
重起apache服务
/usr/local/apache/bin/apachectl -k restart
防火墙放行443端口,原先的80端口可以去掉
vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
重启防火墙
service iptables restart
在浏览网页 https://192.168.6.7
4、生成slapd密码
/usr/local/openldap/sbin/slappasswd
New password: 输入密码
Re-enter new password: 重复密码
生成后的加密密码: {SSHA}W/48LCkOe9rZUHMGyjD8VeHbMF4C3Szm
备注:相同的数字随机加密后会不同
5、修改openldap的配置文件
vi /usr/local/openldap/etc/openldap/slapd.conf
改动三行
suffix "dc=holy,dc=com"
rootdn "cn=root,dc=holy,dc=com"
rootpw {SSHA}W/48LCkOe9rZUHMGyjD8VeHbMF4C3Szm
启动ldap服务
cd /usr/local/openldap/libexec
./slapd
如果启动不成功可以用 ./slapd -d 1 启动debug模式。。将在屏幕打印启动信息
但是你重./slapd -d 1 启动debug模式时候会报错找不到DB_CONFIG的文件
cp /usr/local/openldap/var/openldap-data/DB_CONFIG.example /usr/local/openldap/var/openldap-data/DB_CONFIG
[root@localhost libexec]# ps -ef|grep slapd
root 27286 1 0 09:40 ? 00:00:00 ./slapd
root 27289 25603 0 09:40 pts/0 00:00:00 grep slapd
设置初始化
vi /tmp/init.ldif
dn: dc=holy,dc=com
dc: holy
objectClass: top
objectClass: domain
dn: ou=Hosts,dc=holy,dc=com
ou: Hosts
objectClass: top
objectClass: organizationalUnit
dn: ou=People,dc=holy,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit
账户密码信息
vi /tmp/user1.ldif
dn:cn=user1,ou=People,dc=holy,dc=com
cn: user1
sn: USER1
uid: user1
userPassword: user1
objectClass: inetOrgPerson
cd /usr/local/openldap/bin
ldapadd -x -D "cn=root,dc=holy,dc=com" -W -f /tmp/init.ldif
6、安装php和ldapphpadmin 管理工具
tar -zxvf php-5.2.13.tar.gz
cd php-5.2.13
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-gd \
--enable-gd-native-ttf \
--with-png-dir \
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--enable-mbstring \
--with-libxml-dir \
--enable-sockets \
--without-sqlite \
--without-pdo-sqlite \
--disable-ipv6 \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-ldap --with-gettext
make && make install
修改apache 配置文件
vi /usr/local/apache/conf/httpd.conf
DirectoryIndex index.html index.php
AddType application/x-httpd-php .php
cp php.ini-dist /usr/local/php/etc/php.ini
安装phpldapadmin
cd /qeedoodb/setup/
tar zxvf phpldapadmin-1.2.0.5.tgz
cd phpldapadmin-1.2.1/config
cp config.php.example config.php
cd /qeedoodb/setup/
mv phpldapadmin-1.2.0.5 /usr/local/apache/htdocs/phpldapadmin
重起apache
https://ip/phpldapadmin
就可以登陆管理界面了
7、apache用ldap做验证
vi /usr/local/apache/conf/httpd.conf
AuthType basic
AuthName "private area"
AuthBasicProvider ldap
AuthzLDAPAuthoritative off
AuthLDAPURL ldap://192.168.6.7:389/dc=holy,dc=com?uid?sub?(objectClass=*)
require ldap-user "user1" #只指定一个用户登录
#require valid-user #全部的用户登录都有效
mkdir /usr/local/apache/htdocs/test
vi index.php
ok
输入https://192.168.6.7/phpldapadmin 弹出窗口会提示你输入LDAP验证 账户和密码都是上面设置的user1
后续问题
用ACL来控制用户访问LDAP数据库的权限
access to attrs=userPassword
by anonymous auth
by dn="cn=admin,dc=holy,dc=com" write
by dn="uid=user1,ou=people,dc=holy,dc=com" write
by self write
access to *
by dn="cn=admin,dc=holy,dc=com" write
by self write
by * read
碰到的问题:DB版本和ldap有兼容性,另外环境也很重要
安装php遇到的问题如下:
1、configure: error: libjpeg.(a|so) not found.
解决方法:
yum -y install gd
yum -y install gd-devel
2、
/usr/local/apache/bin/apachectl -k restart
httpd: Syntax error on line 62 of /usr/local/apache/conf/httpd.conf: Cannot load /usr/local/apache/modules/libphp5.so into server: /usr/local/apache/modules/libphp5.so: cannot restore segment prot after reloc: Permission denied
解决方法:
a、关闭SELINUX的方法:
vi /etc/selinux/config 将SELINUX=enforcing 改成SELINUX=disabled 需要重启
这个方法可能会对服务器带来风险。
b、不关闭SELINUX的方法:
setenforce 0
chcon -c -v -R -u system_u -r object_r -t textrel_shlib_t /usr/local/apache/modules/libphp5.so
/usr/local/apache/bin/apachectl -k restart
setenforce 1
3、
bdb_db_open: Warning - No DB_CONFIG file found in directory /usr/local/openldap/var/openldap-data: (2)
解决方法:
cp /usr/local/openldap/var/openldap-data/DB_CONFIG.example /usr/local/openldap/var/openldap-data/DB_CONFIG
4、不能完成ldap_modify 操作。
LDAP said: Cannot modify object class
Error number: 0x45 (LDAP_NO_OBJECT_CLASS_MODS)
描述: ObjectClass modifications are not allowed.
为phpldapadmin目录赋权 chown -R www.www phpldapadmin/
5、
始终无法启动apache,系统管理报错:
Invalid command ‘AuthLDAPEnabled’, perhaps misspelled or defined by a module not included in the server configuration .
原来:“Apache2.0版中有关ldap验证的AuthLDAPEnabled指令取消了“,apache2.2以后的版本也取消了,但是在官方的手册中居然还能查得到
参考一下下配置:
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Deny from all
AuthName "LDAP auth"
AuthType Basic
AuthzLDAPAuthoritative off
#启用ldap验证功能
AuthBasicProvider ldap
AuthLDAPURL "ldap://192.168.6.7/dc=holy,dc=com?uid?sub?(objectClass=*)"
AuthLDAPBindDN "uid=root,dc=holy,dc=com"
Require valid-user
Satisfy any
本文转自 holy2009 51CTO博客,原文链接:http://blog.51cto.com/holy2010/576835