Netbeans tips & tricks for running PHP and Node scripts

In this article I'd like to share some tips on creating and running various script based projects (i.e. projects in which you run something in command line). No Grunt or any other additional helper application is required. You just need Netbeans.

Also added a bonus on setting up PHP debugging (XDebug) on Windows.

Running Node.js and other scripts in Netbeans

The main trick with Netbeans is that project type "PHP" can be configured to run any command. This is true not only for Node.js but any other web technologies project.

So here is a quick step-by-step to create a Node.js project:

  1. Create "PHP Application" project (you can also use "...with Existing resources" if your are adding existing project).
  2. Finish the wizard.
  3. Open properties of your project.
  4. Choose category: Run configuration.
  5. In Run as choose Script (...).
  6. Untick Use Default PHP Interpreter.
  7. As PHP Interpreter choose node binary.
  8. Index file is the JavaScript file you want to run. Can be relative to repository root.
  9. Working Directory must be a full path. Set it to whatever you need.

Note that Netbeans allows to add more one Run configurations so you can easily switch between running various command or with different parameters.

Also note that there is a dedicated NodeJS plugin for Netbeans, but apart for adding a NodeJS icon to the project, it doesn't do anything useful... Or at least not the last time I checked (which was somewhere in January).

How to make PHP command line run faster?

If you are running PHP from command line it can be significantly slow to start. It may take more time to start PHP than actual script execution time. So how to fix this?

Using empty ini for running script

The answer is easy - use -n PHP option. This will ignore php.ini altogether and so will not load any extra modules. And in consequence PHP will run much faster. The option is universal, but is particularly useful when you are using Netbeans to build your application with PHP script.

To add this PHP option in Netbeans do the following:

  1. Open properties of your project.
  2. Choose category: Run configuration.
  3. Select Configuration you want to change.
  4. In PHP Options field add -n. The command to run your script should become something like: php -n your.script.php.

Using less modules for command line PHP

Alternative, and maybe even better solution, is to use separate ini for Apache HTTPD server and separate for PHP command line. The easiest way to do that is to copy your current php.ini to a separate folder (e.g. apache-ini) and point Apache to this ini. This is done by changing PHPIniDir directive in httpd.conf. Example: PHPIniDir "C:/programs/PHP/apache-ini/"

After that you can remove PHP extension statements from main php.ini. In particular php_curl.dll seems to load very slowly.

Note that this way PHP unit tests will also run MUCH faster and you will be able to debug your scripts and command line debugging will also be much faster.

Bonus: Debugging PHP in Netbeans

By default debugging is not possible. Thankfully installing XDebug is quite easy if you follow this steps:

  1. Get correct version of your XDebug dll. There is a helper wizard for that. Or just choose and download yourself. It depends simply on the PHP version and which compiler was used for your PHP distribution.
  2. Put the dll in extensions dir.
  3. In phph.ini add:
[XDEBUG]
zend_extension="c:/programs/PHP/ext/php_xdebug-2.1.2-5.3-vc6.dll"
xdebug.remote_enable=true
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler=dbgp

Of course the path will be different depending on the DLL you downloaded and where you put it.