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
- Create a directory for Apache2 service overrides:
# mkdir -p /etc/systemd/system/apache2.service.d/
- Create a configuration file with the desired limit:
# echo -e "[Service]nLimitNOFILE=500000" > /etc/systemd/system/apache2.service.d/limit_nofile.conf
- Reload systemd and restart Apache2:
# systemctl daemon-reload # systemctl restart apache2
- 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
- Create a directory for Nginx service overrides:
# mkdir -p /etc/systemd/system/nginx.service.d/
- Create a configuration file with the desired limit:
# echo -e "[Service]nLimitNOFILE=4096" > /etc/systemd/system/nginx.service.d/override.conf
- Reload systemd and restart nginx:
# systemctl daemon-reload # systemctl restart nginx
- 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
- 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
- Save the file and exit.
Step 2: Modify PAM Limits Configuration
- Edit the PAM limits file:
# nano /etc/pam.d/common-session
- Ensure the following line is present:
session required pam_limits.so
- Save the file and exit.
Step 3: Modify Sysctl Configuration
- Edit the sysctl configuration file:
# nano /etc/sysctl.conf
- Add the following line with the desired limit:
fs.file-max = 2097152
- Apply the changes:
# sysctl -p
Step 4: Modify Systemd Limits for All Services
- Edit the system-wide limits file:
# nano /etc/systemd/system.conf
- Modify or add the following lines:
DefaultLimitNOFILE=65535
- Edit the user limits file:
# nano /etc/systemd/user.conf
- Modify or add the following lines:
DefaultLimitNOFILE=65535
- Reload systemd:
# systemctl daemon-reexec
- 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.