LDAP crashing if updated with a LDAP account

Nextcloud version: 16.0.3
Operating system and version: Ubuntu 18.04.3 LTS
Apache or nginx version: nginx/1.14.0
PHP version: 7.3

The issue you are facing:
When ever the LDAP configuration is edited by a LDAP account with Admin access, the whole server crashes and genereate: 504 Gateway Time-out

Is this the first time you’ve seen this error? (Y/N):
No, when I installed our Nextcloud 2 years ago (Back in version 12.0).

Steps to replicate it:

1. Log in to an account that is from LDAP that even have Admin access
2. Go to Settings -> LDAP / AD integration
3. Change something in the LDAP / AD integration settings
4. Go out of the LDAP configuation (And now the whole webserver is crashed)

How to “fix” it shortly
1. Go to the terminal of the server, re-enable php (php7.3-fpm in this case)
2. It will now work to log in to the server with a local account (Not LDAP connect)
3. If you go to Users or LDAP / AD integration, the website will crash again (even with a local account)
4. If you now try to re-enable php it wont work, so if you try with restart it will work again, but will also crash if you enter the Users or LDAP / AD integration settings

NOTE:
I have tried to only reboot the server with no luck, so it has to be some file or in the database that is blocking everything that is connected to the LDAP setting.

The output of your Nextcloud log in Admin > Logging:

{"reqId":"4Ldz0JE8NwN28q99eCiI","level":3,"time":"2019-11-18T08:37:42+00:00","remoteAddr":"","user":"--","app":"files","method":"","url":"--","message":{"Exception":"OC\\ServerNotAvailableException","Message":"Lost connection to LDAP server.","Code":0,"Trace":[{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/LDAP.php","line":377,"function":"processLDAPError","class":"OCA\\User_LDAP\\LDAP","type":"->","args":[null]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/LDAP.php","line":300,"function":"postFunctionCall","class":"OCA\\User_LDAP\\LDAP","type":"->","args":[]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/LDAP.php","line":203,"function":"invokeLDAPMethod","class":"OCA\\User_LDAP\\LDAP","type":"->","args":["*** sensitive parameters replaced ***"]},{"function":"search","class":"OCA\\User_LDAP\\LDAP","type":"->","args":["*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***"]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/Access.php","line":1076,"function":"call_user_func_array","args":[[{"__class__":"OCA\\User_LDAP\\LDAP"},"*** sensitive parameter replaced ***"],["*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***"]]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/Access.php","line":1097,"function":"OCA\\User_LDAP\\{closure}","class":"OCA\\User_LDAP\\Access","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/Access.php","line":1132,"function":"invokeLDAPMethod","class":"OCA\\User_LDAP\\Access","type":"->","args":["*** sensitive parameters replaced ***"]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/Access.php","line":1286,"function":"executeSearch","class":"OCA\\User_LDAP\\Access","type":"->","args":["*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***",1,null]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/Access.php","line":1014,"function":"search","class":"OCA\\User_LDAP\\Access","type":"->","args":["*** sensitive parameter replaced ***","*** sensitive parameter replaced ***","*** sensitive parameter replaced ***",1,null]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/Group_LDAP.php","line":498,"function":"searchGroups","class":"OCA\\User_LDAP\\Access","type":"->","args":["*** sensitive parameter replaced ***","*** sensitive parameter replaced ***",1]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/Group_LDAP.php","line":628,"function":"primaryGroupID2Name","class":"OCA\\User_LDAP\\Group_LDAP","type":"->","args":["*** sensitive parameter replaced ***"]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/Group_LDAP.php","line":663,"function":"getUserPrimaryGroup","class":"OCA\\User_LDAP\\Group_LDAP","type":"->","args":["*** sensitive parameter replaced ***"]},{"file":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/Group_Proxy.php","line":123,"function":"getUserGroups","class":"OCA\\User_LDAP\\Group_LDAP","type":"->","args":["adiv"]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/Group\/Manager.php","line":280,"function":"getUserGroups","class":"OCA\\User_LDAP\\Group_Proxy","type":"->","args":["adiv"]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/Group\/Manager.php","line":267,"function":"getUserIdGroups","class":"OC\\Group\\Manager","type":"->","args":["adiv"]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/Group\/Manager.php","line":328,"function":"getUserGroups","class":"OC\\Group\\Manager","type":"->","args":[{"__class__":"OC\\User\\User"}]},{"file":"\/var\/www\/html\/nextcloud\/apps\/groupfolders\/lib\/Folder\/FolderManager.php","line":441,"function":"getUserGroupIds","class":"OC\\Group\\Manager","type":"->","args":[{"__class__":"OC\\User\\User"}]},{"file":"\/var\/www\/html\/nextcloud\/apps\/groupfolders\/lib\/Mount\/MountProvider.php","line":83,"function":"getFoldersForUser","class":"OCA\\GroupFolders\\Folder\\FolderManager","type":"->","args":[{"__class__":"OC\\User\\User"},5]},{"file":"\/var\/www\/html\/nextcloud\/apps\/groupfolders\/lib\/Mount\/MountProvider.php","line":87,"function":"getFoldersForUser","class":"OCA\\GroupFolders\\Mount\\MountProvider","type":"->","args":[{"__class__":"OC\\User\\User"}]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/Files\/Config\/MountProviderCollection.php","line":103,"function":"getMountsForUser","class":"OCA\\GroupFolders\\Mount\\MountProvider","type":"->","args":[{"__class__":"OC\\User\\User"},{"__class__":"OC\\Files\\Storage\\StorageFactory"}]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/Files\/Filesystem.php","line":447,"function":"addMountForUser","class":"OC\\Files\\Config\\MountProviderCollection","type":"->","args":[{"__class__":"OC\\User\\User"},{"__class__":"OC\\Files\\Mount\\Manager"}]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/Files\/Filesystem.php","line":376,"function":"initMountPoints","class":"OC\\Files\\Filesystem","type":"::","args":["adiv"]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/legacy\/util.php","line":309,"function":"init","class":"OC\\Files\\Filesystem","type":"::","args":["adiv","\/adiv\/files"]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/Files\/Utils\/Scanner.php","line":106,"function":"setupFS","class":"OC_Util","type":"::","args":["adiv"]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/Files\/Utils\/Scanner.php","line":142,"function":"getMounts","class":"OC\\Files\\Utils\\Scanner","type":"->","args":[""]},{"file":"\/var\/www\/html\/nextcloud\/apps\/files\/lib\/BackgroundJob\/ScanFiles.php","line":88,"function":"backgroundScan","class":"OC\\Files\\Utils\\Scanner","type":"->","args":[""]},{"file":"\/var\/www\/html\/nextcloud\/apps\/files\/lib\/BackgroundJob\/ScanFiles.php","line":112,"function":"runScanner","class":"OCA\\Files\\BackgroundJob\\ScanFiles","type":"->","args":[{"__class__":"OC\\User\\User"}]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/BackgroundJob\/Job.php","line":61,"function":"run","class":"OCA\\Files\\BackgroundJob\\ScanFiles","type":"->","args":[null]},{"file":"\/var\/www\/html\/nextcloud\/lib\/private\/BackgroundJob\/TimedJob.php","line":55,"function":"execute","class":"OC\\BackgroundJob\\Job","type":"->","args":[{"__class__":"OC\\BackgroundJob\\JobList"},{"__class__":"OC\\Log"}]},{"file":"\/var\/www\/html\/nextcloud\/cron.php","line":123,"function":"execute","class":"OC\\BackgroundJob\\TimedJob","type":"->","args":[{"__class__":"OC\\BackgroundJob\\JobList"},{"__class__":"OC\\Log"}]}],"File":"\/var\/www\/html\/nextcloud\/apps\/user_ldap\/lib\/LDAP.php","Line":338,"CustomMessage":"--"},"userAgent":"--","version":"16.0.3.0"}
{"reqId":"4Ldz0JE8NwN28q99eCiI","level":3,"time":"2019-11-18T08:37:42+00:00","remoteAddr":"","user":"--","app":"PHP","method":"","url":"--","message":"Error while sending QUERY packet. PID=58230 at \/var\/www\/html\/nextcloud\/3rdparty\/doctrine\/dbal\/lib\/Doctrine\/DBAL\/Driver\/PDOConnection.php#107","userAgent":"--","version":"16.0.3.0"}

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => '*Removed*',
  'passwordsalt' => '*Removed*',
  'secret' => '*Removed*',
  'trusted_domains' =>
  array (
    0 => '*Removed*',
    1 => '*Removed*,
    2 => '*Removed*',
  ),
  'datadirectory' => '/var/www/html/nextcloud/data',
  'htaccess.RewriteBase' => '/',
  'dbtype' => 'mysql',
  'version' => '16.0.3.0',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'nextclouduser',
  'dbpassword' => '*Removed*',
  'installed' => true,
  'ldapIgnoreNamingRules' => false,
  'ldapProviderFactory' => '\\OCA\\User_LDAP\\LDAPProviderFactory',
  'ldapUserCleanupInterval' => 60,
  'overwrite.cli.url' => '*Removed*',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'mail_smtpmode' => 'smtp',
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtpsecure' => 'ssl',
  'mail_from_address' => 'noreplynextcloud',
  'mail_domain' => '*Removed*',
  'mail_smtphost' => '*Removed*',
  'mail_smtpport' => '465',
  'mail_smtpauth' => 1,
  'mail_smtpname' => '*Removed*',
  'mail_smtppassword' => '*Removed*',
  'maintenance' => false,
  'log_type' => 'owncloud',
  'loglevel' => 3,
  'logfile' => '/var/log/nextcloud/nextcloud.log',
  'theme' => '',
  'mysql.utf8mb4' => true,
);

The output of your Apache/nginx/system log in /var/log/____:

2019/11/18 08:59:37 [error] 1960#1960: *421 upstream timed out (110: Connection timed out) while reading response header from upstream, client: *Removed*, server: *Removed*, request: "GET / HTTP/2.0", upstream: "fastcgi://unix:/var/run/php/php7.3-fpm.sock", host: "*Removed*"
2019/11/18 09:03:00 [error] 1960#1960: *421 upstream timed out (110: Connection timed out) while reading response header from upstream, client: *Removed*, server: *Removed*, request: "GET / HTTP/2.0", upstream: "fastcgi://unix:/var/run/php/php7.3-fpm.sock", host: "*Removed*"
2019/11/18 09:04:00 [error] 1960#1960: *421 upstream timed out (110: Connection timed out) while reading response header from upstream, client: *Removed*, server: *Removed*, request: "GET /favicon.ico HTTP/2.0", upstream: "fastcgi://unix:/var/run/php/php7.3-fpm.sock", host: "*Removed*", referrer: "*Removed*"

Thanks in adavance!

This one is solved.

If I waited enough, then I could login to the server with a local account and fix the LDAP.