基于 Windows + Laradock 在 vscode 上使用 xdebug 设置

最近学习在 windows 上使用 laradock,花了几天时间各种找资料学习外加设置,终于把 vscode 和 laradock 的 xdebug 连接上了。

安装 Xdebug 扩展

打开 laradock 目录下的 .env,搜索 WORKSPACE_INSTALL_XDEBUGPHP_FPM_INSTALL_XDEBUG 都改为 true

WORKSPACE_INSTALL_XDEBUG=true
PHP_FPM_INSTALL_XDEBUG=true

我的 Xdebug 配置

编辑 laradock/workspace/xdebug.ini 以及 laradock/php-fpm/xdebug.ini,我的具体配置如下:

xdebug.remote_host=docker.for.win.localhost
xdebug.remote_connect_back=0
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM

xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.cli_color=0
xdebug.profiler_enable=0
xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling"

xdebug.remote_handler=dbgp
xdebug.remote_mode=req

xdebug.var_display_max_children=-1
xdebug.var_display_max_data=-1
xdebug.var_display_max_depth=-1

然后重新创建镜像和容器,时间主要花在这里了,尤其 apt-get 时间相当长,光是这里 build 花了我至少 1 个多小时,(╯‵□′)╯︵┻━┻
$ docker-compose up -d nginx mysql redis workspace

我的 VS Code设置

vscode 主要是 pathMappings 这里,我把本地项目映射到了 /var/www/ 目录下,例如 /var/www/p2p/var/www/bbs

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

执行一下 Xdebug,连接成功,本来都要放弃使用 VS Code 的 ╮(╯▽╰)╭ ,现在终于不用换 PhpStorm 啦
基于 Windows + Laradock 在 vscode 上使用 xdebug 设置

参考: https://learnku.com/articles/24389#配置Xdebug

本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 2

废话不多说,温馨提示一下:
xdebug.ini 种有几个参数是设置无效的,因为在 Deockerfile 中有这么一段

RUN sed -i "s/xdebug.remote_autostart=0/xdebug.remote_autostart=1/" /etc/php/${LARADOCK_PHP_VERSION}/cli/conf.d/xdebug.ini && \
    sed -i "s/xdebug.remote_enable=0/xdebug.remote_enable=1/" /etc/php/${LARADOCK_PHP_VERSION}/cli/conf.d/xdebug.ini && \
    sed -i "s/xdebug.cli_color=0/xdebug.cli_color=1/" /etc/php/${LARADOCK_PHP_VERSION}/cli/conf.d/xdebug.ini

有点想不明白,给了配置还强行替换,被坑可惨了 :speak_no_evil:

4年前 评论

@ivothgle 额,忘了说了,我是 build 之后,进入 workspacephp-fpm 的 bash,直接用 vim 强行修改的╮(╯▽╰)╭

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!