Linux系統下安裝配置 OpenLDAP + phpLDAPadmin
實驗環境:
作業系統:Centos 7.4
伺服器ip:192.168.3.41
執行使用者:root
網路環境:Internet
LDAP(輕量級目錄訪問協議)是一個能實現提供被稱為目錄服務的資訊服務,也是一套使用者認證體系系統;一般在大型企業、學校、政府單位使用的比較多,LDAP是由4部分組成,這4部分分別是slapd(獨立LDAP守護程序)、slurpd(獨立的LDAP更新複製守護程序)、LDAP協議庫、工具軟體和示例客戶端(phpLDAPadmin),目錄服務是一種特殊的資料庫系統,用來儲存使用者資訊的資料庫,讀寫速度非常快,擴充套件性非常強,可以實現與地方系統直接對接整合起來統一管理使用者資訊。
LDAP說起來也不簡單,但是ALDP在Linux應用範圍是比較廣泛的,如果想要深入的瞭解LDAP,建議去看下劉遄老師《Linux就該這麼學》這本教程,裡面解釋的非常詳細,也可以在百度輸入此書名去官網看,想要在Linux部署還是推薦這本書去系統的學習,對初學者還是很有版本的,這篇文章搭建ldap+phpldapadmin也是在Linux環境下執行的,所以還是需要Linux基礎才能看懂下面的配置步驟。
1、安裝OpenLDAP
[root@centos7 ~]# yum install openldap-servers openldap-clients -y
[root@centos7 ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@centos7 ~]# ll /var/lib/ldap/DB_CONFIG
-rw-r--r--. 1 root root 845 Aug 1 10:04 /var/lib/ldap/DB_CONFIG
[root@centos7 ~]# chown ldap. /var/lib/ldap/DB_CONFIG //授權配置檔案
[root@centos7 ~]# more /etc/passwd|grep ldap
ldap:x:55:55:OpenLDAP server:/var/lib/ldap:/sbin/nologin
[root@centos7 ~]# systemctl start slapd.service //啟動slapd服務
[root@centos7 ~]# systemctl enable slapd.service //設定開機自動啟動slapd服務
2、設定OpenLDAP管理員密碼
[root@centos7 ~]# slappasswd
New password: //password
Re-enter new password:
{SSHA}d5pkA0TU6b+8/kEoMIxJ59QofCLV
為“olcRootPW”指定上面生成的密碼
[root@centos7 ~]# vim chrootpw.ldif
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}d5pkA0TU6b+8/kEokgQeMIxJ59QofCLV
[root@centos7 ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={0}config,cn=config"
3、匯入基本模式
[root@centos7 ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=cosine,cn=schema,cn=config"
[root@centos7 ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=nis,cn=schema,cn=config"
[root@centos7 ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=inetorgperson,cn=schema,cn=config"
4、在LDAP DB上設定域名,生成目錄管理器密碼
[root@centos7 ~]# slappasswd
New password:
Re-enter new password:
{SSHA}Oq61fgUFW9+ItZboTaW1+VbLuAYst7zw
注意:下面配置檔案這裡得注意每一個屬性: 後必須有空格,但是值的後面不能有任何空格
[root@centos7 ~]# vim chdomain.ldif
# 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=srv,dc=world" read by * none
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=srv,dc=world
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=srv,dc=world
dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}Oq61fgUFW9+ItZboTaW1+VbLuAYst7zw
dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcAccess
olcAccess: {0}to attrs=userPassword,shadowLastChange by
dn="cn=Manager,dc=srv,dc=world" write by anonymous auth by self write by * none
olcAccess: {1}to dn.base="" by * read
olcAccess: {2}to * by dn="cn=Manager,dc=srv,dc=world" write by * read
[root@centos7 ~]# 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"
[root@centos7 ~]# vim basedomain.ldif
# replace to your own domain name for "dc=***,dc=***" section
dn: dc=srv,dc=world
objectClass: top
objectClass: dcObject
objectclass: organization
o: Server World
dc: Srv
dn: cn=Manager,dc=srv,dc=world
objectClass: organizationalRole
cn: Manager
description: Directory Manager
dn: ou=People,dc=srv,dc=world
objectClass: organizationalUnit
ou: People
dn: ou=Group,dc=srv,dc=world
objectClass: organizationalUnit
ou: Group
[root@centos7 ~]# ldapadd -x -D "cn=Manager,dc=srv,dc=world" -W -f basedomain.ldif
Enter LDAP Password: //輸入上面設定的目錄管理器密碼 password
adding new entry "dc=srv,dc=world"
adding new entry "cn=Manager,dc=srv,dc=world"
adding new entry "ou=People,dc=srv,dc=world"
adding new entry "ou=Group,dc=srv,dc=world"
[root@centos7 ~]# ldapsearch -x -b "cn=Manager,dc=srv,dc=world"
# extended LDIF
#
# LDAPv3
# base <cn=Manager,dc=srv,dc=world> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
# Manager, srv.world
dn: cn=Manager,dc=srv,dc=world
objectClass: organizationalRole
cn: Manager
description: Directory Manager
# search result
search: 2
result: 0 Success
# numResponses: 2
# numEntries: 1
5、設定Firewalld,如果未啟用防火牆關閉,忽略
[root@centos7 ~]# firewall-cmd --add-service=ldap --permanent
[root@centos7 ~]# firewall-cmd --reload
6、安裝並配置Apache
[root@centos7 ~]# yum install httpd-devel.x86_64 httpd.x86_64 -y
[root@centos7 ~]# mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf.bak
[root@centos7 ~]# vim /etc/httpd/conf/httpd.conf
# line 86: change to admin's email address
ServerAdmin [email protected]
# line 95: change to your server's name
ServerName www.srv.world:80
# line 151: change
AllowOverride All
# line 164: add file name that it can access only with directory's name
DirectoryIndex index.html index.cgi index.php
# add follows to the end //在尾部新增
# server's response header
ServerTokens Prod
# keepalive is ON
KeepAlive On
[root@centos7 ~]# systemctl start httpd.service
[root@centos7 ~]# systemctl enable httpd.service
[root@centos7 ~]# firewall-cmd --add-service=http --permanent //防火牆排除httpd服務,如果沒有啟用防火牆,此步驟可以忽略。
success
[root@centos7 ~]# firewall-cmd --reload //重新載入firewall
success
[root@centos7 ~]# vim /var/www/html/index.html //測試apache服務
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Test Page
</div>
</body>
</html>
測試:http://192.168.3.41/index.html
7、安裝PHP
[root@centos7 ~]# yum -y install php php-mbstring php-pear
[root@centos7 ~]# vim /etc/php.ini
#修改時區 878行
date.timezone = Asia/Shanghai
[root@centos7 ~]# systemctl restart httpd.service
[root@centos7 ~]# vim /var/www/html/index.php
<html>
<body>
<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
<?php
print Date("Y/m/d");
?>
</div>
</body>
</html>
php列印日期
測試:http://192.168.3.41/index.php
可以不安裝phpLDAPadmin工具,直接下載Windows系統下的LdapAdmin應用程式
8、安裝phpLDAPadmin
[root@centos7 ~]# yum install phpldapadmin.noarch -y
[root@centos7 ~]# vim /etc/phpldapadmin/config.php
397 $servers->setValue('login','attr','dn'); #取消397行註釋
398 //$servers->setValue('login','attr','uid'); #註釋398
[root@centos7 ~]# 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 local
#允許所有的請求訪問
Require all granted
#允許IP段訪問
#Require ip 10.0.0.0/24
</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@centos7 ~]# systemctl restart httpd.service
[root@centos7 ~]# systemctl status httpd.service
測試:http://192.168.3.41/ldapadmin/
如果是按照上面配置執行的操作,登入一直提示失敗,需要執行
[root@centos7 ~]# setsebool -P httpd_can_connect_ldap on
cn=Manager,dc=srv,dc=world
9、基本操作和使用
9.1、新增組
9.2、新增使用者
9.3、phpldapadmin的網站的apache配置檔案
# 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
#注意:/usr/share/phpldapadmin/htdocs 是phpldapadmin根目錄
<Directory /usr/share/phpldapadmin/htdocs>
<IfModule mod_authz_core.c>
# Apache 2.4
#只允許本地請求訪問
# Require local
#允許所有的請求訪問
Require all granted
#允許IP段訪問
#Require ip 192.168.3.0/24
</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>
這裡可以直接訪問phpldapadmin後臺,最好是通過apache做密碼驗證才能登入,這樣比較安全。
參考文獻:httpd配置認證才能訪問網站(原創實踐操作).note
10、禁止匿名使用者登入
# vim /root/ldap_disable_bind_anon.ldif
angetype: modify
add: olcDisallows
olcDisallows: bind_anon
dn: cn=config
changetype: modify
add: olcRequires
olcRequires: authc
dn: olcDatabase={-1}frontend,cn=config
changetype: modify
add: olcRequires
olcRequires: authc
[root@centos7 ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f ldap_disable_bind_anon.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "cn=config"
modifying entry "olcDatabase={-1}frontend,cn=config"
[root@centos7 ~]# systemctl