Centos – Setting ulimit correctly for PHP on Linux

centoslinuxSecurityulimit

I have /etc/security/limits.conf set to the following:

nobody soft nofile 409600
nobody hard nofile 1024000
phpuser soft nofile 409600
phpuser hard nofile 1024000
httpd soft nofile 409600
httpd hard nofile 1024000
* soft nofile 409600
* hard nofile 1024000

However, php pages still display:

[...] failed to open stream: Too many open files [...]

Setting ulimit -n 10000 looks to only be a temporary fix.

I also have the following set:

fs.file-max = 20970800
net.core.somaxconn = 1024000
kern.maxfilesperproc = 16638400
kern.maxfiles = 819200

Best Answer

I see two potential problems.

Your limit may not apply to phpuser

phpuser may ignore your new limit because it might not use PAM to "log-in", so /etc/security/limits.conf would not apply. See this answer for more details.

The system wide limit is reached

Your are changing users processes limits. The kernel also has a system wide limit on the number of open file-handles.

This might be your problem. You can check the value like this:

$ sysctl fs.file-max
$ sysctl fs.file-nr

By default, file-max shoud be 10% of your available system memory in kB, which may not be a very big number: 4 GB RAM ~ 400000 files which is below the limit you are trying to set.

The documentation says:

The three values in file-nr denote the number of allocated file handles, the number of allocated but unused file handles, and the maximum number of file handles. Linux 2.6 always reports 0 as the number of free file handles -- this is not an error, it just means that the number of allocated file handles exactly matches the number of used file handles.

Related Question