Guide to Increasing Open File Limits for Nginx, Apache2, and Linux

Last updated: March 3rd 2025

Introduction

In Linux-based systems, the number of open files is limited by system settings. This limit can affect the performance of services like Apache2 and Nginx, potentially causing errors such as "Too many open files." This guide provides step-by-step instructions to increase the open file limits for Apache2, Nginx, and system-wide in Linux.

All commands below has to be run as root. So switch to root first using the below command.

$ sudo su

Increasing Open File Limits for Apache2

Step 1: Modify Systemd Configuration

  1. Create a directory for Apache2 service overrides:
# mkdir -p /etc/systemd/system/apache2.service.d/
  1. Create a configuration file with the desired limit:
# echo -e "[Service]\nLimitNOFILE=500000" > /etc/systemd/system/apache2.service.d/limit_nofile.conf
  1. Reload systemd and restart Apache2:
# systemctl daemon-reload
# systemctl restart apache2
  1. Verify the changes:
# cat /proc/$(pgrep -u root -f 'apache2')/limits | grep 'open files'

Increasing Open File Limits for Nginx

Step 1: Modify Systemd Configuration

  1. Create a directory for Nginx service overrides:
# mkdir -p /etc/systemd/system/nginx.service.d/
  1. Create a configuration file with the desired limit:
# echo -e "[Service]\nLimitNOFILE=4096" > /etc/systemd/system/nginx.service.d/override.conf
  1. Reload systemd and restart nginx:
# systemctl daemon-reload
# systemctl restart nginx
  1. Verify the changes:
# grep 'open file' /proc/$(pgrep -u root -f 'nginx: master')/limits

Note: This steps can be replicated for other services such as MariaDB and MySQL where this issue usually occurs.

Increasing Open File Limits System-Wide in Linux

Step 1: Modify Security Limits Configuration

  1. Edit the limits configuration file:
# nano /etc/security/limits.conf

Add or modify the following lines with your desired limit at the end of the file:

* soft nofile 65535
* hard nofile 65535
  1. Save the file and exit.

Step 2: Modify PAM Limits Configuration

  1. Edit the PAM limits file:
# nano /etc/pam.d/common-session
  1. Ensure the following line is present:
session required pam_limits.so
  1. Save the file and exit.

Step 3: Modify Sysctl Configuration

  1. Edit the sysctl configuration file:
# nano /etc/sysctl.conf
  1. Add the following line with the desired limit:
fs.file-max = 2097152
  1. Apply the changes:
# sysctl -p

Step 4: Modify Systemd Limits for All Services

  1. Edit the system-wide limits file:
# nano /etc/systemd/system.conf
  1. Modify or add the following lines:
​​​​​​​DefaultLimitNOFILE=65535
  1. Edit the user limits file:
# ​​​​​​​nano /etc/systemd/user.conf
  1. Modify or add the following lines:
​​​​​​​DefaultLimitNOFILE=65535
  1. Reload systemd:
​​​​​​​# systemctl daemon-reexec
  1. Reboot the system to apply changes.

Conclusion

By following this guide, you can successfully increase the number of open files allowed for Apache2, Nginx, and system-wide in Linux. This helps prevent errors and improves system performance for high-traffic web applications.

This article was written by Webdock Support Specialist Taufiq Zainal. Taufiq is passionate about Cloud Hosting and helping Webdock Customers succeed wherever he can.

Related articles

chat box icon
Close
combined chatbox icon

Welcome to our Chatbox

Reach out to our Support Team or chat with our AI Assistant for quick and accurate answers.
webdockThe Webdock AI Assistant is good for...
webdockChatting with Support is good for...