Increase the maximum number of open files on Ubuntu (ulimit -n)
For a service such as nginx or PHP-FPM set LimitNOFILE in a systemd drop-in. The limits.conf file only changes login sessions, which is why edits seem ignored.
A post about this problem was printed at this address on . The text below is new, written on 2026-11-25 from the official documentation linked in it.
On this page
Decide first what the limit is for, because there are two separate places. A service started by systemd takes its limit from its unit. A shell you log in to takes it from PAM. Editing one does nothing for the other, and that's the reason the old recipe with limits.conf so often "doesn't work" for nginx.
See the limit that applies
The shell has two values, a soft limit that a program may raise by itself and a hard limit above it:
ulimit -Sn
ulimit -HnFor a running service, read the limit of the process and not of your shell:
grep 'open files' /proc/$(pgrep -o nginx)/limitsA service: LimitNOFILE
Create a drop-in, so that a package update does not overwrite it:
sudo systemctl edit nginx[Service]
LimitNOFILE=65536sudo systemctl daemon-reload
sudo systemctl restart nginxsystemd.exec documents the setting as the equivalent of ulimit -n. Units without it get the defaults from systemd-system.conf. The same page advises care with a soft limit above 1024 for programs that still use select(), so raise it for the service that needs it and not for everything.
PHP-FPM, MySQL and Redis work the same way: one drop-in per unit. PHP-FPM also has rlimit_files in the pool file.
nginx has two numbers of its own
worker_connections counts every connection of a worker process, including the ones to PHP-FPM or a proxied server, and it can't exceed the limit on open files. worker_rlimit_nofile raises that limit for the workers without restarting the main process:
worker_rlimit_nofile 65536;
events {
worker_connections 16384;
}A proxied request uses two connections, and a file that is sent needs one more descriptor. Keeping the file limit at several times worker_connections leaves room for that. nginx was not run for this page; check the result with the /proc line above.
A login shell: limits.conf
For users who log in, the limits.conf file applies. Use a file of your own in limits.d:
# /etc/security/limits.d/90-nofile.conf
deploy soft nofile 65536
deploy hard nofile 65536The manual says that these limits are set by pam_limits for login sessions. Log out completely and in again. An open session, and everything started from it, keeps the old value.
To try a value for one shell only, raise the soft limit up to the hard one:
ulimit -Sn "$(ulimit -Hn)" 2>/dev/null || ulimit -Sn 4096
[ "$(ulimit -Sn)" != 256 ] && echo raisedraisedThe kernel's limits
fs.nr_open is the ceiling for one process, and the hard limit cannot be set above it. fs.file-max is the number for the whole system, as the manual page says. The note that was here told you to lower file-max to the value you want for one process, which is the wrong direction on a machine where it's already larger. Read both before touching them:
sysctl fs.nr_open fs.file-max
cat /proc/sys/fs/file-nrWhen it isn't the limit
"Too many open files" can also be a leak: a process that opens files or sockets and never closes them reaches any limit, only later. Count the descriptors of the process twice, some minutes apart:
sudo ls /proc/$(pgrep -o php-fpm)/fd | wc -lA number that only grows is a bug to find. If nginx reports the error while talking to PHP, the pages on 502 answers and the other web server fixes are the next stop.
Same subject
nginx 502 Bad Gateway with PHP-FPM: read the error log line first
A 502 means nginx got no usable answer from PHP-FPM. One line in the nginx error log says why: no socket, no permission, a dead worker or a big header.
Install phpMyAdmin on Ubuntu with nginx and PHP-FPM, the manual way
Download and verify the archive, unpack it outside the web root, write a server block with a PHP location, add config.inc.php and lock the address down.
gpg: can't connect to the agent: IPC connect call failed
gpg could not reach gpg-agent over its socket. Restart the agent with gpgconf, check the home folder's path and permissions, and move WSL 1 to WSL 2.
PHPSolvedThe fix first, then the reason