Using Xdebug in MAMP

My current web development environment basically consists of a Macbook Pro running Mountain Lion, PHPStorm 5.0 and Virtual Box running one ore more virtual Ubuntu Servers with everything needed. Correctly setting up a linux guest OS in Virtual Box with shared folders shouldn’t be but is sometimes a bit of a headache. (Even though it does have many advantages.) Others choose not to use virtual servers and instead go for  MAMP when using OS X.

MAMP is a full local server environment including Apache, PHP and MySQL in one package. Anyone feeling at home in *nix systems should feel at home using both OS X and MAMP. It is easy to install and use even if you don’t care much for the terminal or configuring Apache, PHP or MySQL. As I have helped a few collegues setup Xdebug in MAMP I have discovered that a lot of developers don’t do this as they don’t know how simple it is. Even seasoned PHP developers use the old echo/die debugging techniques.

So I thought I’d write a small guide on how to setup debugging using xdebug in MAMP from PHPStorm. The process is similar if you use Zend Studio or another IDE of your choice. Only the IDE configuration should be different. If you already have MAMP installed just skip directly to the PHPStorm part.

Debugging with PHPStorm/xdebug using MAMP

MAMP

Installing MAMP is as easy as downloading it from http://www.mamp.info/. Double click to unpack the zip file. Then double click the the pkg file and follow the instructions. MAMP is installed in /Applications/MAMP and Xdebug is already included, though not enabled. By default MAMP (in version 2.1.1) use PHP 5.4.4 and has Apache listening on port 8888.

To enable Xdebug in MAMP locate and open the file /Applications/MAMP/bin/php/php5.4.4/conf/php.ini using a text editor of your choice. Locate the [xdebug] section, right now in the bottom.

[xdebug]
;zend_extension="/Applications/MAMP/bin/php/php5.4.4/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so"

The semicolon means the line is commented out. Remove the semicolon to make PHP load the xdebug extension. You will also need to add a few configuration directives to setup xdebug locally and to trigger automatically. The complete xdebug configuration should look like below.

[xdebug]
zend_extension="/Applications/MAMP/bin/php/php5.4.4/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so"
xdebug.remote_host=127.0.0.1
xdebug.remote_enable = 1

That’s it. Next time you start (or restart) MAMP, xdebug is enabled and ready for your next debugging adventure.

Create something to debug

I assume you already have PHPStorm installed or know how to install software on OS X. In PHPStorm, open the directory /Applications/MAMP/htdocs. This is the default web root in MAMP. Create an index.php with the following content.

<?
  function testFunc($number) {
    echo 'Hello nr ' . $number . PHP_EOL;
  }
  $n = 0;
  while($n++ < 4) {
    testFunc($n);
  }

Setup debugging in PHPStorm

Select the menu Run > Edit Configurations… to open the Run/Debug Configurations Dialog.
Click on + and choose PHP Web Application.
Give it a name like “My first debug”.
Add a server. Click the button “…” where you specify server. The Servers dialog opens.

Click on + and fill in the values below.
Name: “MAMP Server”
Host: 127.0.0.1
Port: 8888
Debugger: Xdebug
(In this setup you do not need path mappings. You would need path mappings if doing this with virtual servers and shared directories though.)
Click OK to save and return to the Run/Debug Configurations dialog.

Choose your browser or use default.
It should now look like the screenshot below. Click OK to save and return to PHPStorm. You are now ready to start debugging.

Run/Debug configurations in PHPStorm

Start debugging

If you don’t already have the file open you created earlier. Open it and click in the margin to add a breakpoint. For example, add a breakpoint to line 3. It should now look like below.

Adding a breakpoint in PHPStorm

To start debugging, press the rightmost “green arrow with a bug” in the button bar. PHPStorm will now either open your browser or create a new tab in your browser pointing to an address very similar to http://127.0.0.1:8888/?XDEBUG_SESSION_START=18458. The get parameter XDEBUG_SESSION_START tells xdebug in PHP to start a new debugging session and connect to PHPStorm to let you debug. Immediately after the browser opens the page PHPStorm should come into the foreground and be paused on line three where the breakpoint is.

Debug view in PHPStorm

As you can see above you can now investigate variables while stepping forward one line at a time. Just remember to stop your debugging session by clicking the Stop button (red squary) when you’re finished debugging. Otherwise you will experience weird behaviour you might not understand immediately. Familiarize yourself with all the ways you can step through your code and life will get a lot simpler.

Tagged with: , , , ,
Posted in PHP
12 comments on “Using Xdebug in MAMP
  1. Matt Richardson says:

    Thank you very much. Very helpful!

  2. doublejosh says:

    BEWARE, don’t waste a bunch of time edit this file…
    /Applications/MAMP/conf/php5.4.4/php.ini

  3. Danny says:

    Don’t edit /Applications/MAMP/bin/php/php5.4.4/conf/php.ini – it’s the wrong php.ini file. You can find the php.ini file under “Loaded Configuration File” when checking phpinfo(). In this case that’d be /Applications/MAMP/bin/php/php5.4.4/conf/php.ini

  4. Really don’t head out reside right up until you have put to use on the tryout consideration. James documented him self as well as display screen for just about any months time frame since they analyzed a areas and became a member of investments promptly, providing your complete commentary of the merchandise he has been pondering in addition to the reason they demanded your deals the guy do. There is usually practically nothing cherry picked. James Edward is usually a veteran broker which designed ocean last year as he began operating some dwell investor training lessons.

  5. Jairo Santos says:

    Danny’s comment saved me a lot of headaches. The conf directory inside the MAMP root folder just holds some template configuration files to use as backup if you mess the original ones.

  6. website says:

    Thank you a bunch for sharing this with all people you really realize what you’re speaking approximately!

    Bookmarked. Please additionally talk over with my website =).
    We could have a link exchange arrangement among us

  7. Olivia says:

    Hello There. I found your blog using msn. This is
    a really well written article. I will be sure to bookmark it
    and return to read more of your useful info. Thanks for the
    post. I’ll certainly return.

    My web site Wild Blood apk (Olivia)

  8. This ring is very popular among women to show
    their femininity and augment facial beauty. They will let you know if it is advisable based on your health.
    In contrast, women over 40 with teenage daughters were
    the main contributor to the Yes vote (85% of those who voted
    yes has recently or is currently raising a teen daughter).

  9. And where average yearly precipitation is low not only is it possible but
    it is very important to do so. Herbs that bolt easily in response
    to heat, such as cilantro, should be placed on the east-facing slope so that
    they are protected from the afternoon sun. I am working on the
    closed cycle gardening and will plant our vegetable garden in the area that has been
    alfalfa and mulch with the alfalfa but since we have animals
    I will run it though them first and use it as manure.

  10. airsoft guns says:

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several
    emails with the same comment. Is there any way you can remove people from that service?
    Thanks!

  11. I know, because I am always trolling for affiliates to join my team.
    The wheel of lifeis brilliant for anyone who wants to help themselves.

    Training as a life coach will assist you to get your own capability to assist others from guidance, rather than giving advice.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>