Fix “Too Many Open Files” Error Linux (Complete Guide)

The “Too Many Open Files” error in Linux can bring your applications to a halt. It commonly affects:

  • Web servers (Nginx, Apache)
  • Databases (MySQL)
  • Node.js apps
  • Docker containers

This issue occurs when your system exceeds file descriptor limits.

In this guide, you’ll learn:

  • What causes the error
  • How to diagnose it
  • Step-by-step fixes
  • Long-term solutions

What Does “Too Many Open Files” Mean?

Linux limits how many files a process can open simultaneously.

Error examples:

  • Too many open files
  • EMFILE: too many open files
  • ulimit reached

Step 1: Check Current Limits

ulimit -n

Typical output:

  • 1024 (default)

Check system-wide limit:

cat /proc/sys/fs/file-max

Step 2: Check Open Files Usage

lsof | wc -l

Check per process:

lsof -u username

Step 3: Increase Temporary Limit

ulimit -n 65535

⚠️ This resets after reboot.


Step 4: Increase Permanent Limits

Edit:

nano /etc/security/limits.conf

Add:

* soft nofile 65535
* hard nofile 65535

Step 5: Update System Limits

Edit:

nano /etc/sysctl.conf

Add:

fs.file-max = 2097152

Apply:

sysctl -p

Step 6: Fix Nginx Error

Edit:

nano /etc/nginx/nginx.conf

Add:

worker_rlimit_nofile 65535;

Inside events:

worker_connections 4096;

Restart:

systemctl restart nginx

Step 7: Fix Apache Error

Edit:

nano /etc/apache2/apache2.conf

Add:

ServerLimit 4096
MaxRequestWorkers 4000

Step 8: Fix MySQL Error

Edit:

nano /etc/mysql/my.cnf

Add:

open_files_limit = 65535

Restart MySQL.


Step 9: Check for File Descriptor Leaks

Sometimes apps don’t close files properly.

Check:

lsof -p PID

If count keeps increasing → memory/file leak.

Fix:

  • Restart app
  • Debug code

Step 10: Docker Fix

Docker containers have their own limits.

Run container with:

--ulimit nofile=65535:65535

Step 11: Systemd Fix

Edit service file:

nano /etc/systemd/system.conf

Add:

DefaultLimitNOFILE=65535

Reload:

systemctl daemon-reexec

Real-World Example

A high-traffic WordPress site crashed.

Cause:

  • Nginx hit file descriptor limit

Fix:

  • Increased ulimit
  • Tuned Nginx worker connections

Result:

  • Site handled 5x traffic without crashing

Prevent This Error (Best Practices)

1. Monitor Open Files

watch -n 1 "lsof | wc -l"

2. Optimize Application Code

Ensure:

  • Files are properly closed
  • Database connections are reused

3. Use Better Hosting

Low-end servers hit limits quickly.

👉 Upgrade to optimized hosting:

👉 Cloudways (Best for scalable apps)

Load WordPress Sites in as fast as 37ms!

👉 Bluehost (Best for beginners)


When to Upgrade?

Upgrade if:

  • High traffic websites
  • Multiple apps running
  • Frequent crashes

Cloud platforms handle scaling better than traditional hosting.


Bonus: Quick Fix Script

ulimit -n 65535
sysctl -w fs.file-max=2097152

Troubleshooting Checklist

✔ Check ulimit
✔ Check system limits
✔ Restart services
✔ Monitor file usage
✔ Optimize configs


Conclusion

The “Too Many Open Files” error is common in growing systems but easy to fix with proper configuration.

Key takeaways:

  • Increase file descriptor limits
  • Tune services like Nginx & MySQL
  • Monitor usage regularly
  • Use scalable hosting

If you want zero headache scaling, managed platforms like Cloudways make a huge difference.


Final Tip

Most server issues (disk, file limits, crashes) happen because of:

  • Poor resource allocation
  • No monitoring
  • Cheap hosting

Fix these once, and your system becomes stable for years.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *