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

开启Xdebug时PHP出现SIGSEGV崩溃问题求助

PHP-FPM进程因Xdebug触发SIGSEGV崩溃(Laravel 10 + Docker环境)

问题描述

本地Docker开发环境使用php-fpm 8.1.26、Nginx、Laravel 10及PhpStorm 2023.3.1,此前运行正常。开启Xdebug后,PHP-FPM进程频繁触发SIGSEGV(段错误)崩溃,日志如下:

| [21-Dec-2023 09:44:34] WARNING: [pool www] child 9 exited on signal 11 (SIGSEGV - core dumped) after 127.958617 seconds from start
   | [21-Dec-2023 09:44:34] NOTICE: [pool www] child 11 started
   | [21-Dec-2023 09:44:36] WARNING: [pool www] child 10 exited on signal 11 (SIGSEGV - core dumped) after 129.519516 seconds from start
   | [21-Dec-2023 09:44:36] NOTICE: [pool www] child 12 started
   | [21-Dec-2023 09:44:38] WARNING: [pool www] child 11 exited on signal 11 (SIGSEGV - core dumped) after 4.094282 seconds from start
   | [21-Dec-2023 09:44:38] NOTICE: [pool www] child 13 started
   | [21-Dec-2023 09:44:58] WARNING: [pool www] child 12 exited on signal 11 (SIGSEGV - core dumped) after 22.402854 seconds from start
   | [21-Dec-2023 09:44:58] NOTICE: [pool www] child 14 started

禁用Xdebug后应用恢复正常,且Xdebug可正常命中index.php及Laravel启动文件的断点,请求能完成初始化、服务注册,但运行到某一阶段进程即终止。

附相关配置:

Nginx配置

server {
    listen 8001 default_server;
    listen [::]:8001 default_server;
    server_name ${NGINX_HOST};

    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php_centralised_app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # All static files will be served directly.
    location ~* /(styles|images|scripts|storage)/ {
        location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|woff2|svg)$ {
            access_log off;
            expires 1y;

            tcp_nodelay off;

            open_file_cache max=3000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }
    }

    # We don't need .ht files with nginx.
    location ~ /\.ht {
        return  301 /;
    }
}

PHP Dockerfile配置

FROM php:8.1.26-fpm

RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        nano \
        libxslt1.1 \
        libxslt1-dev \
        unzip \
        git \
        gnupg \
        libpq-dev \
        libzip-dev \
        iputils-ping \
        supervisor \
    && docker-php-ext-install zip \
    gd \
    mysqli pdo pdo_mysql  \
    && docker-php-ext-enable pdo_mysql \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-configure intl

# Install xdebug
RUN pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.client_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.log='/tmp/xdebug.log'" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "max_input_vars='10000'" >> /usr/local/etc/php/conf.d/php-extra.ini \
    && echo "" >> /tmp/xdebug.log

    #create user "laravel" and give them permission to write xdebug files. The same user is used to run container on the end file
RUN addgroup --gid 1000 laravel
RUN adduser --ingroup laravel --shell /bin/sh laravel

RUN chown laravel:laravel /tmp/xdebug.log
RUN chmod 777 /tmp/xdebug.log

RUN echo "memory_limit='512M'" >> /usr/local/etc/php/conf.d/php-extra.ini;

ENV PHP_IDE_CONFIG "serverName=threat_match_central_app"

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer --version=2.3.8 && chmod +x /usr/bin/composer

# Set timezone
RUN rm /etc/localtime \
    && ln -s /usr/share/zoneinfo/Europe/Warsaw /etc/localtime \
    && "date" \
    && printf '[PHP]\ndate.timezone = "Europe/Warsaw"\n' > /usr/local/etc/php/conf.d/tzone.ini

WORKDIR /var/www/html

排查与解决方案

1. 匹配Xdebug与PHP版本兼容性

PHP 8.1.26需搭配兼容的Xdebug版本,3.3+版本对PHP 8.1可能存在兼容性问题,建议指定安装3.2.x版本:
修改Dockerfile中Xdebug安装命令:

RUN pecl install xdebug-3.2.2 \
    && docker-php-ext-enable xdebug

2. 分析Xdebug日志定位崩溃点

查看/tmp/xdebug.log内容,崩溃前的日志会标记触发问题的代码位置:

# 进入PHP容器后执行
cat /tmp/xdebug.log

若日志过大,可清空后重新触发崩溃,仅保留最新日志片段。

3. 调整Xdebug配置减少资源占用

禁用不必要的Xdebug功能,避免复杂数据结构处理时触发崩溃:
修改docker-php-ext-xdebug.ini:

xdebug.mode=debug
xdebug.client_host=host.docker.internal
xdebug.start_with_request=yes
xdebug.log='/tmp/xdebug.log'
xdebug.auto_trace=0
xdebug.var_display_max_depth=3
xdebug.var_display_max_children=256

4. 排查PHP扩展冲突

部分扩展(如opcache、gd)可能与Xdebug冲突,可临时禁用非必需扩展,逐一测试:
注释Dockerfile中部分扩展安装命令,重新构建容器验证。

5. 定位Laravel代码触发点

由于断点能走到初始化阶段,重点排查:

  • 服务提供者boot方法中涉及复杂数据处理、第三方调用的逻辑
  • 全局路由中间件的执行代码
  • 使用反射、动态类加载或递归的代码段,可临时注释逐步缩小范围

6. 调整PHP-FPM进程配置

段错误可能与进程资源限制有关,修改www.conf:

RUN echo "pm.max_children = 20" >> /usr/local/etc/php-fpm.d/www.conf
RUN echo "request_terminate_timeout = 300s" >> /usr/local/etc/php-fpm.d/www.conf

7. 重新构建Docker镜像

清理镜像缓存,避免扩展安装不完整:

docker build --no-cache -t your-php-image .

内容的提问来源于stack exchange,提问作者Klick

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 21:47:05