docker中php的xdebug断点调试

2018-06-17  本文已影响0人  有时右逝

前言

端午佳节,难得放松。复习下php的开发过程。其中对php的调试一直还处于原始状态。于是计划进行改变。尝试基于热门的xdebug进行断点调试。

最底部提供了docker-compose脚本下载。

问题

本地开发中,构建的lnmp环境都是使用的docker。和传统的php下的xdebug存在差异。目前调试方法太过于原始,还是echo 、print_r 、var_dump 输出变量调试。
本次期望能和java语言一样,可以在ide中进行断点调试。

解决过程

1、xdebug的原理了解。


image.gif

xdebug是php的扩展,会搜集php执行的信息,然后发送给指令的远程ip+port。这里通常我们使用的开发工具会监听某个port。接受到php执行信息。再debug界面进行显示。

2、首先需要安装xdebug扩展。
这里我们是基于docker的php环境。在php的dockerfile文件中添加如下句子即可。

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

增加xdebug的配置。

[XDebug]
xdebug.enable=1
xdebug.remote_enable=1
xdebug.idekey=PHPSTORM 
;这个是约定的调试码,需要在phpstorm里面设定
xdebug.remote_host=172.16.0.5
;这个是宿主ip
xdebug.remote_port=19001  
;这个是xdebug对编辑器链接的端口
xdebug.remote_log=/var/log/php/xdebug_remote.log

3、IDE的配置
file-setting中配置监听的端口。和上一步的端口一致。

image.png

然后点击


image.png

此时可以运行一下命令,查看端口是否被监听。

image.png

4、安装浏览器插件。xdebug helper

image.png

配置选项为


image.png

5、配置debug服务器。

image.png image.png

可以点击验证下


image.png

6、最终的测试效果

设置断点-点击debug启动-访问web项目
即可看到效果。

image.png

注意事项

deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib
deb-src http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib
Log opened at 2018-06-16 13:36:33
I: Connecting to configured address/port: 172.21.0.1:19001.
W: Creating socket for '172.21.0.1:19001', poll success, but error: Operation now in progress (29).
E: Could not connect to client. :-(
Log closed at 2018-06-16 13:36:33
lsof -i:19001

来确保生效了。我之前配置错了。监听的是9000端口。导致一直失败。

2019-07-20 10:57:59更新这里
配置文件映射的时候 ,不同的框架可能存在差异,这里thinkcmf如图。

image.png image.png

2019-07-20 10:58:03更新结束

文件脚本下载。

这里提供一份我的脚本。仅供参考。目前我测试运行 良好。
使用该脚本需要docker和docker-compose知识支持。需要自行去研究。

下载地址。https://github.com/jsRuner/lnmp/releases/tag/xdebug

简单教程:

image.png
上一篇 下一篇

猜你喜欢

热点阅读