Laradock 使用 PhpStorm Debug 代码

安装 Xdebug 扩展

  • 编辑 laradock 目录下 .env 文件,修改以下两项
    WORKSPACE_INSTALL_XDEBUG=true
    PHP_FPM_INSTALL_XDEBUG=true
  • 修改后执行 docker-compose build php-fpm workspace 使配置生效

配置 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=0
      xdebug.remote_enable=0
      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
    • 其中 remote_host 很关键。我使用的是 windown,如使用 Mac 应修改为 docker.for.mac.localhost **

      配置 PhpStorm

  • 配置 PHP server,具体配置如图
    file
    ** 其中server name 需和 laradock 目录中 .env 文件中 PHP_IDE_CONFIG 配置项一致,默认为 laradock **

  • 配置、验证 Xdebug

    • 配置保持默认配置即可,修改完配置后需校验配置
      file
    • 点击 Validate 后界面如图
      file
      点击下方 Validate 按钮,校验成功后如图
      file
      填写校验脚本地址时,需注意,如项目为 laravel ,目录必须为 public 目录。在这一步我被坑了很久,直接填写项目目录校验会 404
    • 添加 debug 项目配置
      run -> Edit Configurations,添加 PHP Remote Debug。其中 server 为 laradock 的 PHP_IDE_CONFIG 配置项,默认为 laradock,IDE key 为 PhpStorm。如图:
      file
    • 配置完成后,选择上一步配置的 remote debug 启功,设置断点,刷新浏览器项目网页,即可开始调试
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 7

其中 remote_connect_back 参数很关键,如果设置为 1 ,remote_host 则会失效。可能导致 phpstorm 跟 laradock 通信出错,无法 debug

4年前 评论

配置 PHP server 和Validate的时候host怎么填?

4年前 评论
Mortence (楼主) 4年前

这个文档好多坑啊!踩坑完毕。

4年前 评论

https://www.onthink.com/2019/02/20/mac-lar...
推荐看这篇
按照链接中的设置
然后让 phpstorm 右上角的电话 变绿色
刷新网页 就会触碰断点
不想调试的时候 让电话图标颜色变红色即可
看了很多相关教程 我发现链接中的实现是最简单的

4年前 评论
Mortence (楼主) 4年前

参考楼主的步骤,在验证 Xdebug的时候,提示:no debug extension is loaded

4年前 评论

@lhp3851 那就说明 xdebug 没安装成功或者没有加载成功

4年前 评论

:blush:@Mortence

我参考了楼主和Laradock的提示,终于可以了,这里贴上我的配置:
xdebug.remote_host=docker.for.mac.localhost

xdebug.remote_connect_back=0

xdebug.remote_port=9000

xdebug.idekey=PHPSTORM

xdebug.remote_autostart=1

xdebug.remote_enable=1

xdebug.cli_color=1

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

4年前 评论

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