Skip to content

Instantly share code, notes, and snippets.

@Im0rtality
Last active March 17, 2023 14:36
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save Im0rtality/9710254 to your computer and use it in GitHub Desktop.
Save Im0rtality/9710254 to your computer and use it in GitHub Desktop.
PHP CLI Debugging in Vagrant using Xdebug and PHPStorm

In host:

  1. Go to PHPStorm Settings > Project settings > PHP > Servers
  2. Add server with following parameters:
    • Name: vagrant (same as serverName= in debug script)
    • Host, port: set vagrant box IP and port
    • Debugger: Xdebug
    • [v] Use path mappings
    • Map your project root in host to relative dir in guest
    • Hit OK
  3. PHPStorm > Run > Start listen for PHP Debug connections
  4. Put a breakpoint

In guest:

  1. Find where your Xdebug config is stored. Run $ php -i | grep ini and look for *xdebug.ini or *custom.ini - depends on configuration (mine was in /etc/php5/cli/conf.d/zzzz_custom.ini)
  2. Add following xdebug.remote_host = 11.22.33.1, when box ip is 11.22.33.*
  3. Create file /usr/bin/debug with contents added below (file debug below)
  4. Make it executable $ sudo chmod 0777 /usr/bin/debug
  5. Run your script prefixed with debug:
    • $ debug app/console cache:clear
  6. Debugger window should open in IDE
#!/bin/sh
env PHP_IDE_CONFIG="serverName=vagrant" XDEBUG_CONFIG="idekey=PHPSTORM" $@
# for Symfony apps use following
# env PHP_IDE_CONFIG="serverName=vagrant" XDEBUG_CONFIG="idekey=PHPSTORM" SYMFONY_DEBUG="1" $@
@pavarnos
Copy link

pavarnos commented Aug 8, 2019

you can also

  • in phpStorm Settings > Run > Edit Configurations
  • add a PHP Script configuration
  • set the file to the full local filesystem path eg /your/path/to/app/console
  • set arguments eg cache:clear
  • set environment variables to PHP_IDE_CONFIG="serverName=vagrant";XDEBUG_CONFIG="idekey=PHPSTORM"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment