解决Linux中出现Too many open files

June 25th, 2009 | Categories: Linux | Tags: , ,

前几天查看VPS的php-cgi和nginx的日志都出现Too many open files这样的错误,原因是file-max默认值(1024)太小了,要解决这个问题,只要更改file-max的大小就可以.

用ulimit修改了一下:

ulimit -n 10240

这样就可以将打开文件数从1024提高到了10240,但重启了VPS,就会恢复默认大小了..要永久的改变file-max值,可以root身份执行下列命令

# echo "10240"  > /proc/sys/fs/file-max  # 适用于2.2和2.4版内核
# echo "10240" > /proc/sys/fs/inode-max # 仅适用于2.2版内核

或将下列内容放入 /etc/sysctl.conf,做永久性的更改:

fs.file-max = 10240   # 适用于 2.2 和 2.4 版内核
fs.inode-max = 10240 # 仅适用于 2.2 版内核

最正确的做法是在/etc/security/limits.conf里设置:

* hard nofile 65535
* soft nofile 65535

Related posts:

  1. DNS解析太慢?何不试试OpenDNS--更安全、更快、更智能的DNS
  1. June 25th, 2009 at 05:33

    以后看到php-cgi和nginx进程出现内存占用不稳定,CPU占用不稳定就一定要查看日志!少走点弯路

  2. June 25th, 2009 at 06:48

    内核是啥意思?CentOS的内核?

  3. June 25th, 2009 at 10:27

    还没投入linux的怀抱

  4. June 25th, 2009 at 12:05

    小猫换了域名后疯狂post。。。

    • June 25th, 2009 at 13:47

      而且猫猫还换了模板的说……

    • June 25th, 2009 at 13:56

      博客荒废很久了.刚好最近在搞VPS,就更新更新了~