Debug using Localhost
PHPStorm allows you to dynamically debug your code using breakpoints, stepping, and many other features. The following steps document how I set up debugging on my MacBook Pro using Lion OSX version 10.7.3 and PHPStorm 3.0.
I am running localhost as part of the operating system instead of using MAMP but the overall strategy remain the same.
I hope these notes are helpful in getting your debugging sessions set up as quickly as possible on your system.
Make sure php.ini is loaded
You can verify that your php.ini file is being read by creating a simple PHP file and save it in the root of your localhost web folder. You may want to name it test.php:
<html> <head> <title>Testing PHP</title> </head> <body> <h1>Testing PHP</h1> <h2>Demo Code</h2> <?PHP echo "This is a test"; phpinfo( ); ?> </body> </html>
The key to this page is the phpinfo( ). This is an invaluable function to help you verify that PHP and all its support programs are up and running.
When you run this page using localhost you should get an information page that looks something like this:
Make sure there is a path in the Loaded Configuration File reference. This tells you that PHP
is using your php.ini file.
If not, check the settings in your Apache .conf file. (Mine was located in
private/etc/apache2/httpd.conf. It includes this line of code:
Make certain to reset your server if you change either the .conf or the php.ini files. Apple
logo/System Preferences/Sharing/ Uncheck “Web Sharing” and then recheck it to turn the server
off and back on, rereading both of these initialization files.
You can get a compiled version of xDebug from the Komodo website. Select the “PHP Remote Debugging Client” for Mac (Universal).
Download and unzip this file and save the xdebug.so file in /usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so. The long folder name designates the version of xdebug which became important later on when I was installing the Google Chrome extension.
Add the following lines to the php.ini file. Make certain you are using the php.ini file that your PHP program is using. (Refer back to the path found when you ran test.php in the first step.)
;PKJ http://files.zend.com/help/Zend-Server/zend_debugger_-_configuration_directives.htm ;PKJ http://drupal.org/node/157609 ;PKJ http://stackoverflow.com/questions/1936792/where-can-i-download-xdebug-for-php-and-mac-os-x [xdebug] zend_extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so xdebug.remote_enable = 1 xdebug.remote_host = localhost xdebug.remote_port= 9000 xdebug.remote_log = /users/yourUserNameGoesHere/Desktop/xDebug.log
Notice I listed several web pages you can use as a reference. You can also comment out the xdebug.remote_log if you don’t need that. It should be a path pointing to the spot where you want to have the log created. I just put it on my desktop for quick debugging.
(Don’t forget to run it from localhost.) and you should see two references to xDebug.
Once under PHP Modules and again under Zend Modules. Use CTRL F in your browser to locate these references quickly on the phpInfo web page.
Set up PHPStorm
A nice, fast shortcut to access the Preferences Window in PHPStorm is CMD , (comma).
Inside the PHPStorm Preferences select PHP/Debug.
— Set the port to 9000 (matching what you have in the php.ini)
— Check the box “Can accept external connections”
— The next three break options can be left check or unchecked depending on you own preferences.
Also go into PHP/Debug/XDebugProxy and set the following:
— IDE Key: PHPSTORM (This is used to connect the Google Chrome Extension with PHPStorm
— Host: localhost
— Port: 9000
Add Extension to Google Chrome
Using Google Chrome Extensions do a search for Xdebug Helper and install it.
The option settings must match what you have in the PHPStorm Preference settings that you made in the previous step.
Use the Remote Debugger
To use the remote debugger, open up the test.php in a browser (using localhost) as well as in PHPStorm. A small grey bug icon will display in the right end of the URL.
After you refresh the actual web page (CTRL R) you can control the flow of the program and view the variables by switching to PHPStorm and using the controls in the Debug Window.
NOTE: You have to run the debugging session from the browser by refreshing the page. That will trigger off the breakpoint set in the actual code.
Toggling back and forth between the browser and PHPStorm…
- Step Over (F8) allows you to skip through a function.
- Step Into (F7) allows you to step into a function.
- Use the Red X (CMD F4) to stop a debugging session. (At first you might forget this and have several debug sessions all
piled on top of each other.)