参考文章:https://cloud.tencent.com/developer/article/1026304

                    

操作过程中出现问题可查看这两篇文章做对比

 

准备

由于是首次搭建,需要完全参考别人的方法来尝试,所以安装的是centos7桌面版本,这样方便复制粘贴及使用目录结构查看文件

环境:VMware15.5 + CentOS-7-x86_64-DVD-2003

Centos的搭建这里就不进行介绍了,网上很多资料

 

知识准备

搭建OpenLDAP之前,至少要对LDAP有一定的了解,不然出现问题也不知所以然。

 

正文

所有的操作都在root权限下进行,如果是非管理员用户,执行命令需自行添加sudo

1.安装openldap

#安装相关包

yum install -y openldap openldap-clients openldap-servers

# 复制一个默认配置到指定目录下,并授权,这一步一定要做,然后再启动服务,不然生产密码时会报错

[root@localhost song]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

# 授权给ldap用户,此用户yum安装时便会自动创建

[root@localhost song]# chown -R ldap. /var/lib/ldap/DB_CONFIG

#启动服务

[root@localhost song]# systemctl start slapd

[root@localhost song]# systemctl enable slapd

Created symlink from /etc/systemd/system/multi-user.target.wants/slapd.service to /usr/lib/systemd/system/slapd.service.

#查看状态,正常启动则ok

[root@localhost song]# systemctl status slapd

centos ldap配置 centos安装ldap_apache

2.配置ldap服务

Ldap的配置,就是配置/etc/openldap/slapd.d目录下的cn=config,网上很多文章都是配置的ldap.conf,那是旧版的做法。而且这些文件最好不要直接进行编辑,使用ldapmodify命令进行修改,下面进行操作。

centos ldap配置 centos安装ldap_php_02

1)首先要设置ldap的管理员密码。生成经处理后的明文密码(这里我输入明文密码123456

其中 {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx 就是加密处理后的明文密码,之后会用到这个密码。

[root@localhost song]# slappasswd

New password:

Re-enter new password:

{SSHA}d8IiMNvp4JXzpLti7CPj+qG19NEMsBCm

3.添加ldap管理员密码

vim chrootpw.ldif

#内容如下,密码换成我们上面刚生成的密码

# specify the password generated above for "olcRootPW" section

dn: olcDatabase={0}config,cn=config

changetype: modify

add: olcRootPW

olcRootPW: {SSHA}wfYgDRZdRyxTuwVg1bzpXjjgAWGNYeN3

#导入文件

ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif

centos ldap配置 centos安装ldap_centos ldap配置_03

没有返回错误说明导入成功

 

我们来理解一下我们添加的内容

第一行,dn就是我们要修改的目标文件olcDatabase={0}config.ldif

第二行,类型,就是修改这个文件

第三行,add,增加配置项olcRootPW

第四行,增加配置项olcRootPW的内容,即密码

打开olcDatabase={0}config.ldif文件就看到增加了一项

centos ldap配置 centos安装ldap_php_04

3)导入基本Schema模式,关于schema的作用,看ldap知识的文档

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/cosine.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/nis.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/collective.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/corba.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/duaconf.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/dyngroup.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/inetorgperson.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/java.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/misc.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/openldap.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/pmi.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/ppolicy.ldif



4.设置域名

生成管理员密码,这个账户是访问ldap操作所使用的账户,之前步骤设置的是密码是本地客户顿的密码,如使用ldapadd操作添加数据等操作就是本地客户端操作,需要输入密码,当前步骤设置的密码是当使用api访问ldap服务时提供的,管理员账户和密码一起提供,如这里配置的账户为“cn=Manager,dc=songtest,dc=com”,执行操作和上面一样,密码为123456

centos ldap配置 centos安装ldap_apache_05

导入chdomain.ldif文件,这里我使用的域名是songtest.com

vim chdomain.ldif

#内容,其中olcAccess项是权限设置

# replace to your own domain name for "dc=***,dc=***" section

# specify the password generated above for "olcRootPW" section

dn: olcDatabase={1}monitor,cn=config

changetype: modify

replace: olcAccess

olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth"

  read by dn.base="cn=Manager,dc=songtest,dc=com" read by * none



dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcSuffix

olcSuffix: dc=songtest,dc=com



dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcRootDN

olcRootDN: cn=Manager,dc=songtest,dc=com



dn: olcDatabase={2}hdb,cn=config

changetype: modify

add: olcRootPW

olcRootPW: {SSHA}0ACzvtrFdA+ev5PxtsorO2AzxSpdCcTG



dn: olcDatabase={2}hdb,cn=config

changetype: modify

add: olcAccess

olcAccess: {0}to attrs=userPassword,shadowLastChange by dn="cn=Manager,dc=songtest,dc=com" write by anonymous auth by self write by * none

olcAccess: {1}to dn.base="" by * read

olcAccess: {2}to * by dn="cn=Manager,dc=songtest,dc=com" write by * read

#修改

[root@localhost song]# ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif

#修改了五项,提示五项都修改成功

SASL/EXTERNAL authentication started

SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth

SASL SSF: 0

modifying entry "olcDatabase={1}monitor,cn=config"

modifying entry "olcDatabase={2}hdb,cn=config"

modifying entry "olcDatabase={2}hdb,cn=config"

modifying entry "olcDatabase={2}hdb,cn=config"

modifying entry "olcDatabase={2}hdb,cn=config"

5)导入表数据

