PHP 在 CentOS 7 上:LDAP 无法绑定到服务器
PHP on CentOS 7: LDAP could not bind to the server
我有以下代码
public function openConnection()
{
$this->ldapServerHandle = ldap_connect(ConfigH::getConfig()->ldap->host);
$bindDN = ConfigH::getConfig()->ldap->serviceAccount->bindDN;
if ($this->ldapServerHandle) {
$this->ldapBindHandle = ldap_bind(
$this->ldapServerHandle,
$bindDN,
ConfigH::getConfig()->ldap->serviceAccount->password
);
if (!$this->ldapBindHandle) {
$errorMsg = "LDAP::__construct(): Could not bind the service account ".$bindDN;
LoggerH::emergency($errorMsg);
throw new LDAPException($errorMsg);
}
} else {
$errorMsg = "LDAP::__construct(): Could not connect to the LDAP server ".ConfigH::getConfig()->ldap->host;
LoggerH::emergency($errorMsg);
throw new LDAPException($errorMsg);
}
}
问题
从今天早上开始我就遇到了这个让我头疼的错误:
Warning: ldap_bind(): Unable to bind to server: Can't contact LDAP server [...]
在 Windows 上一切正常,当我在 CentOS 生产服务器上执行代码时,它停止工作了。
我已经检查过的
- OpenLDAP 已安装并启用
- 可以从 CentOS 服务器访问 LDAP 服务器(Kerberos 在同一台机器上没有遇到问题)
我已经尝试过的
- 重新安装了
php-ldap
扩展程序
- 检查凭据和地址一百万次
附加信息
- ConfigH::getConfig()->ldap->host returns 类似 "adserver.ourcompany.com" 的东西,这是我们的 LDAP 服务器的地址
- ConfigH::getConfig()->ldap->serviceAccount->bindDN returns 一个有效的绑定 DN
- ConfigH::getConfig()->ldap->serviceAccount->password returns服务账号的密码
解决方案
谁用 CentOS 就得到 SELinux,是的。
在 Google(例如结果的第 4 页)和 Whosebug 中更深入地挖掘之后,我发现问题是由 SELinux 限制 httpd
通过某些端口进行通信引起的,尽管防火墙被配置为允许它,包括 LDAP 的。
要允许 httpd
通过这些端口进行通信,运行 以下命令
setsebool -P httpd_can_network_connect 1
(原解here(WhoIsRich的回答))
我有以下代码
public function openConnection()
{
$this->ldapServerHandle = ldap_connect(ConfigH::getConfig()->ldap->host);
$bindDN = ConfigH::getConfig()->ldap->serviceAccount->bindDN;
if ($this->ldapServerHandle) {
$this->ldapBindHandle = ldap_bind(
$this->ldapServerHandle,
$bindDN,
ConfigH::getConfig()->ldap->serviceAccount->password
);
if (!$this->ldapBindHandle) {
$errorMsg = "LDAP::__construct(): Could not bind the service account ".$bindDN;
LoggerH::emergency($errorMsg);
throw new LDAPException($errorMsg);
}
} else {
$errorMsg = "LDAP::__construct(): Could not connect to the LDAP server ".ConfigH::getConfig()->ldap->host;
LoggerH::emergency($errorMsg);
throw new LDAPException($errorMsg);
}
}
问题
从今天早上开始我就遇到了这个让我头疼的错误:
Warning: ldap_bind(): Unable to bind to server: Can't contact LDAP server [...]
在 Windows 上一切正常,当我在 CentOS 生产服务器上执行代码时,它停止工作了。
我已经检查过的
- OpenLDAP 已安装并启用
- 可以从 CentOS 服务器访问 LDAP 服务器(Kerberos 在同一台机器上没有遇到问题)
我已经尝试过的
- 重新安装了
php-ldap
扩展程序 - 检查凭据和地址一百万次
附加信息
- ConfigH::getConfig()->ldap->host returns 类似 "adserver.ourcompany.com" 的东西,这是我们的 LDAP 服务器的地址
- ConfigH::getConfig()->ldap->serviceAccount->bindDN returns 一个有效的绑定 DN
- ConfigH::getConfig()->ldap->serviceAccount->password returns服务账号的密码
解决方案
谁用 CentOS 就得到 SELinux,是的。
在 Google(例如结果的第 4 页)和 Whosebug 中更深入地挖掘之后,我发现问题是由 SELinux 限制 httpd
通过某些端口进行通信引起的,尽管防火墙被配置为允许它,包括 LDAP 的。
要允许 httpd
通过这些端口进行通信,运行 以下命令
setsebool -P httpd_can_network_connect 1
(原解here(WhoIsRich的回答))