0x00 描述

宿主机是 mac mini,构建的项目在 docker 中,所以需要在 PHPStorm 上配置 Xdebug 进行远程代码调试。

0x01 环境

宿主机:macOS High Sierra
Docker:Docker version -ce, build e68fc7a
容器环境:CentOS Linux release  (Core) + PHP  + nginx/ + Xdebug
PhpStorm:

0x03 下载安装 Xdebug

下载 Xdebug

首先要确定 Xdebug 的版本要和环境中的 PHP 版本相对应。

进入 docker 环境,打印 phpinfo() 的输出页面,或者在命令行输入

php -r "phpinfo();"

得到的结果,全选,复制,把结果粘贴到 https://xdebug.org/wizard.php 的文本框中,查看对应的 Xdebug 版本。

安装Xdebug

把得到的 Xdebug 版本下载到 docker 环境中,按照 https://xdebug.org/wizard.php 的说明进行安装:

Download xdebug-.tgz
Unpack the downloaded file with tar -xvzf xdebug-.tgz
Run: cd xdebug-
Run: phpize (See the FAQ if you don't have phpize.

As part of its output it should show:

Configuring for:
...
Zend Module Api No:
Zend Extension Api No:
If it does not, you are using the wrong phpize. Please follow this FAQ entry and skip the next step.

Run: ./configure
Run: make
Run: cp modules/xdebug.so /usr/lib64/php/modules
Update /etc/php.ini and change the line
zend_extension = /usr/lib64/php/modules/xdebug.so

0x04 配置 PHP 与 Xdebug

在 docker 中,根据 phpinfo 的 php.ini 路径,打开 php.ini,加入(编辑)以下代码:

[xdebug]
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=10.203.17.92
xdebug.remote_port=
xdebug.idekey=PHPSTORM
xdebug.auto_trace=
xdebug.auto_exception_trace=
xdebug.remote_autostart=
xdebug.collect_vars=
xdebug.collect_return=
xdebug.collect_params=
xdebug.show_local_vars=
xdebug.profiler_enable=
xdebug.trace_enable_trigger=
xdebug.remote_log="/var/log/php.xdebug.log"

有几个地方要注意一下:

1. xdebug.remote_host

这是宿主机的 ip,在 docker 外(mac 环境中),输入命令:

ifconfig

找到 en0 中的 inet:

en0: flags=<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu
    options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
    ether :fe:f7:0a::ff
    inet6 fe80::4f0:bb3b:4a49:d7f9%en0 prefixlen  secured scopeid 0x5
    inet 10.203.17.92 netmask 0xffffff00 broadcast 10.203.17.255
    nd6 options=<PERFORMNUD,DAD>
    media: autoselect (100baseTX <full-duplex>)
    status: active

xdebug.remote_host 就是上面例子中的 10.203.17.92

2. xdebug.remote_port

这里默认是 9000 端口,改一下避免和 php-fpm 的 9000 端口冲突

3. xdebug.remote_log

配置一下,方便排错

另外附上 /etc/php.d/xdebug.ini:

[xdebug]
; repeated defind at /etc/php.d/-xdebug.ini file
; zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable = on
xdebug.remote_host = 10.203.17.92
xdebug.remote_port =
xdebug.idekey = "PHPSTORM"

这里和 php.ini 保持一致。

0x05 配置 PHPStorm

1. 进入 preferences

2. 进入 Languages & Frameworks - PHP -Debug ,找到 Xdebug,编辑监听端口和 php.ini 保持一致。

3. 进入 DBGp Proxy,端口和 php.ini 中配置保持一致,配置如下:

4. 进入 Servers,点 “+” 添加一个测试项目,例子中是 admin,Name 随便填写,Host 写项目的域名,端口 80,Debugger 选 Xdebug,勾选下面的路径映射,在右侧填写与本地项目对应的远程目录

5. 保存。

6. 编辑配置,选择 Edit Configurations

添加一个 PHP Web Page,有的版本是 PHP Web Application,是一样的:

填写配置信息:

Server 选择上两步在 Servers 中添加的 Server,这里是 admin,另外 Start URL 选择项目的首页,即使是 https 的 URL,在上述配置端口的地方一样配置成 80:

7. 保存。

8. 调试:

在代码中设置断点(“1”处),打开“2” 处的电话图标,点击“3”处的 debug 选项,会自动跳到浏览器中之前设置的项目首页,当触发到断点时会自动跳到 PHPStorm 中,“4”处可以跳过断点,“5”处显示变量的值:

0x06 安装浏览器 Xdebug 插件

在 google 商店中找到并安装 Xdebug helper,安装之后可以在任意页面启动调试:

以下是 Xdebug helper 的设置页面:

在一些 https 的项目中,PHPStorm -> Preference -> Languages & Frameworks -> PHP -> Servers 处的端口可以配置成 443,但是这个时候通过 PHPStorm 的 Debug 按钮从 Start Url 处开始无法调试,原因是 Xdebug 会把调试的地址认为是 http://xxxx.com:443 而不是 https://xxxx.com,此时就可以用浏览器的 Xdebug helper 插件来直接 debug,选择下拉菜单中的第一个 Debug 选项就可以开始 debug 了。

macOS 下 PHPStorm + Xdebug 调试 Docker 环境中的代码的更多相关文章

  1. PHPStorm + Xdebug 调试PHP代码 有大用

    星期四, 12/26/2013 - 19:54 - shipingzhong PHPStorm + Xdebug 调试PHP代码 http://e.v-get.com/2013-11-20 16:55 ...

  2. Linux下用Xdebug调试php

    Linux下用Xdebug调试php 博客分类: php PHPLinuxZendEclipseC# 为了调试PHP程序,安装一下xdebug. 官方网址: http://www.xdebug.org ...

  3. PHP - MAC下PhpStorm安装调试环境xdebug

    今天下午一直在捣鼓如何用PhpStorm进行调试,查找了许多资料,零零碎碎的,所以自己弄篇文章记录一下步骤. 安装xdebug 使用brew安装xdebug,语法如下 brew install hom ...

  4. phpstorm+xdebug调试详细教程

    对于PHP开发,初来咋到,开发环境的搭建和理解感觉是最烦人的一件事了.不像JAVA,打开一个Eclipse就可以开搞,Php的Debug都要几个插件来配合工作.这些都是死的,好说.但是对于Xdebug ...

  5. PHPStorm+XDEBUG 调试Laravel

    首先输出phpinfo(); https://xdebug.org/wizard.php 打开然后查看适合你的调试扩展版本 ,目前支持到php7.2 整个页面ctrl+a  复制进去 然后下载 扩展文 ...

  6. centos7+nginx 1.9.0+php-fpm+phpstorm+xdebug+vmware开发环境搭建

    1.php-fpm yum install php-fpm 默认配置在本地9000端口监听 service php-fpm restart启动 2.nginx 1.9.0 需先安装gcc zlib o ...

  7. PhpStorm+xdebug调试——更新

    之前写过一篇<PhpStorm+xdebug+postman调试>,但是经过后来一段时间的使用,发现有些累赘.这里介绍一种比较简单方便的操作. 在上一篇文章中有一段“ 7.设置Server ...

  8. XAMPP + PhpStorm + Xdebug本地实验环境搭建

    参考: 下载合适的XDebug 点击这里,选择合适xdebug XAMPP配置 php_xdebug-xxxx.dll 拷贝dll至 D:\XAMPP\php\ext php.ini 文末追加 [XD ...

  9. docker环境中neo4j导入导出

    neo4j 官方文档有说明,使用 neo4j-admin restore / dump 导出和恢复数据库的时候需要停掉数据,否则会报数据库正在使用的错误:command failed: the dat ...

随机推荐

  1. SpringCloud学习目录

    Spring Cloud直接建立在Spring Boot的企业Java创新方法上,它通过实现经过验证的模式来简化分布式.微服务风格的体系结构,从而为您的微服务带来弹性.可靠性和协调. 以上来自spri ...

  2. vue-router那些事儿

    vue-router适用于单页面应用. 一.vue-router的引用方法1.用script标签 <script src="https://unpkg.com/vue-router/d ...

  3. LR特征维数特别大实时计算问题

    美团 https://tech.meituan.com/machinelearning-data-feature-process.html 维数灾难 待续...

  4. 潭州课堂25班:Ph201805201 django框架 第一课 环境搭建 (课堂笔记)

    在虚拟机中创建虚拟环 mkvirtualenv -p /usr/bin/python3 djtest mkvirtualenv 是创建命令, -p /usr/bin/python3  是指定在 pyt ...

  5. 逻辑运算的妙用-Single Number

    题目:一个int的array,除了一个元素只有一个其余的都是两个,找到这一个的元素. 使用:逻辑运算 XOR异或运算 关于逻辑运算的总结[转] &&和||:逻辑运算符 &和|: ...

  6. fastjson 使用教程

    fastjson 是阿里的开源项目,具网上的说法 fastjson 的解析速度是 Gson 的6倍,体积小,而且开源. 项目地址: https://github.com/alibaba/fastjso ...

  7. bzoj1026: [SCOI2009]windy数(数位dp)

    1026: [SCOI2009]windy数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8203  Solved: 3687[Submit][Sta ...

  8. NodeJS多进程

    NodeJS多进程 Node以单线程的方式运行,通过事件驱动的方式来减少开销车,处理并发.我们可以注册多进程,然后监听子进程的事件来实现并发 简介 Node提供了child_process模块来处理子 ...

  9. printf scanf cin cout的区别与特征

    printf和scanf是c语言的输入输出,学习c++以后,自然是用cin cout这两个更简单的输入输出 printf scanf 都需要进行格式控制,比较麻烦,但优点是速度比较快,毕竟多做了一些事 ...

  10. JS_高程3.基本概念(6)函数

    1.ECMAScript中的函数使用function关键字来声明. eg: function sum (num1,num2){ alert(num1+num2); } sum(3,7); 注意: 在有 ...