AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 837200
Accepted
Nick
Nick
Asked: 2017-03-10 00:21:56 +0800 CST2017-03-10 00:21:56 +0800 CST 2017-03-10 00:21:56 +0800 CST

为什么 php-fpm 在没有服务器负载的情况下每秒产生和摧毁数百个孩子?

  • 772

显示php7.0-fpm.log每秒产生数百个孩子,然后立即被摧毁。这种情况正在不停地发生。日志如下所示:

[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4980 started
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4978 exited with code 0 after 0.014658 seconds from start
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4981 started
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4980 exited with code 0 after 0.014354 seconds from start
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4982 started
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4979 exited with code 0 after 0.015170 seconds from start
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4983 started
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4981 exited with code 0 after 0.014709 seconds from start
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4984 started
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4982 exited with code 0 after 0.014741 seconds from start
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4985 started
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4983 exited with code 0 after 0.014727 seconds from start
[09-Mar-2017 02:58:25] NOTICE: [pool www] child 4986 started

设置来自/etc/php/7.0/fpm/php-fpm.conf:

pid = /run/php/php7.0-fpm.pid
error_log = /var/log/php7.0-fpm.log
include=/etc/php/7.0/fpm/pool.d/*.conf

设置来自/etc/php/7.0/fpm/pool.d/www.conf:

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
;pm.process_idle_timeout = 10s;
;pm.max_requests = 500
catch_workers_output = yes
php_flag[display_errors] = on
php_admin_value[error_log] = /var/log/phperrors.log
php_admin_flag[log_errors] = on

;所有其他行都用(默认设置)注释掉。这是 Ubuntu 16.04,使用默认包。PHP和Nginx版本如下:

php-fpm7.0 -v
PHP 7.0.13-0ubuntu0.16.04.1 (fpm-fcgi)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.13-0ubuntu0.16.04.1, Copyright (c) 1999-2016, by Zend Technologies

nginx -v
nginx version: nginx/1.10.0 (Ubuntu)

没有进行任何调整(除了启用日志记录)。此服务器尚未部署到生产环境,因此没有用户负载。

为什么 php-fpm 每秒产生并立即摧毁数百个孩子?这是配置错误,还是 Ubuntu 16.04 附带有错误的 php-fpm 版本?

nginx php-fpm ubuntu-16.04
  • 2 2 个回答
  • 6517 Views

2 个回答

  • Voted
  1. Best Answer
    user356895
    2017-03-10T01:06:21+08:002017-03-10T01:06:21+08:00

    既不是错误配置也不是错误版本。事实上,没有什么可担心的。

    您的进程快速退出和重生的原因是您pm.max_requests在 php-fpm 池配置文件中使用默认值,因为它通过分号;符号注释。要了解pm.max_requests参数代表什么,您可以阅读以下从默认配置中获取的描述:

    pm.max_requests = int

    每个子进程在重生之前应执行的请求数。这对于解决 3rd 方库中的内存泄漏问题很有用。对于无休止的请求处理,请指定“0”。等效于 PHP_FCGI_MAX_REQUESTS。默认值:0。

    你的是 0,因为它被评论了。您可以将其设置为 100-500(取决于您的需要),以便您的 php-fpm 在处理该数量的请求后回收该进程。

    顺便说一句,您应该注意,日志文件中的这些消息只是信息性的,没有任何问题,所以请不要担心。这些日志条目可以通过使用值warning而不是参数 innotice来避免。它几乎显示了所有内容 - 略低于level - 因为默认值设置为.log_levelphp-fpm.confdebugnotice

    取自默认配置:

    log_level = 字符串

    错误日志级别。可能的值:警报、错误、警告、通知、调试。默认值:通知。

    祝你好运

    • 7
  2. Josip Rodin
    2017-08-30T06:46:56+08:002017-08-30T06:46:56+08:00

    这已经发生在我的一台带有 Ubuntu 14 (PHP 5) 和 16 (PHP 7) 的服务器上。对于早期版本,会有一些延迟,但对于最新版本,一旦 FPM 进程处理了一个有问题的请求,它就会变得疯狂。

    我注意到有问题的请求确实发生exec在https://stackoverflow.com/questions/44400072/php-fpm-forever-respawning-children-when-executing-uglifycss中,但我仍然不明白这是怎么回事应该导致这个。它似乎与在后台执行的东西(与&操作员)密切相关。

    我无法提供实际的解决方案,只能提供从 cron 运行的丑陋的解决方法脚本:

    #!/bin/sh -e
    
    v=${1:-7.0}
    poolname=${2:-www}
    
    # requires Restart=always and KillMode=process in /lib/systemd/system/php7.0-fpm.service!
    retire_fpm() {
      logger -p user.crit -t $0 "$(sudo pkill -e php$v-fpm || sudo pkill -e php-fpm$v || echo "aiee, could not find php fpm version $v to retire" >&2)"
    }
    
    if sudo initctl version 2>/dev/null; then # upstart
      logdir=/var/log
      logfile=$logdir/upstart/php$v-fpm.log
    else
      logdir=/var/log
      logfile=$logdir/php$v-fpm.log
    fi
    
    logfile_huge=$(test -e $logfile && find $logfile -size +200M || true)
    if [ -n "$logfile_huge" ]; then
      logger -p user.crit -t $0 "demolishing $logfile to prevent the partition from filling up"
      cp /dev/null $logfile
    fi
    
    rotated_logfile_huge=$(test -e $logfile.1 && find $logfile.1 -size +200M || true)
    if [ -n "$rotated_logfile_huge" ]; then
      logger -p user.crit -t $0 "demolishing $logfile.1 to prevent the partition from filling up"
      cp /dev/null $logfile.1
    fi
    
    if [ ! -e $logfile ]; then
      if ! df -P $logdir/ | awk '/^\// && $5+0 > 50 { exit(1) }'; then
        logger -p user.crit -t $0 "initiating php$v-fpm termination (init will restart it) because there is no $logfile yet the partition is filling up"
      else
        logger -p user.crit -t $0 "initiating php$v-fpm termination (init will restart it) because there is no $logfile, something went awry"
      fi
      retire_fpm
      exit
    fi
    
    logfile_grew=$(find $logfile -size +5M || true)
    
    if [ -z "$logfile_grew" ]; then
      exit 0
    fi
    
    # 5 lines: handle recovery: one started line, one exited line, three post-signal lines
    recent_fast_respawns=$(tail -5 $logfile | egrep -c ' NOTICE: .pool '$poolname'. child .+ exited with code 0 after 0....... seconds from start')
    
    if [ "$recent_fast_respawns" -le 1 ]; then
      logger -p user.notice -t $0 "php$v-fpm log $logfile indicates a lot of recent activity, but only $recent_fast_respawns recent fast respawns, leaving it be"
      exit 0
    fi
    
    logger -p user.crit -t $0 "initiating php$v-fpm termination (init will restart it) because $logfile indicates $recent_fast_respawns recent fast respawns"
    retire_fpm
    

    这不应该存在......但我一直在随着时间的推移更新它。<叹气>

    • 0

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve