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

vdb*_*oor 6 linux php fastcgi mod-fcgid apache-2.2

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

我使用的主要原因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"
Run Code Online (Sandbox Code Playgroud)

我的 Apache 配置如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

vdb*_*oor 3

找到答案:/sf/ask/41891111/ Between -several-php-processes-when-running-under-fastcgi/1094068#1094068

问题不是 PHP,而是 mod_fcgid。虽然 PHP 生成多个子项,但mod_fcgid对其一无所知,并且将为每个子项提供一个请求。因此,FcgidMaxProcessesPerClass 1使用 when 时,所有 PHP 执行都会相继发生。*

链接中提供的解决方案:http://www.brandonturner.net/blog/2009/07/fastcgi_with_php_opcode_cache/解释了如何使用mod_fastcgi没有此限制的解决方案。它将向同一个孩子发送多个请求。

[*] 请注意,不要FcgidMaxProcessesPerClass 1在 PHP、Ruby 等的许多单独实例中使用结果。尽管它们都能够在单个进程中内部处理许多请求。


因此,新的 Apache 配置将 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>
Run Code Online (Sandbox Code Playgroud)