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
- 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.
Related articles
-
Optimizing Network Speed on Your Webdock KVM Server
A mini article with some kernel tweaks to improve network performance on your server
Last updated: September 6th 2024
-
How to configure Crontab on Linux
In this article we detail how Crontab works and all the available options for configuration along with correct syntax and examples.
Last updated: January 4th 2024
-
How to free up disk space on an Ubuntu Nginx or Apache Web Server
This article outlines useful commands you can run on your server in order to free up disk space.
Last updated: October 16th 2023
-
How to monitor webdock server resources using Prometheus, Grafana, Node Exporter and Prometheus Alert Manager
This guide includes the step by step procedure of installing different packages like Prometheus, Grafana, Node exporter and Alert Manager.
Last updated: February 20th 2025
-
How to Disable IPv6 on Your Webdock Server
The article explain how to disable IPv6 on your Webdock server, both temporarily and permanently.
Last updated: August 13th 2024
-
Automating Initial Server Configuration with Ansible
Read our new article: Learn how to automate your cloud server configuration using Ansible.
Last updated: February 12th 2025
-
Top Tools to Install on Your Ubuntu Web Server
A list of important tools that you can install on your production Ubuntu web server
Last updated: July 19th 2023
-
How To Benchmark Your Server with YABS
A guide to do benchmarking of your server's CPU, network performance, and such using YABS.
Last updated: April 1st 2024
-
Systemd Units - A Comprehensive Guide for Linux Admins
A detailed guide on systemd internals for Linux admins
Last updated: August 13th 2024
-
A Quick Guide to Managing Systemd Services
A short guide that helps you manage systemd services
Last updated: August 13th 2024
-
How to Benchmark PHP Performance on Your Webdock Server
Instructions for bechmarking PHP performance on your Webdock server
Last updated: February 12th 2025
-
Why don't I have the memory or disk allowance that I expect?
In this article we show why inside your instance you may see lower than expected RAM or DISK allowance available. It has all to do with units!
Last updated: November 29th 2024