subscribe: Posts | Comments

Step debugging in PHP with MAMP, Xdebug and MacGDBp

Table of Content

  1. Install and enable debugging from Xdebug (MAMP)
  2. Remote debugging
  3. Using remote paths
  4. Perpetually Disconnected Status or Socket Errors

Install and enable debugging from Xdebug (MAMP)

The most difficult part of setting up MacGDBp is setting up the Xdebug extension. You cannot use the built-in web server because that version of PHP will not properly execute Xdebug. Instead, the easiest method is to download MAMP. After installing Mamp, you have to install Xdebug.

  1. Since MAMP v2.0+ should shipped with xdebug pre-installed, we should not need to manually compile PHP and xdebug in our case.
  2. Open up /Applications/MAMP/conf/php5.xxx/php.ini for editing in a text editor. In my case, I am using MAMP Pro 2.2 with multiple version PHP, so my screen may looks a bit different to MAMP standard version.
  3. Remove the heading comment marker at your xdebug extension setting line, for example, here is my (MAMP Pro 2.2) PHP 5.4.19 template setting:
    zend_extension=”/Applications/MAMP/bin/php/php5.4.19/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so”
  4. Continue your xdebug zend_extension settings with the following configuration to your xdebug extension:
    xdebug.remote_enable=1
    xdebug.remote_host=localhost
    xdebug.remote_port=9000
    xdebug.remote_autostart=1
  5. For a list of more settings and deeper explanations, see this website

MAMP Pro xdebug settings

Remote debugging

There are many PHP debugger software from the net, here I choose MacGDBp (Mac or Windows).

MacGDBp will merely listen for any connection coming into your computer on port 9000. The Xdebug extension works by connecting to the specified host and port in php.ini; if MacGDBp is running on the specified host, then the two will connect. In order for this to work, particularly if your server is not on your local network, you will need to have your home router forward all data coming in on port 9000 to your computer. Make sure that your Mac’s firewall also allows this. See the above configuration section for more information.

For most of the case, you may want to disable the xdebug autostart feature (xdebug.remote_autostart=0), and on a page that you need debugging, you can just add a query string “XDEBUG_SESSION_START=ANY_NAME” to manually start the debugger.

xdebug_session_start_macgdbp

To make your life easier, you may want to use xdebug helper for your Chrome browser, this is a tiny plugin to add xdebug variable along with your page request.

xdebug helper 1

Using remote paths

If you do not debug PHP scripts locally, but on a remote server, the file paths of your scripts will likely be different. This means that setting breakpoints will not work as expeted; MacGDBp will tell Xdebug (running on the remote server) to set a breakpoint for a file that’s on your local machine. The only time this happens is when breakpoints are set; all other operations that involve source code are done via Xdebug (including viewing source). To use breakpoints with a remote instance, you will need to keep two copies of the source code: one on the remote server and one locally (or you can mount the remote server). Then open MacGDBp –> Preferences –> Paths. The path replacement system works by transforming a path on your local computer into the one on the remote server. When you add a breakpoint path, it will replace all the “local paths” from the replacements pane to create the remote path of the source code.

For example, if I were debugging a remote website where the local source was on my computer, this is how I’d set it up:

Local Path (OS X): /Users/robert/htdocs/mywebsite
Remote Path (Linux web host): /home/hosting_company/public_html

Perpetually disconnected status or socket errors

The “could not bind to socket” error means that MacGDBp cannot bind to port 9000, which means something else is already using that port. If you got this upon the first launch of MacGDBp, check if another application is using this port. Also, check your firewall. If you had opened MacGDBp and then this error happened, try closing and reopening the application, and restart if all else fails.

 

I also found a video from Vimeo about this topic:

Leave a Reply