Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

PHP LDAP in Laravel fails with Can't contact LDAP server over TLS

ErrorCan't contact LDAP server

Short answer

PHP's LDAP client uses OpenLDAP's settings. If it does not trust the CA that signed your directory server's certificate, a TLS connection fails with Can't contact LDAP server. Point TLS_CACERT in ldap.conf at your CA file, restart PHP, and check that SSL or TLS mode and the port match.

  • Applies to Laravel 12, LdapRecord 3, PHP 8.3, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Rule out the certificate first: run the same connection from the server with the OpenLDAP tool, using the same host and port as the app.

    ldapsearch -x -H ldaps://ldap.example.com:636 -b "" -s base
  2. 2Match what you see, then jump to its fix

    1. 1The failure appears only with ldaps:// or StartTLS, and ldapsearch reports the same certificate problemThe client does not trust the CA that signed the server certificateFix
    2. 2You set a TLS option in PHP code and it has no effectTLS options are set after the connection already existsFix
    3. 3use_ssl and use_tls are both on, or use_tls is combined with port 636SSL, TLS and the port do not matchFix

What does the error look like?

  • Can't contact LDAP server
  • unable to get local issuer certificate

Cause 1 of 3

The client does not trust the CA that signed the server certificate

1Check

ldap.conf is where OpenLDAP-based clients, including PHP, read their trust settings. TLS_CACERT names the file with the Certificate Authorities the client will recognize.

cat /etc/ldap/ldap.conf
php --ri ldap

2Fix

Copy your domain CA certificate to the server and reference it in ldap.conf, then restart the web server or PHP-FPM so the change is picked up.

# /etc/ldap/ldap.conf (or /etc/openldap/ldap.conf)
TLS_CACERT /etc/ssl/certs/ca.pem
TLS_REQCERT hard

3Verify

Repeat the ldapsearch command; it now connects without a certificate error.

ldapsearch -x -H ldaps://ldap.example.com:636 -b "" -s base

Confirmed by ldaprecord.com

Did this fix it?

Cause 2 of 3

TLS options are set after the connection already exists

1Check

PHP requires TLS-related options to be set globally, by passing null as the link, and before ldap_connect or ldap_start_tls.

grep -rn "ldap_set_option\|LDAP_OPT_X_TLS" app config

2Fix

Set the option before connecting. In LdapRecord, put it in the connection's options array, which passes ldap_set_option constants.

'options' => [
    LDAP_OPT_X_TLS_REQUIRE_CERT => LDAP_OPT_X_TLS_HARD,
],

3Verify

Clear the cached config, then retry the login.

php artisan config:clear

Confirmed by php.net

Did this fix it?

Cause 3 of 3

SSL, TLS and the port do not match

1Check

LdapRecord allows only one of use_ssl or use_tls. With use_tls you must keep the default port 389, and use_ssl moves the default to 636.

grep -n "use_ssl\|use_tls\|port" config/ldap.php

2Fix

Pick one mode and the port that matches it.

'use_tls' => true,
'use_ssl' => false,
'port' => 389,

3Verify

Retry the connection from the app.

php artisan config:clear

Confirmed by ldaprecord.com

Did this fix it?

How do I stop it coming back?

  • Keep the directory's CA certificate in your server provisioning, next to ldap.conf.
  • Do not set TLS_REQCERT never in production; it stops the client checking the server certificate at all.
  • Restart PHP-FPM or the web server after every ldap.conf change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Security

Is my PHP application affected by PHP2Shell, and how do I mitigate it?

A scanner or advisory feed reports CVE-2026-109619 (RCE via stale global state) on your server

You are exposed when the PHP serving web requests is older than 8.2.35, 8.3.36, 8.4.27 or 8.5.12 on its branch, or when a distribution package is older than its vendor's fixed build.

6 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.