Setup php, xdebug and PhpStorm on Mac OS 10.7 + Xcode 4.3.2
I've been trying out JetBrains PhpStorm to see how well I'm able to use it for JavaScript and PHP Development work (PhpStorm is a Super set of WebStorm, their JavaScript IDE). The JavaScript pieces work flawlessly out of the box- I'm able to debug in Firefox or Chrome on either my Mac or Windows Development box.
It took a bit more effort to get PHP Debugging setup on my Mac dev box and I want to call out the step-by-step process I went through to it working. Most of the effort involved setting things up on the Mac OS X side of things. Once the tools were in place PhpStorm integrated nicely.
Edit: See Article 2 for information on how to debug PHP Web Applications
Notes:
- JetBrains PhpStorm [jetbrains.com]
- PEAR / PECL Install instructions [php.net]
- xdebug Install Instructions [xdebug.org]
- GNU autoconf [gnu.org]
- Link to autoconf-latest.gz [gnu.org]
- Help getting PHP working on Mac OS X Lion 10.7.3 [stackoverflow.com]
- XCode Changes / issues:
- Does not install CLI Tools by default in 4.3 [stackoverflow.com]
- Thread about how XCode 4.3 is missing autoconf [osdir.com]
Setup PHP
- Validate that php is installed by running which php at the command prompt
- Edit the /etc/apache22/httpd.conf file and uncomment the libphp line
- sudo nano /etc/apache2/httpd.conf
- Press Ctrl-w and search for libphp
- Uncomment the line and save the file using Ctrl-x
- Next we need to enable the php.ini file. By default Apple ships a /private/etc/php.ini.default file which can be renamed to php.ini using this command:
- cp /private/etc/php.ini.default /private/etc/php.ini
- Restart the apache service for the change to take effect:
- sudo apachectl restart
Note: You may need to enable Web Sharing in System Preferences if you want to host php files from your local mac
PHP Should now be available on your Mac
Connect PhpStorm to the PHP Interpreter
- Start PhpStorm and open PhpStorm -> Preferences -> PHP
- Click the '...' button next to Interpreter
- Click the '+' button and select /usr/bin/php (default for Mac OS X 10.7 Lion)
- Give the PHP Interpreter a Name and select a Debugger
PhpStorm should now be successfully connected to the PHP Interpreter. Next we'll get some pre-requisite tools that'll allow us to install the debugger
Install CLI Build tools via XCode 4.3.2
- Start XCode
- Open XCode Preferences and select the Downloads tab-group
- Click the Install button next to Command Line Tools and be prepared to wait for a bit
Once the CLI Tools have been installed you can proceed to installing PECL/PEAR in preparation to install xdebug
Install PECL / PEAR
- The Unix/Linux/BSD Install Instructions are pretty straightforward. I only had to make one change to take into account the fact that Mac Lion doesn't ship with wget:
- Download the phar file:
curl -O http://pear.php.net/go-pear.phar - Start the installer:
sudo php -d detect_unicode=0 go-pear.phar
Note: If you DO NOT want a 'System-wide' install, don't use sudo - Change the install to a place that works for you. This can be done by changing the Installation base ($prefix) setting in the installer's configuration screen. I changed mine from /Users/username/pear to /usr so I didn't have to muck with the $PATH variable
- You will be asked if you would like to alter /private/etc/php.ini. I said Yes [Y] during my install
- You'll get a final install screen message:
php.ini include_path updated.
Current include path : .:
Configured Directory : /usr/share/pear
Currently used php.ini (guess) : /private/etc/php.ini
At this point the PEAR Package manager should be installed. I don't know where PECL comes into play- it looks to be the package installer tool.
Install xdebug via PECL / PEAR
- sudo pecl install xdebug
(sudo may not be necessary, but I went for it based on the /usr setting I used above)
- You will probably see a couple of errors appear at this point (unless you already have autoconf installed):
riondevmac:temp rion$ sudo pecl install xdebug
downloading xdebug-2.2.0.tgz ...
Starting to download xdebug-2.2.0.tgz (247,670 bytes)
....................................................done: 247,670 bytes
66 source files, building
running: phpize
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.ERROR: `phpize' failed
Note: From XCode 4.3 the autoconf tool has been removed from the XCode CLI Tools. The lack of autoconf is causing the above error message. If you have upgraded from a previous version of XCode, you may still have autoconf available
- Install autoconf if you get the above error:
- Download the latest autoconf from ftp.gnu.org
- In a Terminal / command prompt, navigate to the autoconf-x.yz folder (autoconf-2.69 at the time of writing)
- Run these commands to install autoconf:
- ./configure
- make
- sudo make install
- Validate your install by running which autoconf (should return location)
- Re-run the xdebug install now that autoconf is available: sudo pecl install xdebug
- There is an Installation Instructions screen which passes by quickly during the install. Here is what it says:
+----------------------------------------------------------------------+ | | | INSTALLATION INSTRUCTIONS | | ========================= | | | | See http://xdebug.org/install.php#configure-php for instructions | | on how to enable Xdebug for PHP. | | | | Documentation is available online as well: | | - A list of all settings: http://xdebug.org/docs-settings.php | | - A list of all functions: http://xdebug.org/docs-functions.php | | - Profiling instructions: http://xdebug.org/docs-profiling2.php | | - Remote debugging: http://xdebug.org/docs-debugger.php | | | | | | NOTE: Please disregard the message | | You should add "extension=xdebug.so" to php.ini | | that is emitted by the PECL installer. This does not work for | | Xdebug. | | | +----------------------------------------------------------------------+
- If the install went well you'll see these messages at the end of the console log:
Build process completed successfully
Installing '/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so'
install ok: channel://pecl.php.net/xdebug-2.2.0
configuration option "php_ini" is not set to php.ini location
You should add "extension=xdebug.so" to php.ini - To wrap up the xdebug install we need to edit the /private/etc/php.ini file:
- Open the php.ini file and search for xdebug
- Uncomment the line that looks like this:
;zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so" - Here's an image of what my php.ini file looked like afterwards:
Note: There is a slight difference between the line that is in the php.ini file (zend_extension) and what the xdebug installation instructions mention (extension). In my testing I have found the default line to be sufficient, so I didn't alter it
Validate PhpStorm can use xdebug
At this point PhpStorm should be able to use the php debugger. You can validate this by:
- Opening up a PHP Project
- Setting a PHP Breakpoint
- Debug the project form within PhpStorm
You should see see Variables, Watches and other things in the 'debug' window: