buy
How to increase the number of open files allowed for Apache2

Overview

When you try to start or stop Apache you may see errors like "Error: Too many open files" and so file limit needs to be increased.

Procedure

On a server using systemd to manage services please follow the below steps.

Step 1 : Make directory as httpd.service.d under /usr/lib/systemd/system/

mkdir /usr/lib/systemd/system/httpd.service.d

Step 2 : Now create a file with .conf extension like /usr/lib/systemd/system/httpd.service.d/limits.conf

touch /usr/lib/systemd/system/httpd.service.d/limits.conf

Step 3 : Now add the following content in limits.conf file like following:

[Service]
LimitNOFILE=40000

Step 4 : Now reload daemon and restart httpd service.

systemctl daemon-reload

systemctl restart httpd 
or 
/usr/local/apps/apache2/bin/apachectl restart

If this limits.conf file changes does not work then you can directly modify /usr/local/apps/apache2/bin/apachectl and comment out the following lines of sections :

#if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
# $ULIMIT_MAX_FILES
#fi

Then just after that commented section add following line with the limit needed:

ulimit -n 40000

and you need to reload daemon and restart httpd service

systemctl daemon-reload

systemctl restart httpd 
or 
/usr/local/apps/apache2/bin/apachectl restart

check the configured number of open files allowed for Apache2

First you need to get any one process ID that is running.

You can use ps to obtain the httpd process ID.

Run this command : ps auxw|grep -E '^USER|httpd'

You will have output like this :

root@server:~# ps auxw|grep -E '^USER|httpd'
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     2827484  0.0  2.0 378792 42036 ?        Ss   09:40   0:00 /usr/local/apps/apache2/bin/httpd -k start
nobody   2831437  0.0  1.0 381472 21840 ?        S    09:53   0:00 /usr/local/apps/apache2/bin/httpd -k start
nobody   2831438  0.0  1.0 381472 21840 ?        S    09:53   0:00 /usr/local/apps/apache2/bin/httpd -k start
nobody   2831439  0.0  1.0 381472 21844 ?        S    09:53   0:00 /usr/local/apps/apache2/bin/httpd -k start
nobody   2831440  0.0  1.0 381472 21260 ?        S    09:53   0:00 /usr/local/apps/apache2/bin/httpd -k start
nobody   2831441  0.0  1.0 381608 21584 ?        S    09:53   0:00 /usr/local/apps/apache2/bin/httpd -k start
nobody   2831443  0.0  1.0 381472 21840 ?        S    09:53   0:00 /usr/local/apps/apache2/bin/httpd -k start
root     2834969  0.0  0.0   9032   660 pts/0    R+   10:03   0:00 grep --color=auto -E ^USER|httpd

Now using one of those pids you can get the limit setting from the pid proc files.

For example, taking pid 2827484 from the ps output above you can check with:

grep 'Max open files' /proc/2827484/limits

the output it provides which shows the limit is set to 40000.

root@server:~# grep 'Max open files' /proc/2827484/limits
Max open files            40000                40000                files
    Was this page helpful?
    Newsletter Subscription
    Subscribing you to the mailing list