19

I regularly use xdebug to debug applications, I've built a laravel application that takes an upload of a csv inserts the data to the database and the ids to a job queue.

I've written an artisan command to be run via cron to then do something with this data.

Xdebug works for accessing the site via the browser, but its not breaking on breakpoints when ran from cli.

I run php5-fpm. My files /etc/php5/fpm/php.ini and /etc/php5/cli/php/ini both contain the following settings:

zend_extension=/usr/lib/php5/20121212/xdebug.so 
xdebug.remote_enable = 1 
xdebug.idekey = 'dev_docker' 
xdebug.remote_autostart = 1 
xdebug.remote_connect_back = {{my host ip}} 
xdebug.remote_port = 9000 
xdebug.remote_handler=dbgp

I then run the artisan command

php artisan jobqueue::process --batch-size=10 --sleep=10

I know the command is running as ->info('text') is displayed in the terminal

Anyone know what I'm missing?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
James Kirkby
  • 1,716
  • 5
  • 24
  • 46
  • This related question looks helpful to me: https://stackoverflow.com/q/27936323/470749 – Ryan Sep 22 '17 at 22:31
  • Had the same problem, took me more than an hour to figure it out, here's a working solution https://stackoverflow.com/questions/1947395/how-can-i-debug-a-php-cli-script-with-xdebug/61865143#61865143 – 8ctopus May 18 '20 at 08:15

8 Answers8

36

Maybe this will help someone.

In short I had the same problem but I didn't have luck with accepted answer. My solution is to run this from the command line:

php -dxdebug.remote_enable=1 -dxdebug.remote_autostart=on -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 artisan my:command
boroboris
  • 1,548
  • 1
  • 19
  • 32
  • 1
    This didn't work for me. Netbeans still just says "running" and didn't stop at the marked line. – Ryan Sep 22 '17 at 22:04
  • 3
    You should add `dxdebug.remote_autostart=on` – Dragas Jun 15 '18 at 11:24
  • Hi Boroboris, could you please see if you could answer this related one? https://stackoverflow.com/questions/54711818/xdebug-of-laravel-dusk-in-netbeans-on-homestead – Ryan Feb 15 '19 at 14:51
  • 1
    This should be marked as correct answer. `xdebug.remote_autostart=on` was the one. – jayarjo Jul 12 '19 at 07:29
  • This worked for me on Windows (XAMPP) but I had to format the command with a space after each `-d` option for it to work: `php -d xdebug.remote_enable=1 -d xdebug.remote_autostart=on -d xdebug.remote_mode=req -d xdebug.remote_port=9001 -d xdebug.remote_host=127.0.0.1 -f artisan my:command` – MarijnK Jan 30 '20 at 08:25
  • 1
    If using docker: `-dxdebug.remote_host=host.docker.internal` – Nickson Yap May 25 '20 at 14:43
13

If you're using XDebug version 3, try:

php -dxdebug.mode=debug -dxdebug.client_host=host.docker.internal -dxdebug.client_port=9003 -dxdebug.start_with_request=yes artisan your:command

Update 2023-08

I'm using xDebug v3.2.1, php v8.2. Add these lines to xdebug.ini file if you don't want to write those long command every time. /etc/php/8.2/modes-available/xdebug.ini

zend_extension=xdebug.so
xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
xdebug.start_with_request=yes
Alex
  • 1,148
  • 8
  • 30
Richard Arets
  • 131
  • 1
  • 3
3

I got it working with remote_autostart=1 and setting the PHP_IDE_CONFIG environment variable to "serverName=localhost". localhost is the name of your server config in PHPStorm. Now when I run php artisan I can break at the regular breakpoints.

Let me be more clear :)

If you've got xdebug working with PHPStorm and regular requests this is what you should do to get it working with command line php (artisan).

You have configured paths in PHPStorm so it knows which file it should show you with the breakpoints. These paths are configured under a server (Preferences -> Languages & Frameworks -> PHP -> Servers).

The name of this server should be the serverName value in the PHP_IDE_CONFIG environment variable.

Pepijn
  • 1,439
  • 17
  • 16
  • finally! I was using the right xdebug config but xdebug always fail. The issue was I need to set the server config in phpstorm with the right port and xdebug is running. – Hilman Nihri Aug 03 '21 at 19:18
3

If you use vagrant, than you can create artisandebug shell file.

#!/bin/bash
HOST=10.0.2.2

# xdebug 3
php -dxdebug.mode=debug -dxdebug.start_with_request=yes -dxdebug.client_host=$HOST -dxdebug.client_port=9003 artisan  "$@"

# xdebug < 3
# php -dxdebug.remote_autostart=on -dxdebug.remote_connect_back=off -dxdebug.remote_host=$HOST -dxdebug.client_port=9003 artisan  "$@"

Than make it executable and run command :

chmod +x artisandebug

./artisandebug some:command --there
yaroslawww
  • 978
  • 9
  • 12
1

According to xdebug.remote_connect_back documentation it's using $_SERVER['REMOTE_ADDR'] to get debugging host. I guess that in CLI you must use xdebug.remote_host instead.

Furgas
  • 2,729
  • 1
  • 18
  • 27
  • Hi Furgas, could you please see if you could answer this related one? https://stackoverflow.com/questions/54711818/xdebug-of-laravel-dusk-in-netbeans-on-homestead – Ryan Feb 15 '19 at 14:51
0

For profiling with xdebug 3.1.1 and docker

php 
-dxdebug.mode=profile 
-dxdebug.client_host=IP_SERVICE_WITH_PHP 
-dxdebug.client_port=XDEBUG_CLINT_PORT 
-dxdebug.start_with_request=yes 
-dxdebug.output_dir=/tmp 
artisan ARTISAN_COMMAND

Example

php -dxdebug.mode=profile -dxdebug.client_host=172.19.0.3 -dxdebug.client_port=9003 -dxdebug.start_with_request=yes -dxdebug.output_dir=/tmp artisan help
Agafonov
  • 11
  • 1
0
php -dxdebug.mode=debug -dxdebug.client_host=localhost -dxdebug.client_port=9003 -dxdebug.start_with_request=yes artisan ARTISAN_COMMAND
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

An alternative approach would be to:

  1. First start debugger
  2. Run: php artisan tinker
  3. Within tinker run: Artisan::call('your-command');