#打开文件

vim basedomain.ldif

#内容,以dc=songtest,dc=com为根创建目录

# replace to your own domain name for "dc=***,dc=***" section

dn: dc=songtest,dc=com

objectClass: top

objectClass: dcObject

objectclass: organization

o: Server Com

dc: songtest



dn: cn=Manager,dc=songtest,dc=com

objectClass: organizationalRole

cn: Manager

description: Directory Manager



dn: ou=People,dc=songtest,dc=com

objectClass: organizationalUnit

ou: People



dn: ou=Group,dc=songtest,dc=com

objectClass: organizationalUnit

ou: Group



#执行命令,需要输入密码,密码就是之前设置的管理员的密码,123456

ldapadd -x -D cn=Manager,dc=songtest,dc=com -W -f basedomain.ldif

写入成功

centos ldap配置 centos安装ldap_apache_06

关闭防火墙
[root@localhost song]# systemctl stop firewalld
[root@localhost song]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost song]# setenforce 0

这一步结束就能够访问到了

  1. 添加一个ldap用户
#生成密码

[root@localhost song]# slappasswd

New password:

Re-enter new password:

{SSHA}nolI/d+N5yGfM8jg0ilHlB7v+wGCdoBT

#打开文件

vim ldapuser.ldif

#内容,在我们上面创建的目录结构下添加用户

# create new

# replace to your own domain name for "dc=***,dc=***" section

dn: uid=kevin,ou=People,dc=songtest,dc=com

objectClass: inetOrgPerson

objectClass: posixAccount

objectClass: shadowAccount

cn: Kevin

sn: Linux

userPassword: {SSHA}nolI/d+N5yGfM8jg0ilHlB7v+wGCdoBT

loginShell: /bin/bash

uidNumber: 1000

gidNumber: 1000

homeDirectory: /home/kevin



dn: cn=kevin,ou=Group,dc=songtest,dc=com

objectClass: posixGroup

cn: Kevin

gidNumber: 1000

memberUid: kevin

#添加,输入密码

[root@localhost song]# ldapadd -x -D cn=Manager,dc=songtest,dc=com -W -f ldapuser.ldif

Enter LDAP Password:

adding new entry "uid=kevin,ou=People,dc=songtest,dc=com"

adding new entry "cn=kevin,ou=Group,dc=songtest,dc=com"
  1. 使用LDAP Admin工具进行ldap服务的管理

下载地址:http://www.ldapadmin.org/download/ldapadmin.html

