一个单独的php-fastcgi进程阻止所有其他的PHP请求

我最近切换到PHP的FastCGI设置(Apache2-worker和mod_fcgid )。 但是,当一个PHP脚本非常繁忙时,似乎会阻止所有其他PHP请求。 我的configuration有什么问题?

我使用mod_fcgid主要原因是保持PHP内存使用情况受到控制。 使用mod_php ,所有单独的Apache分支在服务PHP之后将在内存中增长。

我也切换到apache2-worker模型,因为所有线程不安全的PHP代码都存在于Apache之外。

我的FastCGI脚本如下所示:

 #!/bin/sh #export PHPRC=/etc/php/fastcgi/ export PHP_FCGI_CHILDREN=5 export PHP_FCGI_MAX_REQUESTS=5000 global_root=/srv/www/vhosts.d/ exec /usr/bin/php-cgi5 \ -d open_basedir=$global_root:/tmp:/usr/share/php5:/var/lib/php5 \ -d disable_functions="exec,shell_exec,system" 

我的Apacheconfiguration如下所示:

 <IfModule fcgid_module> FcgidIPCDir /var/lib/apache2/fcgid/ FcgidProcessTableFile /var/lib/apache2/fcgid/shm FcgidMaxProcessesPerClass 1 FcgidInitialEnv RAILS_ENV production FcgidIOTimeout 600 AddHandler fcgid-script .fcgi FcgidConnectTimeout 20 MaxRequestLen 16777216 <FilesMatch "\.php$"> AddHandler fcgid-script .php Options +ExecCGI FcgidWrapper /srv/www/cgi-bin/php5-wrapper.sh .php </FilesMatch> DirectoryIndex index.php </IfModule> 

find答案在: https : //stackoverflow.com/questions/598444/how-to-share-apc-cache-between-several-php-processes-when-running-under-fastcgi/1094068#1094068

问题不是PHP,而是mod_fcgid。 当PHP产生多个孩子时, mod_fcgid是无知的,并将为每个孩子提供一个请求。 因此,当使用FcgidMaxProcessesPerClass 1时,所有的PHP执行都会FcgidMaxProcessesPerClass 1发生。 *

该解决scheme提供了哪些链接: http : mod_fastcgi解释了如何使用mod_fastcgi ,它没有这个限制。 它会发送多个请求给同一个孩子。

[*]请注意,不使用FcgidMaxProcessesPerClass 1导致许多单独的PHP,ruby等实例,尽pipe它们都能够在一个进程内部处理多个请求。


因此,一个新的Apacheconfiguration使用PHP和fastcgi:

 <IfModule mod_fastcgi.c> # Needed for for suEXEC: FastCgiWrapper On FastCgiConfig -idle-timeout 20 -maxClassProcesses 1 -initial-env RAILS_ENV=production FastCgiIpcDir /var/lib/apache2/fastcgi AddHandler php5-fcgi .php Action php5-fcgi /.fcgi-bin/php5-wrapper.sh DirectoryIndex index.php ScriptAlias /.fcgi-bin/ /srv/www/cgi-bin/ <Location "/.fcgi-bin/php5-wrapper.sh"> Order Deny,Allow Deny from All #Allow from all Allow from env=REDIRECT_STATUS Options ExecCGI SetHandler fastcgi-script </Location> # Startup PHP directly FastCgiServer /srv/www/cgi-bin/php5-wrapper.sh # Support dynamic startup AddHandler fastcgi-script fcg fcgi fpl </IfModule> 

首先,除非Apache的文档已经过时,否则你的包装器脚本和设置就是错误的Just Plan Wrong。 请阅读mod_fcgid文档中的“特殊PHP注意事项”,并使用该脚本和示例设置。 你目前的设置基本上会产生一堆不可用的PHPsubprocess,然后每个5001st PHP请求都会出错,因为PHP将在第5000次请求之后退出,但是你错过了FcgidMaxRequestsPerProcess 5000指令,它告诉mod_fcgid它将需要启动一个新的5000个请求后的PHP进程。

至于同时的PHP进程,每个同时请求都需要它自己的PHP进程,所以你需要增加你的FcgidMaxProcessesPerClass指令到一个更高的数目。