V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
KasuganoSoras
V2EX  ›  问与答

PHP FPM 五分钟假死一次,没有任何报错

  •  
  •   KasuganoSoras · 20 天前 · 1415 次点击

    我的 Linux 服务器上搭建了 Nginx+PHP-FPM 的网站服务,从不知道什么时候开始,网站就经常 502 ,后来经过仔细观察发现每隔 5 分钟准时挂一次,具体表现为 /dev/shm/php-cgi.sock 消失,但 fpm 主进程和子进程都还在,Nginx 访问直接 502 。然后我想,可能是 PHP7 的问题,于是我又装了个 PHP8 ,结果还是一样 5 分钟挂一次,我就纳闷了。

    我尝试把 PHP-FPM 的日志等级设置为 debug ,然后观察日志输出内容,网站访问正常的时候,日志输出大概如下:

    [30-Aug-2024 09:03:56.136408] DEBUG: pid 3493523, fpm_event_loop(), line 430: event module triggered 1 events
    [30-Aug-2024 09:03:56.798162] DEBUG: pid 3493523, fpm_pctl_perform_idle_server_maintenance(), line 382: [pool www] currently 0 active children, 256 spare children, 256 running children. Spawning rate 1
    [30-Aug-2024 09:03:56.848024] DEBUG: pid 3493523, fpm_event_loop(), line 430: event module triggered 1 events
    [30-Aug-2024 09:03:57.474479] DEBUG: pid 3493523, fpm_event_loop(), line 430: event module triggered 1 events
    [30-Aug-2024 09:03:57.798815] DEBUG: pid 3493523, fpm_pctl_perform_idle_server_maintenance(), line 382: [pool www] currently 0 active children, 256 spare children, 256 running children. Spawning rate 1
    
    

    但是每次挂掉的时候,就会持续输出下面这样的内容,此时 php-cgi.sock 就会消失,并且没有看到任何 WARNING 或者 ERROR 日志内容。

    [30-Aug-2024 09:03:57.798815] DEBUG: pid 3493523, fpm_pctl_perform_idle_server_maintenance(), line 382: [pool www] currently 0 active children, 256 spare children, 256 running children. Spawning rate 1
    

    我尝试过调整 php-fpm.conf 文件的 max_children 等配置,但是都没用,还是每 5 分钟准时挂一次。我又找来另一台服务器,用同样的系统,同样的 PHP 版本和配置,同样的 Nginx 配置,同样的访问流量,结果新的服务器啥事没有,甚至新的服务器配置比有问题的那台还低,这就奇怪了。

    实在是想不到原因了,求助一下各位大佬,谢谢~


    软硬件配置

    • PHP:7.4.30 NTS / PHP 8.2.7 NTS
    • Nginx:1.23.3
    • 系统:Fedora 37
    • CPU:E5-2696 v4 * 2 (有问题的)/ E5-2660 v2 * 2 (没问题的)
    • 内存:128G (有问题的)/ 64G (没问题的)
    • 硬盘:两台机器都有至少 1T 以上空闲空间

    PHP-FPM 配置

    https://gist.github.com/kasuganosoras/1d8611a42bcbba19363dcab3931bfd52

    ulimit -a

    real-time non-blocking time  (microseconds, -R) unlimited
    core file size              (blocks, -c) unlimited
    data seg size               (kbytes, -d) unlimited
    scheduling priority                 (-e) 0
    file size                   (blocks, -f) unlimited
    pending signals                     (-i) 514500
    max locked memory           (kbytes, -l) 8192
    max memory size             (kbytes, -m) unlimited
    open files                          (-n) 1000000
    pipe size                (512 bytes, -p) 8
    POSIX message queues         (bytes, -q) 819200
    real-time priority                  (-r) 0
    stack size                  (kbytes, -s) 8192
    cpu time                   (seconds, -t) unlimited
    max user processes                  (-u) 1000000
    virtual memory              (kbytes, -v) unlimited
    file locks                          (-x) unlimited
    
    9 条回复    2024-08-30 12:37:05 +08:00
    hhacker
        1
    hhacker  
       20 天前
    这么明确, 那就是有个每五分钟一次的定时任务咯
    justfindu
        2
    justfindu  
       20 天前
    这种很明确了 你程序有个慢执行或者慢查询拖了整个进程 你查一下 PHP 和 SQL 的慢执行
    KasuganoSoras
        3
    KasuganoSoras  
    OP
       20 天前
    @hhacker #1 感谢,你点醒我了,我看了一下还真是有个 NextCloud 的定时任务,5 分钟执行一次的,我现在把这个定时任务删了再观察一下
    KasuganoSoras
        4
    KasuganoSoras  
    OP
       20 天前
    @justfindu #2 还真是,NextCloud 的 cron 不知道为什么会这么慢,现在删掉了定时任务过了快 10 分钟了也没事,应该是没有问题了,感谢!
    BeforeTooLate
        5
    BeforeTooLate  
       20 天前
    @hhacker 我第一感觉就是有个定时任务,不然不会时间精准到这个程度,哈哈
    MigrantWorkers
        6
    MigrantWorkers  
       19 天前
    用 webman 吧,老项目跑了一年多了 cli 模式挺稳定的
    woshihgs
        7
    woshihgs  
       19 天前
    OP 也不像是新人,固定 5 分钟应该很容易就能想到是定时任务
    KasuganoSoras
        8
    KasuganoSoras  
    OP
       19 天前
    @woshihgs #7 我前两年搭建 NextCloud 的时候,用 crontab 创建了一个定时任务,当时不知道什么原因一直没生效,后来我就干脆写了个 systemd 服务来运行 cron 。时隔多年更新过 NextCloud 之后不知道为啥当时创建的 cron 又生效了,然后就出现了今天的事情🙃
    z1829909
        9
    z1829909  
       19 天前
    好奇 cron 的话是 cli 模式运行吗, 为什么会影响到 fpm 呢.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5823 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 02:08 · PVG 10:08 · LAX 19:08 · JFK 22:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.