Xdebug+PhpStorm远程调试

作者: 小武说 | 来源:发表于2017-10-15 17:16 被阅读2197次

    开发环境:

    • Windows笔记本:日常开发使用机器,安装PHPStrom
    • Linux远程服务器:安装php-fpm/nginx等所有开发依赖环境,IP: 172.16.0.182
    • 【注意】,Windows通过vpn访问到Linux服务器,也就是说Windows->Linux是可以通过IP直接访问的,而Linux->Windows无法直接连通,因为对Linux而言,Windows开发机的IP只有VPN的网关地址
    • Windows和Linux之前通过PhpStorm的sftp进行代码同步

    目标

    • 远程调试(remote debug):也就是在windows本地调试远程服务器(Linux)上的代码
    • 日常开发用Windows中在PhpStorm中设置断点,可调试远程Linux远程服务器(172.16.0.182)的代码

    过程

    1. Linux远程服务器上,PHP安装Xdebug插件

      zend_extension = /home/work/php70/lib/php/extensions/no-debug-non-zts-20151012/xdebug.so
      
      xdebug.remote_host=127.0.0.1
      ; //注意这里是,客户端的ip<即IDE的机器的ip,不是你的web server>
      
      xdebug.remote_enable=on
      xdebug.remote_port = 9001
      ;//注意这里是,客户端的端口<即IDE的机器的ip,不是你的web server>
      ;// 由于VPN缘故,Linux无法直接访问Windows,需要先指定为Linux本机的9001端口,再通过ssh端口映射的功能转发至Windows机器
      
      xdebug.remote_log = /tmp/xdebug.log
      

    2. Windows上配置phpStorm

      2.1 SFTP同步远程代码

    sftp.png

    2.2 设置PHP-Servers为远程Linux服务器,调试模式为Xdebug


    xdebug-settings-servers.png

    2.3 新建调试配置:Run-> Edit Debug Configuration

    edit-debug-configuration.png
    1. Windows上开启Xshell端口映射(隧道->TCP/IP转移)

      将Linux的9001端口请求转发至Windows的9001端口上,这里通过Xshell的隧道方式实现:类型选择Remote(Incomming)

      xshell-tunnel.png

    在xshell中通过ssh连接Linux远程服务器后,查看->隧道窗格->转移规则,可见远程9001已和本地的9001建立连接


    xshell-tunnel-connecting.png
    1. Windows开启调试,访问远程服务器代码的url:http://172.16.0.182:8088/

      • 选择新建的Xdebug配置项

      • 点击绿虫子调试按钮(Shift+F9)开启调试


        start-debug.png
      • 点击调试按钮后,会自动打开浏览器访问:http://172.16.0.182:8088/?XDEBUG_SESSION_START=17461

        这里XDEBUG_SESSION_START=17461会在cookie中设置XDEBUG_SESSION=17461,而phpStorm一直在监听17461的idekey来访问,如下图:

    xdebug-waiting.png
     访问到断点时会进入ide的debug模式,Enjoy~
    
    debugging.png

    注意事项

    • 此例中只通过任意的idekey和phpStorm的调试按钮来协调开始调试模式,而不是一些教程中设置特定的idekey=PHPSTORM和浏览器插件(Xdebug Helper)来启动调试,后者在笔者环境下未成功,望知晓
    • 本案例中特殊之处在于vpn访问的特殊网络结构,单向通信容易,逆向则需要借助端口转发工具
    • 为避免调试过程中Nginx返回502超时,需要修改php-fpm和nginx设置,如下:
    // 修改php-fpm.conf配置
    ; The timeout for serving a single request after which the worker process will
    ; be killed. This option should be used when the 'max_execution_time' ini option
    ; does not stop script execution for some reason. A value of '0' means 'off'.
    ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
    ; Default Value: 0
    ;request_terminate_timeout = 0
    request_terminate_timeout = 300 //这里用0或设置5分钟
    
    // 修改nginx中fastcgi超时时间
    fastcgi_read_timeout 7200s;  // 设置fastcgi_read表示nginx从php-fpm读取返回数据的时间
    

    参考文档:

    相关文章

      网友评论

        本文标题:Xdebug+PhpStorm远程调试

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