I struggled immensely one evening to find a tutorial out there that worked for me, on how to debugPHP applications running over MAMP, using the PHP Storm IDE. After absorbing bits and pieces from otherguides, what really made it click for me, was reading the officialxdebug documentation,where they provide a diagram of how the debugging components communicate, and what some of the xdebug settings mean.
Happy debugging! Docker on Mac OS. At the time of writing Docker on MacOS uses Hyperkit as a small VM to run the docker engine on. Due to this extra layer, setting up Xdebug on Docker for Mac OS requires a small workaround: There is a hostname available in each of your containers named the docker.for.mac.localhost. This hostname will resolve to. Free version (2.15) is available on this site. It works with PHP Edit, PHP Coder and some other IDEs. Free dbg front-end (dbg-cli) for Linux/Unix can be used to debug scripts either directly or under DDD 3.3.9 (GNU debugger). Also see RedHat 7.3, YellowDog and Mandrake distributions at RPM Find site. See screenshots here. PHP Tools on Mac and Linux: PHP Tools on Mac and Linux. NuSphere earned world's recognition as a provider of PHP Tools not just on Windows but on all major platforms. NuSphere's PHP Debugger is available on Linux, Windows, Mac OS and Unix making debugging of php applications easy with PhpED - NuSphere's PHP IDE. PHP scripts encoded with Nu-Coder - PHP Encoder can be deployed on any. In dwarf3 the debugger encodes the string as 'array 0.x of char'. But internally it currently uses different dwarf tokens to encode that type, that it does when encoding an actual array. So if the debugger detects fpc as compiler, it looks for this internal difference. (If fpc changes that in future, then the debugger will be clueless (again)).
Php Debugger For Mac Os
To point out - it doesn't really matter that the PHP server in this scenario is MAMP, this configuration will work forany 'remote' PHP server. I put remote within inverted commas, because the server is actually running on my localmachine, but is considered remote for the implementation.
What I had going on as my dev environment (but don't let the exact details put you off, as the flow should be the same):
- MAMP 3.5, free version, running PHP 5.6.10 (did someone say PHP 7 was out?)
- Mac 10.10.5 (OS X Yosemite)
- PHPStorm (tested on 7.1.3 and 8.0.3)
Without further ado, let's begin this winning guide, which by the way, was surprisingly straight forward to get working.
Setting MAMP up with xdebug
Xdebug is the life and soul of this debugging party - it's a Zend Engine PHP extension that enables the debugging ofPHP, as well as PHP profiling and other useful things.
Luckily for us, MAMP already comes with the xdebug extension out of the box, so we don't need to bother installing itourselves, we just need to make sure it's enabled. We also need to ensure some xdebug settings are configured correctly.Let's do both of these tasks at the same time!
Find out what version of PHP MAMP is running (Preferences > PHP):
As mentioned before, that's version 5.6.10 for me. It's important that we know what version is running, because we needto edit the active
php.ini
config file. The PHP config file I'll be editing is at/Applications/MAMP/bin/php/php5.6.10/conf/php.ini
.Open up
php.ini
for editing, and search for xdebug
. There should be a setting that includes the xdebug ZendExtension, but it may be commented out. Ensure it's uncommented, and also add some xdebug settings, giving yousomething that looks like this:Breakdown of PHP config settings:
zend_extension
- make sure this value is the file path that points to the relevant xdebug module running foryour PHP version in MAMPxdebug.remote_enable=1
- we are going to be setting up a remote debugging session, so make sure it's enabledxdebug.remote_host=127.0.0.1
- Xdebug acts as the client, and theIDE (PHPStorm) is going to act as the server. This means that the remote host of the server is where theIDE is running, which will (probably) be localhost for you, i.e. the same machinexdebug.remote_port=9000
- PHPStorm, i.e. the server, listens for xdebug debugging on port 9000 as default,so unless you've got a good reason to change it, keep it as 9000xdebug.idekey=PHPSTORM
- This sets our debugging session key toPHPSTORM
. In theory, it can be named anything.Xdebug remote debugging uses session cookies to identify that a debugging session is in play. I'll discuss this abit more later on
Some of these above settings may already be the default for MAMP. So you might not need to explicitly set them all.
Restart MAMP servers, so that the
php.ini
changes are applied, and visit the php info page(http://127.0.0.1:8888/MAMP/) to check your new configuration is live. Here were some screenshotsof my applied settings:You can also check if the Zend Engine xdebug extension is installed via the command line:
If you're satisfied that the MAMP configuration is correctly setup, move onto the next stage - setting up PHPStorm.
Setting up PHPStorm
With MAMP out of the way, it's now time to configure some options in PHPStorm.
Step 1
Remember that PHPStorm is acting as the server for xdebug, and we configured xdebug (the client) to knock on the doorof port 9000. Let's first of all make sure that PHPStorm is setup to listen for incoming connections on port 9000.
- File > Default Settings
- Use the search field to type in debug
- Focus in on the menu PHP > Debug
- Ensure that the Xdebug section is listening on port 9000
Here's a screenshot of my settings:
Step 2
Prepare PHPStorm, by telling it to begin listening for PHP debug connections. This is simply (but vitally) achieved bypressing a button in the IDE. Make sure you've got View > Toolbar showing, and look for the button that resembles a(proper old school) phone.
Here are some screenshots to guide you:
Thanks to someJetBrains documentationfor that one.
Step 3
Lastly, PHPStorm needs to be aware of our remote server (MAMP) that's actually interpreting the PHP scripts.We're going to point PHPStorm in the right direction.
Here's a list of instructions, but see the screenshots below for further clarity.
Php Debug Mac
- Edit a debug configuration, Run > Edit Configurations
- Press the plus icon
- From the list, choose the option PHP Remote Debug
- Optionally give it a name
- Within the Configuration section, next to the Servers input, click on the ellipsis (…) to add a new server
- When the new window pops up, click the plus icon to add a new server (see below screenshots for details)
- Give it a name, such as MAMP. Set the host to 127.0.0.1. Set the Port to
8888(or whatever your MAMP server is configured to listen to). Make sure Xdebug is selected from the menu. Click OK - Now the server window has closed, you're back to the PHP Remote Debug window.
- Enter your IDE key, matching the same one as set inside of the
php.ini
config. That's PHPSTORM for us - Click OK. You're done!
Thanks goes to moreJetBrains documentationon this.
Debugging PHP in PHPStorm
Finally, we're getting to the good stuff. Get your debug configuration running, by either making sure it's selectedin the drop menu next to the bug icon, and clicking on the bug icon to activate. Or from the menu -Run > Run, then choose the debug configuration you created earlier.Here's a screen grab of the former method:
Upon activation, a debug bar should appear at the bottom of PHPStorm, that says it's waiting for connections(Waiting for incoming connection with ide key ‘PHPSTORM'):
To test our example, we've got an
index.php
file sitting in the htdocs
of our MAMP server,with the following content:Stick a break point on the executing line, like so:
Open your browser, and navigate to http://127.0.0.1:8888/?XDEBUG_SESSION_START=PHPSTORM
Your breakpoint should get hit, automatically switching windows to PHPStorm. You'll see something like the following:
You can see that I'm inspecting the
$_GET
variable, where I can see the XDEBUG_SESSION_START
key.This will also exist in $_COOKIE
.What's XDEBUG_SESSION_START
about?
Php Debugger For Mac Installer
Xdebug sets a cookie in the browser, which links up with the DBGp protocol, and the rest is beyond my understandingin all honesty. But it essentially enables the debugging session to ride through the various components to providethe debugging experience. Go knock yourself out with thedocsif you fancy it.
The cookie expires after an hour. And if you take a peek at the request headers, say through the browser dev tools,you'll see a cookie being set:
This means that for the next hour, you don't need to pass the query
XDEBUG_SESSION_START=PHPSTORM
through to theHTTP request if you don't want to, and the debugging will still be active. Yay for cookies! Meaning that you can hithttp://127.0.0.1:8888/ instead of http://127.0.0.1:8888/?XDEBUG_SESSION_START=PHPSTORM forsubsequent requests.'But query strings aren't cool!'
Well, there are some bookmarklets that you can add to the browser which eliminates even the need for manuallyentering the query string, and you can generate them here:
Farewell
I hope you've now got to grips on how to debug PHP using MAMP, via xdebug and PHPStorm.