72

I have managed to initiate php-cli script debug session from the IDE itself, but I need to start the debugging session from the shell / command line. These are rather complex maintenance PHP scripts which take a lot of input parameters, so entering arguments from within Netbeans is a bit cumbersome.

I have done it before with Zend studio: https://zend18.zendesk.com/hc/en-us/articles/203838096-Debugging-PHP-CLI-Scripts, but now I need to get it working with Netbeans.

starball
  • 20,030
  • 7
  • 43
  • 238
wurdalack
  • 891
  • 2
  • 8
  • 6

7 Answers7

111

I got this working on Ubuntu/Netbeans by:

  • copying the xdebug config lines from the /etc/php5/apache2/php.ini file into /etc/php5/cli/php.ini
  • setting an environment variable with the name of the debug session (you can get this from the query string in the url of the page netbeans launches when you start debugging) so the command is: export XDEBUG_CONFIG="idekey=netbeans-xdebug"

Then it's simply a case of starting debugging in netbeans and doing php myscript.php at the command line.

Note: If you want to debug remotely using netbeans you need to use Debug File on the file that is being run from the command line, not normal Debug.

likeitlikeit
  • 5,563
  • 5
  • 42
  • 56
Andrew Hancox
  • 2,330
  • 1
  • 20
  • 28
  • 2
    +1 to this, thank you @AndrewHancox. For debugging a console apps I also found it helpful to set "Do Not Open Web Browser" in project Properties → Run Configuration → Advanced... – Adam Monsen Nov 17 '11 at 17:47
  • 14
    in windows you have to use `SET XDEBUG_CONFIG=idekey=xdebug` (notice the missing quotes!) – fishbone Mar 06 '12 at 12:05
  • not working to me, I'm using Ubuntu 12.04 LTS and Netbeans 7.3. Could me give me a clue? using gui works, but with console no. I have a conf.d with the xdebug settings on apache2 and cli folders. I have exported the variable too. – GarouDan May 15 '13 at 02:20
  • 1
    GarouDan - use "php -i | grep php.ini" at the command prompt to check you've edited the right file and "php -i | grep debug" to check you've got the debug settings in there. Failing that check the idekey hasn't changed. – Andrew Hancox May 15 '13 at 11:27
  • This answer is correct in that you have to copy the XDEBUG section from ../apache2/php.ini to ../cli/php.ini. Then pressing the debug button in NetBeans. Netbeans/PHP/XDebug setup doc = https://netbeans.org/kb/docs/php/configure-php-environment-ubuntu.html#enableXDebug. Using in Netbean 8.0 Beta. – fishjd Feb 11 '14 at 21:00
  • 1
    I tried to put `export XDEBUG_CONFIG="idekey=netbeans-xdebug"` to `/etc/php5/cli/php.ini`, but since it was weird and nothing worked, I googled for "Ubuntu [export](http://stackoverflow.com/questions/10841837/the-meaning-of-export-command-in-ubuntu)" and learned that it should be run in ternminal before running php script. For me it was not obvious. – iVenGO Sep 15 '14 at 19:32
  • +1 Thanks. You can also checkout http://xdebug.org/docs/remote for details around the environment variables – Sid Kshatriya Oct 04 '15 at 12:35
  • If you are using Powershell in Windows you have to use `$env:XDEBUG_CONFIG="idekey=netbeans-xdebug"` to set the environment variable – plocks Mar 09 '16 at 13:57
44

Add xdebug.remote_autostart=On to your php.ini file or add -dxdebug.remote_autostart=On as parameter to the PHP binary call (php -d... script.php).

See http://xdebug.org/docs/remote

johannes
  • 15,807
  • 3
  • 44
  • 57
  • 3
    I use PHPStorm on Windows for my IDE and based on johannes answer I call my php scripts from like so `code` php -dxdebug.remote_autostart=On -didekey=PHPSTORM -dremote_host=localhost -dprofiler_enable=1 MY_PHP_PAGE.php PARAM1 PARAM2 `code` – Coenie Richards Apr 24 '13 at 10:41
  • 3
    @Coenie Richards that should be: php -dxdebug.remote_autostart=On -dxdebug.idekey=PHPSTORM -dxdebug.remote_host=localhost MY_PHP_PAGE.php PARAM1 PARAM2 – Tim Jan 03 '14 at 13:23
16

I will put all together, the following is that works for me.

file:
/etc/php5/cli/php.ini

zend_extension="/usr/lib/php5/20121212/xdebug.so" -> xdebug bin path
xdebug.remote_enable=on
xdebug.remote_host=127.0.0.1
xdebug.remote_handler="dbgp"
xdebug.remote_mode="req"
xdebug.remote_port=9000 -> same port configured in netbeans debugging tab
xdebug.idekey="netbeans-xdebug" -> same ide configured in netbeans debuggin tab
xdebug.remote_autostart=1

then, without any other parameter

php script.php

Darwin
  • 171
  • 1
  • 3
2

I had the same problem, my solution was this:

  1. Environment: Netbeans 8.2 under windows (apache+php)
  2. Assuming you already have PHP and NetBeans configured to debug code using Xdebug (http://wiki.netbeans.org/HowToConfigureXDebug#Notes_on_Windows_Configuration)
  3. On netbeans create new Configuration (“Project Properties” > “Run configuration” > “New…”
  4. In the new Configuration set Do Not Open web Browser (“Advanced” > “Do Not Open web Browser”)
  5. Set active the new configuration created (drop down in tool bar)
  6. Set breakpoint for debug
  7. Open debug (CTRL+F5)
  8. Open Terminal window (“Tools” > “Open in Terminal”)
  9. Type in terminal: $ export XDEBUG_CONFIG="idekey=netbeans-xdebug" (the value "netbeans-xdebug" must coincide with “Tools” > “Options” > “Debugging” > “Session ID”)
  10. Type in terminal: $ php.exe -f "C:\Apache24\htdocs\www.SiteName\ScriptName.php" -- "Arg1=x&Arg2=y"
  11. Follow debug…
0

I had the same problem my solution was this:

In Netbeans > the project window > right click on the php project > properties > Run configuration. Create a New Configuration.

Fill the correct values:

  • Run as "script"
  • set php interpreter
  • change index file in my case it was "cron/index.php".
Julian
  • 4,396
  • 5
  • 39
  • 51
0

You can use the Dephpugger project if you dont want to configure xDebug for your IDE (i hate configurations).

https://github.com/tacnoman/dephpugger

You can run the debugger in terminal, like ipdb for Python and byebug for Ruby.

Renato Cassino
  • 792
  • 1
  • 7
  • 27
0

Make sure you also setup DBGP_IDEKEY value, because usually it is not idekey. For example on Linux:

export DBGP_IDEKEY="netbeans-xdebug" 
export XDEBUG_CONFIG="netbeans-xdebug=netbeans-xdebug"
Mike
  • 9
  • 1