1)下载解压,直接运行

centos ldap配置 centos安装ldap_vim_07

2)登录

centos ldap配置 centos安装ldap_vim_08

3)连接上之后就能够显示我们导入的数据了,LDAP admin还支持一些操作,这里就不进行介绍了

centos ldap配置 centos安装ldap_php_09

6.使用PHPLDAPadmin进行LDAP服务配置

PHPLDAPadmin的配置比较麻烦一下,这里直接用的别人,步骤没啥问题

1)安装和配置httpd

[root@test-vm002 ~]# yum -y install httpd

[root@test-vm002 ~]# rm -f /etc/httpd/conf.d/welcome.conf

[root@test-vm002 ~]# vim /etc/httpd/conf/httpd.conf         //修改下面几行内容

ServerName www.example.com:80                               //第96行

AllowOverride All                                           //第151行

DirectoryIndex index.html index.cgi index.php               //第164行

# add follows to the end                                    //添加这几行

# server's response header

ServerTokens Prod

# keepalive is ON

KeepAlive On



[root@test-vm002 ~]# systemctl start httpd

[root@test-vm002 ~]# systemctl enable httpd

Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

[root@test-vm002 ~]# vim /var/www/html/index.html

this is test page for httpd!!!
  1. 访问http://192.168.186.159

centos ldap配置 centos安装ldap_apache_10

3)安装php

[root@test-vm002 ~]# yum -y install php php-mbstring php-pear

[root@test-vm002 ~]# systemctl restart httpd

[root@test-vm002 ~]# systemctl enable httpd

[root@test-vm002 ~]# vim /var/www/html/index.php

<?php

phpinfo();

?>

访问http://192.168.186.159/index.php

centos ldap配置 centos安装ldap_php_11

4)安装phpLDAP admin

[root@test-vm002 ~]# wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[root@test-vm002 ~]# rpm -ivh epel-release-latest-7.noarch.rpm

[root@test-vm002 ~]# yum repolist                     #检查是否已添加至源列表

[root@test-vm002 ~]# yum --enablerepo=epel -y install phpldapadmin



登陆设置

[root@test-vm002 ~]# vim /etc/phpldapadmin/config.php

$servers->setValue('login','attr','dn');                    //第387行,打开这行的注释.使用用户名登陆

// $servers->setValue('login','attr','uid');                //注释掉这行。禁止使用uid登陆



[root@test-vm002 ~]# vim /etc/httpd/conf.d/phpldapadmin.conf

#

#  Web-based tool for managing LDAP servers

#



Alias /phpldapadmin /usr/share/phpldapadmin/htdocs

Alias /ldapadmin /usr/share/phpldapadmin/htdocs



<Directory /usr/share/phpldapadmin/htdocs>

  <IfModule mod_authz_core.c>

    # Apache 2.4

    Require all granted            //设置访问权限

  </IfModule>

  <IfModule !mod_authz_core.c>

    # Apache 2.2

    Order Deny,Allow

    Deny from all

    Allow from 127.0.0.1

    Allow from ::1

  </IfModule>

</Directory>



[root@test-vm002 ~]# systemctl restart httpd

[root@test-vm002 ~]# systemctl enable httpd

[root@test-vm002 share]# ps -ef|grep httpd

root       4150      1  0 01:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

apache     4151   4150  0 01:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

apache     4152   4150  0 01:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

apache     4153   4150  0 01:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

apache     4154   4150  0 01:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

apache     4155   4150  0 01:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

apache     4177   4150  0 01:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

apache     4184   4150  0 01:13 ?        00:00:00 /usr/sbin/httpd -DFOREGROUND

root       4188   3000  0 01:14 pts/1    00:00:00 grep --color=auto httpd



[root@test-vm002 share]# chown -R apache.apache /usr/share/phpldapadmin

5)配置完成,访问http:// 192.168.186.159/ldapadmin,登录即可

centos ldap配置 centos安装ldap_php_12