Netbeans, XDebug and Vagrant

Getting Xdebug to work with an Apache server running in a Vagrant box takes a bit more effort than with a local server.

I am assuming Xdebug is already installed on your server.

To debug code executed in your browser you need the following:

  • In Netbeans open you project properties by right licking on your project and selecting “Properties” from the context menu.
  • Go to “Run Configuration” and click the “Advanced…” button.
    If you are going to run your script from the browser set the Default: [url] option on top. You are only going to need to map your paths between the Vagrant box and your PC system. Enter the path to your project in the “Server Path” section and pick a folder matching this location in the “Project path”.
  • This should be enough, you can simply choose the browser to use by Netbeans and hit the debug button to get everything running.

Getting Xdebug to run for code triggered from command line via SSH requires some additional steps.

  • You need the same path mapping as above, but unlike code run in the browser, you want the “Do Not Open Web Browser” option checked.
  • SSH into your box and run a netstat command. You are going to get some output, including something like the following:
    Proto Recv-Q Send-Q Local Address           Foreign Address         State      
    tcp        0      0           ESTABLISHED

    This tells you the foreign address of the box is – this is the ip through which a connection with your PC is established.

  • Knowing the gateway of your box you can setup correct variables for XDebug by the following command (replace the remote_host or idekey by other values if needed):
    export XDEBUG_CONFIG="idekey=netbeans-xdebug remote_host= profiler_enable=1"
  • You can check the documantaion for running Xdebug remotely here. After you have the correct values assigned to XDEBUG_CONFIG you need to hit the debug button on netbeans. Once pressed it will show “Waiting for Connection” in the status bar. You can run you php script from with SSH now, the “Waiting for Connection” message should disappear and Netbeans should be able to halt your script on breakpoints.