PHP经验分享PHP实战程序猿的进阶屋

Xdebug+PhpStorm远程调试

2017-10-15  本文已影响2197人  小武说

开发环境:

目标

过程

  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

注意事项

// 修改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读取返回数据的时间

参考文档:

上一篇下一篇

猜你喜欢

热点阅读