How to configure a Vagrant (Homestead) VM in Phpstorm with Xdebug on Mac

How to configure a Vagrant(homestead) VM in Phpstorm with Xdebug on Mac

The debug result of the same script are different by two debug ways

Start a debug session from web browser

file

Start a debug session from phpstorm

file

How to configure

The web browser way (Starting from browser)

  • This way is the most common and very useful for usual development

  • Install Xdebug. It's default installed in homestead.

  • Create a sever. That's very important!

    • Open the Preferences, go to the Languages & Frameworks > PHP > Servers, adding a server. Input the Name of server, the Host name and port of the URL u will debug, check Use path mappings, and type the root path of Project files on local and remote.
      file
  • Edit the xdebug configuration file.

    • On the vagrant VM side, make sure the xdebug.ini contents as below. The homestead contains four php versions, make sure u edit the right one.
      zend_extension=xdebug.so
      xdebug.remote_enable = 1
      xdebug.remote_connect_back =1
      xdebug.remote_port = 9000
  • Validate debugger configuration on the web server. ( Validate result will tell you how to do. U can see Remote host is configured as ..., don't mind it, as we configure remote_connect_back=1 in the xdebug.ini)

    file

  • Install browser toolbar.

  • Enable listening for PHP Debug Connections.

  • All the three steps are too easy to be mentioned.
  • Type the correct Debug port in the Xdebug section of Languages & Frameworks > PHP > Debug, which is the same value with the one in xdebug.ini.

The cli way (Starting from PhpStorm)

  • This way is very useful for debugging single php script file such as artisan.

  • Add the CLI Interpreter. Using the Vagrant way.

    file

  • The Path mappings will be filled automatically.
    file
  • If u can't find the cli xdebug (That's the default), u could copy the same file from the fpm folder to the cli folder. Don't use the command sudo phpenmod -s cli xdebug, because this command will create a symbolic link to the real xdebug.ini file which will lead to the same debug port on cli and web browser debuging. But it seems that two ways cannot work on the same debug port! That's Key!. So u need to edit a bit of the new copied file, just change the xdebug.port to 9001 or others except 9000 ( 9000 seems to be used by web browser debugging by default and unchangeable) as below:
    zend_extension=xdebug.so
    xdebug.remote_enable = 1
    xdebug.remote_connect_back =1
    xdebug.remote_port = 9001

How to debug

Now, if u want to debug by browser, just keep the default 9000 xdebug port in PhpStorm . If u want to debug by cli, u can just change it to 9001 or other No you set in the xdebug.ini file in the cli folder.

Have a good trip in PHP development! :smile:

References:

Remarks:

  • If u have installed the barryvdh/laravel-debugbar, the debug will break at the first line in the public/index.php which is not convenient. My solution is to uncheck the Break at first line in PHP scripts as below:
    file
本作品采用《CC 协议》,转载必须注明作者和本文链接
日拱一卒
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
93
粉丝
85
喜欢
153
收藏
121
排名:71
访问:11.4 万
私信
所有博文
社区赞助商