mod_fcgid出现500错误的解决

[MT上的php5](347)是用mod_fcgid方式运行的,但运行一段时间之后,有时候会莫名其妙的出现500错误,也看不出是哪个php程序错误,不过html文件倒能正常解析,查看用户的$HOME/statistics/logs/error_log文件,会发现大量这样的内容:

[Fri Aug 10 01:00:43 2007] [error] [client 202.108.23.76] Premature end of scrip
t headers: index.php
[Fri Aug 10 01:01:20 2007] [error] [client 60.191.82.228] Premature end of scrip
t headers: index.php

而在系统的log文件/var/log/apache/error_log中,是这样的错误:

[Fri Aug 10 01:03:05 2007] [notice] mod_fcgid: process /var/www/vhosts/fwolf.com
/httpdocs/blog/index.php(21666) exit(communication error), terminated by calling
 exit(), return code: 120

G到一些资料,第一类错误是脚本执行被中断,没有返回完整的http header;第二类错误是cgi程序的执行被强行中止。根本原因是fastcgi会自动kill掉一些发呆的、长时间没有响应的进程(要不它比较快,比较省内存啊),但是fastcgi的默认idle时间设置过于苛刻([官方文档](http://fastcgi.coremail.cn/doc.htm)中每个选项都有默认的时间值),所以有些处理比较慢、需要调用外部资源的程序就有可能被误杀,所以先略微调整一下,继续观察:

<IfModule mod_fcgid.c>
    IdleTimeout 600
    ProcessLifeTime 3600
    MaxProcessCount 8
    DefaultMinClassProcessCount 3
    DefaultMaxClassProcessCount 3
    IPCConnectTimeout 30
    IPCCommTimeout 600
    #MaxRequestsPerProcess 500
</IfModule>
  • IdleTimeout 发呆时限
  • ProcessLifeTime 一个进程的最长生命周期,过期之后无条件kill
  • MaxProcessCount 最大进程个数
  • DefaultMinClassProcessCount 每个程序启动的最小进程个数
  • DefaultMaxClassProcessCount 每个程序启动的最大进程个数
  • IPCConnectTimeout 程序响应超时时间
  • IPCCommTimeout 与程序通讯的最长时间,上面的错误有可能就是这个值设置过小造成的
  • MaxRequestsPerProcess 每个进程最多完成处理个数,达成后自杀,因为PHP最多只处理500次请求。不过这个是mod_fcgid 1.11版本添加的,我们主机上暂时不支持。

上述选项的确切作用我也不是十分清楚,先用着这个设置,再根据情况调整。

参考:

  • [The mod_fcgid Home Page](http://fastcgi.coremail.cn/doc.htm)
  • [Ruby on Rails > fcgid -> errors](http://www.ruby-forum.com/topic/54403)
  • [Rails on Fedora + Plesk Fiasco](http://ifakedit.com/log/2006/03/28/rails-on-fedora-plesk-fiasco/#comment-344)
  • [[typo] Error with large number of flickr images](http://rubyforge.org/pipermail/typo-list/2006-July/003153.html)
  • [Secure PHP environments with PHP, suexec and FastCGI (mod fcgid) (en)](http://www.felix-schwarz.name/Secure_PHP_environments_with_PHP,_suexec_and_FastCGI_(mod_fcgid)_(en))
  • [Problem with PHP5 as fcgid](http://forum.swsoft.com/showthread.php?threadid=42946)
  • [mod_fcgid instead of mod_fastcgi – Images don’t load?!?! ](http://isp-control.net/forum/mod-fcgid-instead-mod-fastcgi-images-dont-load-t-797.html)
  • [Hou, senseless timeout setting of fcgid](http://1stein.org/2006/10/06/HouSenselessTimeoutSetting)
  • [HDD and mod_fcgid Frustration](http://nikolasco.livejournal.com/327606.html)
  • [Debian mod_fastcgi Notes](http://wiki.rubyonrails.org/rails/pages/Debian+mod_fastcgi+Notes)
  • [Watch for huge requests on default FCGI](http://weblog.rubyonrails.com/2005/01/03/watch-for-huge-requests-on-default-fcgi)

3 thoughts on “mod_fcgid出现500错误的解决”

  1. 谢谢 经测试发现MaxRequestsPerProcess 最好设置在500以下 否则会出现莫名奇妙的错误 类似 [warn] (104)Connection reset by peer: mod_fcgid: read data from fastcgi server error. [warn] (104)Connection reset by peer: mod_fcgid: can’t get data from http client

Leave a Reply

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