17

I setup Laravel Homestead. I then configured both homestead xdebug.ini and PHPStorm to make the debugging work.

Here is my xdebug.ini inside homestead

zend_extension=xdebug.so
xdebug.remote_autostart = on
xdebug.remote_enable = on
xdebug.remote_connect_back = on
xdebug.remote_port = 9000
xdebug.idekey = "vagrant"

To start a debugging session the steps I follow are

  1. In PHPStorm --> Start Listening for connections
  2. In PHPStorm set a breakpoint
  3. In my browser --> Use XDebug Chrome Helper OR add to my URL ?XDEBUG_SESSION_START=
  4. Load the page

This works perfectly. My problem is when I'm inside homestead command line and I run a php artisan command then I can't get it to hit my breakpoints.

What I've tried

  1. XDEBUG_CONFIG="idekey=PHPSTORM" PHP_IDE_CONFIG="serverName=server_name" php -dxdebug.remote_host="127.0.0.1" artisan mycommand

  2. php -d xdebug.profiler_enable=On artisan mycommand

  3. I also tried to set xdebug.remote_autostart=On then sudo service php5-fpm restart but still my breakpoints never get hit in PHPStorm

user391986
  • 29,536
  • 39
  • 126
  • 205

3 Answers3

27

Two things are important:

  1. remote_connect_back can not work in the CLI case because Xdebug can not detect the remote IP when you are in the console
  2. When using homestead / VirtualBox in the NAT network configuration, your development machine (which is running PHPStorm) does not have the IP 127.0.0.1 seen from inside the VM. Instead, it has usually an IP like 10.0.2.2. To find out the correct IP, have a look at your Apache's access.log,

The following worked for me:

php -dxdebug.remote_autostart=on -dxdebug.remote_connect_back=off 
  -dxdebug.remote_host=10.0.2.2 artisan
  1. edit If your breakpoints are not hit, you have to set up the folder mappings correctly (as your path in the IDE is different from what the web server sees:

Folder Mappings

  1. Do export PHP_IDE_CONFIG="serverName=yourservername" in your VM, where yourservername is what you configured in the screenshot under "name"

  2. Add a Php Remote Debug Configuration with an IDE key and the server configured above Debug Configuration

  3. And add your IDE key and the remote_host to the VM's XDEBUG-CONFIG

    export XDEBUG_CONFIG="idekey=artisan remote_host=10.0.2.2"

References: http://randyfay.com/content/remote-command-line-debugging-phpstorm-phpdrupal-including-drush

sepehr
  • 17,110
  • 7
  • 81
  • 119
Alex
  • 32,506
  • 16
  • 106
  • 171
  • You add breakpoints in your IDE with clicking on the beginning of the line. Or are you asking something else? – Alex May 15 '15 at 21:57
  • Using these parameters works partially: the debugger stops at artisan main(). I can go step by step through the autoiloading process, but when I hit the run button, it misses the breakpoint in the command's fire() method – Salvador Maine Jun 23 '15 at 12:29
  • @SalvadorMaine: Edited my answer (see step 3.) – Alex Jun 24 '15 at 13:55
  • 1
    I think there is something more... I just added point 4. Give it a try – Alex Jun 26 '15 at 08:43
  • This answer should be marked as correct! Works flawlessly! – bx2 Mar 17 '16 at 12:46
  • this works for me even when I use an ssh tunnel with 127.0.0.1 – NiRR Aug 03 '16 at 06:41
  • I had to use only this option: export XDEBUG_CONFIG="idekey=artisan remote_host=10.0.2.2" And everything started to work. Thank you very much! – Piotr Suchanek Mar 24 '17 at 11:31
  • I have not been able to get this to work yet using Windows 10 with a Homestead (Vagrant Ubuntu) guest and Netbeans. I'm not sure if I'm following all the steps correctly. E.g. Homestead uses Nginx instead of Apache, and I'm not positive what my Homestead IP is. Is `ip addr show` a fair way to tell? That shows a whole bunch of output including `inet 10.0.2.15/24 brd 10.0.2.255 scope global enp0s3` among other stuff. But I still have no luck, even trying your instructions with `10.0.2.15`. – Ryan Sep 22 '17 at 22:30
  • @Ryan did you check the access.log of Apache for the right IP? – Alex Sep 22 '17 at 22:35
  • @Alex Homestead seems to be using Nginx instead of Apache. And the Nginx access logs look empty. I just learned of this command: `printenv`. The output of that contains (among other lines) `SSH_CLIENT=10.0.2.2 60362 22`. So that looks like `10.0.2.2` should be the IP to use, correct? – Ryan Sep 23 '17 at 01:04
  • **Thank you**, it finally worked, still the process is somewhat awkward, as first you have to start `php artisan serve` and on top of that start debug **remote debug** configuration. I thought it's as straightforward as run the server and click listen for incoming connections.. Oh well, better this, than nothing at all. –  Dec 14 '17 at 11:19
  • Hi Alex, could you please see if you could answer this related one? Of the people I've found on the internet so far, you seem to be the person most likely to understand it: https://stackoverflow.com/questions/54711818/xdebug-of-laravel-dusk-in-netbeans-on-homestead Thanks. – Ryan Feb 15 '19 at 14:52
  • Thank you, works very well. I just had to adapt a little bit to make it work on a docker container : get your host IP from within the container running the server and PHP, by running the command `/sbin/ip route|awk '/default/ { print $3 }'` . Then follow same steps as above, except replace `-dxdebug.remote_host=10.0.2.2` with `-dxdebug.remote_host=` – vittorio Dec 20 '21 at 13:18
23

Or, if that all is just too complicated or not working - you can trigger your artisan command via a url (route) using

Artisan::call('whatever:command');
Sabrina Leggett
  • 9,079
  • 7
  • 47
  • 50
  • 1
    Awesome. I have wasted 2 hours of my precious life on setting up thing otherwise, but all in vain. And here comes this brilliant reply to save the day :-) – hhsadiq Dec 28 '17 at 09:37
  • 1
    I've tried configuring this in the past and still have had no success.... this is really the easiest solution, I just create a route that I will comment out on deployment.... sorta just wasted to much time on this config and this seems to be a viable solution, thanks. – David Jarrin Dec 29 '17 at 16:52
  • It works but I wish I could figure out how to get it working without it! If anyone gets a solution please post it! – samuraiseoul Jun 18 '18 at 16:49
1

In most recent versions of Homestead you can do xphp artisan whatever and phpstorm will popup a windows automatically.

https://laravel.com/docs/8.x/homestead#debugging-cli-applications

Tadeo Rod
  • 574
  • 5
  • 11