美文网首页
[PHP] docker + vscode + xdebug 断

[PHP] docker + vscode + xdebug 断

作者: w_w_wei | 来源:发表于2019-09-24 00:54 被阅读0次

前提条件

  1. 容器中安装了 XDebug
  2. 编辑器 VS Code

说明

  1. 并不需要在容器中额外为 xdebug ,开启端口监听
  2. vscode 会开启一个监听端口, 由 xdebug 主动连接
  3. 这里使用用 ctwj/nginx_php7 镜像

配置

1. VS Code

在调试页配置php调试环境,
会在项目根目录生成 .vscode 文件夹,
其中 launch.json 需要配置,配置如下:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "pathMappings": {
                "/var/www/html": "${workspaceRoot}"
            },
            "log": true,
            "port": 9000,
            "externalConsole": false,
            "ignore": [
                "**/vendor/**/*.php"
            ]
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000
        }
    ]
}

配置完成,启动调试后, 会在本机开启 9000 端口.

2. XDebug 配置

配置文件 /etc/php.d/xdebug.ini

zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_connect_back=0
xdebug.remote_host=docker.for.mac.localhost
xdebug.idekey=VSCODE
xdebug.remote_autostart=1
xdebug.remote_log=/tmp/xdebug.log

使用

vscode中 打断点, 尝试浏览器访问,如果代码经过断点, 会断在断点处

连接不上

  1. 在容器中查看 /tmp/xdebug.log 可以定位问题
  2. 最常见问题 xdebug.remote_host 不对, 改为实际ip地址即可

使用示例

覆盖 xdebug 配置文件

docker run -d -p 80:80 -v /website:/var/www/html -v /website/xdebug.ini:/etc/php.d/xdebug.ini ctwj/nginx_php7

附 remote_host 可选值

  • docker.for.win.localhost
  • docker.for.mac.localhost
  • host.docker.internal

其中容器默认使用的是 docker.for.mac.localhost

相关文章

网友评论

      本文标题:[PHP] docker + vscode + xdebug 断

      本文链接:https://www.haomeiwen.com/subject/zpvxyctx.html