Nginx,  PHP,  Ubuntu

Increase Max Number of Allowed Opened Files

This is “How To Increase Max Number of Allowed Opened Files Nginx and PHP-FPM on Ubuntu“.
According to some tutorials you can changed the max number of opened files per user in /etc/security/limits.conf

*    soft nofile 64000
*    hard nofile 68000

You also changed the max number of opened files for entire system in /etc/sysctl.conf

fs.file-max = 200500

After reboot your machine, you should check soft and hard limits for your user login to the system with

thuns@ubuntu:~$ ulimit -Sn
64000
thuns@ubuntu:~$ ulimit -Hn
68000

As you see, the limits applied to users. The problem is sometime that can’t change the limits for Nginx and PHP

thuns@ubuntu:~$ sudo ps aux|grep nginx
root      1124  0.0  0.0  45988   980 ?        Ss   01:04   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data  1125  0.0  0.2  46164  2372 ?        S    01:04   0:00 nginx: worker process
thuns     2754  0.0  0.0   5108   848 pts/6    S+   01:14   0:00 grep --color=auto nginx
thuns@ubuntu:~$ cat /proc/1124/limits
Limit                     Soft Limit           Hard Limit           Units     

.....
Max open files            1024                 4096                 files     
.....  

And the same thing for PHP

thuns@ubuntu:~$ sudo ps aux|grep php
root      1097  0.0  2.1 118388 22496 ?        Ss   01:04   0:00 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data  1140  0.0  0.5 118388  5532 ?        S    01:04   0:00 php-fpm: pool www
www-data  1141  0.0  0.5 118388  5532 ?        S    01:04   0:00 php-fpm: pool www
thuns     2795  0.0  0.0   5108   852 pts/6    S+   01:20   0:00 grep --color=auto php
thuns@ubuntu:~$ cat /proc/1097/limits
Limit                     Soft Limit           Hard Limit           Units     
.....
Max open files            1024                 4096                 files     
.....

But don’t worry, if you face that issue, this one can solved the problem!

You need to add that configuration to the systemd unit file. Best would be to make this via a drop-in file.

mkdir /etc/systemd/system/nginx.service.d
echo "[Service]
LimitNOFILE=10000" > /etc/systemd/system/nginx.service.d/local.conf
systemctl daemon-reload
systemctl restart nginx

Then repeat that for the php7.2-fpm.service unit file. (Depend on your php version installed)

You also can set defaults in /etc/systemd/system.conf, but it is recommended to use the drop-in files.

Leave a Reply

Your email address will not be published. Required fields are marked *