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
18 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.

  12. Greetings! This is my 1st comment here so I just wanted to give a quick shout out and say I really enjoy reading through your blog posts.
    Can you suggest any other blogs/websites/forums that deal with the same topics?

    Thanks!

  13. seo says:

    Excellent post. I used to be checking continuously this weblog and I’m inspired!
    Extremely helpful information specifically the closing part :
    ) I maintain such information a lot. I used to be looking
    for this certain info for a very lengthy time.

    Thanks and good luck.

  14. Undeniably believe that that you stated.
    Your favorite reason seemked to be at the web the simplest factor to take into accout of.
    I say to you, I certainly get annoyed at the same time as
    other people consider worries that they just ddo not realize about.
    You controlled to hit the nail upon tthe top and outlied out the whole thing without having side-effects ,
    other folks cann take a signal. Will likely be ayain to get more.
    Thqnk you

    Look at my web blog – airsoft-props.com

  15. Can I simply say what a comfort to unhcover an individual who truly knows what they’re discussing
    on the web. You certainly realize howw to bring a problem to light and make
    it important. More people must read this and understand this side of
    your story. I can’t beliwve you are not more popular because you mos certainly possess the gift.

  16. If you opt to use a synthetic oil, the interval at which you need to get your oil changed jumps from every 3,000 miles to every 5,000 –
    7,500 miles. This allows your engine to run at its maximum
    capacity and help extend the life of the engine. A�Borderline Pumping Temperature-tests for the lowest
    temperature at which the oil will adequately flow through your engine;.

  17. Pretty section of content. I just stumbled upon your site and in accession capital to
    say that I get in fact enjoyed account your weblog posts.
    Any way I will be subscribing on your augment or even I achievement you access consistently fast.

1 Pings/Trackbacks for "Using Xdebug in MAMP"
  1. [...] Det första som bör göras är att installera en lämplig webbserver (t.ex Apache) och därefter själva PHP-systemet. Detta kan göras på olika sätt, via exempelvis användning av EasyPHP, LAMP (Linux Apache MySQL Php), MAMP (Mac (OS X) Apache MySQL Php), MacPorts och flera andra tillvägagångssätt (beroende på vilket operativsystem du använder dig av). Kör du Windows eller Mac (OS X) rekommenderar jag främst EasyPHP respektive MAMP – där detta är redan färdiginställt från början, eller väldigt enkelt att ställa in (se mer info för MAMP och Xdebug i ett inlägg hos dotvoid.com på denna länk). [...]

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>