Xdebug on PHPStorm with Homestead

Setup Xdebug on PhpStorm with Homestead

Xdebug is an extension for PHP to assist with debugging and development.

It provides a slightly better DX than doing a DD, VAR_DUMP or PRINT_R on php when you’re trying to inspect something.

  1. SSH into your homestead machine (vagrant ssh).

  2. Run this command to enable xdebug sudo phpenmod xdebug

  3. Install browser debugging extension on your primary browser, Here are links

  4. Map PHP Interpreter in phpstorm.

    • In phpstorm press ctrl + alt + s
    • Select Languages & Frameworks > PHP > CLI Interpreter
    • Click on + button & type your vagrant/homestead server details.
    • In General, Set PHP executable to: /usr/bin/php
  5. Setup path mappings

    • In phpstorm press ctrl + alt + s
    • Select Languages & Frameworks > PHP > Servers
    • Click on + button.
    • Enter host, post, debugger (=Xdebug from the drop down)
    • Tick Use path mappings
    • Set the corresponding project for local directory to absolute path on server. Eg: D:\sites\flipkart to /home/vagrant/flipkart
  6. Now, open your Laravel project in PHPStorm. Open web.php & go to the controller that handles / (root) of your project. (eg: HomeController@index) On the line that returns view or JSON, press ctrl + f8 to add debugging breakpoint. Note: This is important step, Without doing this the Xdebuger will finish execution without any output.

  7. Go to your browser, And open the project’s / or root url for which you set up the breakpoint (eg: flipkart.local)

Xdebug causes PHP to run significantly slower. To disable Xdebug, run sudo phpdismod xdebug within your Homestead virtual machine and restart the FPM service.

Here are some useful links if you get stuck

Set Up xDebug, with Homestead, PHPStorm and PHP7

Remote file path is not mapped to any file path in project

Debugging Web Requests With Xdebug

Debugging: Configure Xdebug + Laravel Homestead + VS Code + PHPUnit