IntelliJ、PhpStorm:使用 xdebug 进行调试会忽略 XDEBUG_SESSION cookie

IntelliJ, PhpStorm: Debugging with xdebug ignores XDEBUG_SESSION cookie

我正在尝试使用 Intellij 中的 PHP 插件调试 Drupal 网站(在 PhpStorm 中也是如此)。

我有以下设置:

Chrome 指向本地主机别名的浏览器 mydomain.local 并且 XDebug Helper 扩展已安装并设置为调试。在 Developer Tools 下的 Cookies 中,我可以看到 XDEBUG_SESSION cookie 设置为 PHPSTORM.

我已经使用以下设置为 xdebug 插件配置了 php:

xdebug.extended_info = 1
xdebug.idekey = "PHPSTORM"
xdebug.max_nesting_level = 500
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 0
xdebug.remote_enable = 1
xdebug.remote_handler = dbgp
xdebug.remote_host = 127.0.0.1
xdebug.remote_mode = req
xdebug.remote_port = 9000

在 IntelliJ 中,我设置了一个指向 mydomain.local 的服务器,在 运行 配置中,我正在使用该服务器并将 Ide 键设置为 PHPSTORM

现在的问题是:

如果我在 PHP 脚本 中启用 在第一行中断,那么调试器会立即在 index.php 中第一个可中断的位置中断。如果我禁用该选项,我会收到一条警告,指出没有命中断点,即使我设置了断点并且我确定代码正在执行。我看到的警告如下所示:

Debug session was finished without being paused
It may be caused by path mappings misconfiguration or not synchronized
local and remote projects.
To figure out the problem check path mappings configuration for 
'mydomain.local' server at PHP|Servers or enable Break at first line in
PHP scripts option (from Run menu).

现在,如果我显式使用 URL 并附加以下查询参数:?XDEBUG_SESSION_START=PHPSTORM 那么我的所有断点都会在 IntelliJ 中正确中断。

问题:为什么 XDEBUG_SESSION cookie 被忽略了?

更新: 添加了我的 PHP 和来自 php -v 的 XDebug 版本输出:

PHP 7.0.8-0ubuntu0.16.04.3 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.8-0ubuntu0.16.04.3, Copyright (c) 1999-2016,
    by Zend Technologies
    with Xdebug v2.4.0, Copyright (c) 2002-2016, by Derick Rethans

和我的 Apache 虚拟主机配置:

<VirtualHost *:80>
    DocumentRoot /var/www/html/mydomain
    ServerName mydomain.local
    <Directory /var/www/html/mydomain>
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/mydomain.log
</VirtualHost>

更新二: 我安装了一个名为 fpm 的 php 扩展。我不太确定为什么安装它或者我是否需要它。我认为它是使用 php 自动安装的。这会造成干扰吗?

我曾经在“过度配置”我的设置时遇到过这个问题。

  1. 您可能想尝试将 XDebug helper extension 添加到 chrome
  2. 添加后转到插件的设置和 select PhpStorm:

  1. 尝试将您的 xdebug 配置精简为这些值:

(适用于我的盒子)

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_port=9000
  1. PhpStorm 配置应包含调试端口 9000 和 [X] 可以接受外部连接:

  1. 那么你应该在 PhpStorm 中监听调试连接:

  1. 同时在浏览器中启用调试(通过 xdebug 助手):

  1. 如果 bug 图标是绿色的 ,那么如果你刷新页面,你应该可以开始了,PhpStorm 应该会在第一个断点处停止。

如果您使用 fpm 和 nginx,可能您指定了 fastcgi_pass FastCGI-server,它默认在端口 9000 上工作。如果是这样,请为 Xdebug 设置另一个空闲端口,例如 9009

在虚拟机中调试代码时,我在 PHPStorm 9 中遇到了同样的问题。但首先你的问题:

问题:为什么 XDEBUG_SESSION cookie 被忽略了?

回答:我不完全知道,但我怀疑基于它在您明确指定 ?XDEBUG_SESSION_START=PHPSTORM 或 "break on first line" 时有效,它会忽略传入的请求。它会完成它的工作,但不会在断点处停止,因为它认为当前请求不需要它。

我的解决方案

在调试浏览器调用时,帮助我将远程主机 name/ip 添加到 xdebug。或 export 从命令行调试时的那些设置。

对于xdebug.ini

xdebug.remote_host=mydomain.local;
xdebug.remote_connect_back = On

命令行用法:

export PHP_IDE_CONFIG=serverName=mydomain.local; php -dxdebug.remote_autostart=1 -dxdebug.remote_connect_back=1 -dxdebug.remote_host=mydomain.local ./script.php

然后设置path mapping。为此,请转到 Settings -> Languages & Frameworks -> PHP -> Server。 在那里 select 您设置的服务器,例如mydomain.local 如果存在并输入 mydomain.local 作为主机名,端口 80XDEBUG。 现在检查 "Use path mappings" 并向下滚动到您的 index.php 或您可以从浏览器访问的其他一些唯一入口点。 下一个听起来很愚蠢,但对我有用。如果您的 index.php 位于 /home/lanoxx/project/index.php 下,请在 "Absolute path on the server" 下输入 相同的 位置。然后在 index.php 文件中设置断点并从浏览器加载页面。

事实证明问题不在 IntelliJ 或 PhpStorm 中,而是特定于 Drupal。特别是我需要禁用所有缓存。

文章 Disable Drupal 8 caching during development 详细解释了执行此操作的步骤。

在我禁用缓存 Drupal 后,一切都按预期工作。

尝试这些配置,看看是否有帮助

zend_extension=/usr/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so
xdebug.remote_autostart = 1 <-------- THIS
xdebug.remote_enable = 1 <----------- AND THIS