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

hustnzj發表於2018-08-26
  • I used to debug only with var_dump or echo which was very stupid and exhausted. Laravel's default debug output mitigate the pain greatly, but if u understand how to debug in IDE, everything will be much easier!
  • The software versions as below:
    • homestead 6.1.0, revised by @Summer
    • phpstorm 2018.2

Start a debug session from web browser

file

Start a debug session from phpstorm

file

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

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.

  • 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

相關文章