What does it mean?
On Linux, almost everything is a file: real files, network connections and logs. Each running program may only keep a certain number of them open at once. This is the open files limit. If a busy program such as a web server or database hits the limit, it shows "Too many open files". It is like a juggler who can only hold so many balls.
Warning: back up any config file before you change it.
Steps
- Log in to your server with SSH.
- Check the current limit for your user:
ulimit -n
A common default is 1024. That is low for a busy site.
- See how many files a program has open. Replace
PIDwith its process number:
ls /proc/PID/fd | wc -l
This counts the open files for that program.
- Raise the limit for a session, to test:
ulimit -n 65535
This lasts only until you log out.
- Make it permanent. Back up and open the limits file:
cp /etc/security/limits.conf /etc/security/limits.conf.bak
nano /etc/security/limits.conf
- Add these lines at the bottom:
* soft nofile 65535
* hard nofile 65535
Soft is the working limit. Hard is the highest value a user can set. The star means all users.
- Save the file and log out, then log in again.
- Check with
ulimit -n.
For services started by systemd
Services like Nginx or MySQL ignore limits.conf. Set the limit in the service instead:
- Run
systemctl edit nginx(use your service name). - Add:
[Service]
LimitNOFILE=65535
- Save, then restart the service:
systemctl daemon-reload
systemctl restart nginx
Check the system-wide limit
cat /proc/sys/fs/file-max
This number is the most files the whole server can open.
Quick recap
- Each program can hold a limited number of open files.
- Check with
ulimit -n. - Raise it in limits.conf, or in the systemd service.
- Restart the program and test.