Debug PHP command line with Xdebug and Netbeans

While running FuelPHP tasks on the command line I get some trouble with debugging.
It's simple to have Xdebug running with the server but not so obvious to get it running with the command line.
I've finally found out how !

Now, I'll show you how to setup php command line debug with Netbeans on Windows.
I'm currently using XAMPP and my webroot folder is C:/www/fuel.dev

Xdebug install check

In your command line do a "php -v" an your must see something like this :

php -v  
PHP 5.3.5 (cli) (built: Jan  6 2011 17:54:09)  
Copyright (c) 1997-2010 The PHP Group  
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies  
    with Xdebug v2.1.0rc1, Copyright (c) 2002-2010, by Derick Rethans  

If you see the last line, this indicates that Xdebug is running.

Xdebug configuration

If you're already debugging with Xdebug, your setup is nearly good.
Open your php.ini file. Xampp uses only one php.ini for cgi and cli. You can find it in yourXamppFolder/php/php.ini.

Check that your base setup for debugging is :

xdebug.remote_enable = On  
xdebug.remote_handler= dbgp  
xdebug.remote_mode   = req  
xdebug.remote_host   = 127.0.0.1  
xdebug.remote_port   = 9000  

If your setup is different than this one and your debugging is working, it's ok.
The only important point is to change the xdebug.idekey to have :

xdebug.idekey = "netbeans-xdebug"  

Where the idekey reflect the one entered in the Netbeans setup. We'll see that bellow.
Then save your php.ini and restart your server.

Netbeans setup

General setup

Go to Tools > Options > PHP > General Tab :

  • Fill the PHP 5 interpreter by browsing,
  • Optionally check "Output Window" if you want to see command line output,
  • Optionally uncheck "Web Browser" if you don't want to see command line output in a browser,
  • Check that "Debug Port" is the same as the xdebug.remote_port value,
  • Check that "Session ID" is the same as the xdebug.idekey value,
  • Click OK.

General setup for Netbeans PHP command line debugging

Project setup

Right click on your project in the Project panel and go to Properties > Run Configuration

In this panel you already have your configuration for server debuging.

  • In the configuration line : hit "New",
  • Name it PHP CLI or Toto,
  • "Run As" : Script,
  • The "PHP interpreter" can be the default one we've already setup,
  • Select the script you will run in command line (change this any time you want to run another script, because debug session is ending with the script),
  • "Arguments" and "Working Directory" are up to you depending on the command you need
  • "PHP Options" : -d xdebug.idekey=netbeans-xdebug
  • Click OK

Project setup for Netbeans PHP command line debugging

Let's debug the command line

You can now hit your Debug button and you're ready to develop new tasks for Symfony, FuelPHP or any good code you like to use.

Happy coding !

Leave a comment

Your email will never be published

Comments for this post

No comment yet.