You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

php8.1-fpm终止后无法重启引发502 Bad Gateway问题求助

问题

我在DigitalOcean Droplet上部署了Laravel应用,使用Laravel Forge进行服务器管理,Envoyer实现零停机部署。网站持续出现502 Bad Gateway错误,经排查定位为php8.1-fpm终止后未自动重启。

请问:

  1. php8.1-fpm是否会定期终止并重启?
  2. 若确实如此,为何本次未自动重启?
日志内容
[11-Oct-2023 00:31:03] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 8 total children
[11-Oct-2023 00:31:05] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 0 idle, and 9 total children
[11-Oct-2023 00:31:09] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 10 total children
[11-Oct-2023 00:31:13] WARNING: [pool www] child 172325, script '/home/forge/my-site-address.com/releases/20231010085401/public/index.php' (request: "POST /index.php") execution timed out (60.698366 sec), terminating
[11-Oct-2023 00:31:13] WARNING: [pool www] child 171901, script '/home/forge/my-site-address.com/releases/20231010085401/public/index.php' (request: "POST /index.php") execution timed out (68.674962 sec), terminating
[11-Oct-2023 00:31:13] WARNING: [pool www] child 172325 exited on signal 15 (SIGTERM) after 513.394876 seconds from start
[11-Oct-2023 00:31:13] NOTICE: [pool www] child 172536 started
[11-Oct-2023 00:31:14] WARNING: [pool www] child 171901 exited on signal 15 (SIGTERM) after 1803.025691 seconds from start
[11-Oct-2023 00:31:14] NOTICE: [pool www] child 172537 started
[11-Oct-2023 00:31:33] WARNING: [pool www] child 172519, script '/home/forge/my-site-address.com/releases/20231010085401/public/index.php' (request: "POST /index.php") execution timed out (74.734089 sec), terminating
[11-Oct-2023 00:31:33] WARNING: [pool www] child 172519 exited on signal 15 (SIGTERM) after 80.438516 seconds from start
[11-Oct-2023 00:31:33] NOTICE: [pool www] child 172546 started
[11-Oct-2023 01:00:22] NOTICE: Terminating ...
[11-Oct-2023 01:00:23] NOTICE: exiting, bye-bye!
解答

关于php8.1-fpm是否定期终止重启

php-fpm主进程不会主动定期终止重启,只有两种场景会触发进程重启:

  • 子进程层面:若配置了pm.max_requests参数,子进程处理完指定数量的请求后会自动退出,主进程会立即启动新的子进程,这是为了避免长期运行导致的内存泄漏。
  • 主进程层面:收到外部信号(如SIGTERM、SIGINT)、系统资源耗尽被OOM Killer杀死,或者手动执行重启/停止命令时,主进程才会终止。

本次未自动重启的原因分析

从日志最后两行NOTICE: Terminating ...和exiting, bye-bye!可以看出,php-fpm主进程是主动收到终止信号后正常退出,而非意外崩溃。这种情况下是否自动重启取决于systemd的服务配置:

  • Laravel Forge默认用systemd管理php-fpm服务,如果服务配置中Restart参数未设置为always或on-failure,主动终止的服务不会触发自动重启。
  • 另外,Envoyer的零停机部署脚本可能在部署过程中发送了终止信号给php-fpm,但后续的重启步骤执行失败,导致服务未恢复。

同时,前期日志显示进程池持续繁忙,多个子进程因请求超时被终止,但主进程都正常重启了子进程,说明此时主进程运行状态正常,直到01:00:22收到终止信号才退出。

解决建议

  • 检查php-fpm的systemd配置:编辑/etc/systemd/system/php8.1-fpm.service,确保添加或修改Restart=always,然后执行systemctl daemon-reload && systemctl restart php8.1-fpm,让服务在任何退出场景下自动重启。
  • 优化php-fpm进程池配置:编辑/etc/php/8.1/fpm/pool.d/www.conf,提高pm.start_servers、pm.min_spare_servers、pm.max_spare_servers的值,缓解进程池繁忙问题;设置pm.max_requests = 500(可根据实际请求量调整),让子进程定期重启避免内存泄漏。
  • 排查请求超时问题:检查Laravel中耗时的POST请求逻辑,优化数据库查询、外部API调用等操作,或者调整php-fpm的request_terminate_timeout参数(不建议设置过大,避免占用过多服务器资源)。

内容的提问来源于stack exchange,提问作者Florin Frătică

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 16:02:12