Skip to the fix
PHPSolvedThe fix first, then the reason

Newest fixes

Linux, nginx and Apache | | 3 min

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
  1. See the limit that applies
  2. A service: LimitNOFILE
  3. nginx has two numbers of its own
  4. A login shell: limits.conf
  5. The kernel's limits
  6. When it isn't the limit

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:

bash
ulimit -Sn
ulimit -Hn

For a running service, read the limit of the process and not of your shell:

bash
grep 'open files' /proc/$(pgrep -o nginx)/limits

A service: LimitNOFILE

Create a drop-in, so that a package update does not overwrite it:

bash
sudo systemctl edit nginx
ini
[Service]
LimitNOFILE=65536
bash
sudo systemctl daemon-reload
sudo systemctl restart nginx

systemd.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:

nginx
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:

output
# /etc/security/limits.d/90-nofile.conf
deploy  soft  nofile  65536
deploy  hard  nofile  65536

The 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:

bashran in bash
ulimit -Sn "$(ulimit -Hn)" 2>/dev/null || ulimit -Sn 4096
[ "$(ulimit -Sn)" != 256 ] && echo raised
real output
raised

The 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:

bash
sysctl fs.nr_open fs.file-max
cat /proc/sys/fs/file-nr

When 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:

bash
sudo ls /proc/$(pgrep -o php-fpm)/fd | wc -l

A 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