Setting up PHP & MySQL on OS X 10.6 Snow Leopard
(Updated 1st May 2010)
With OS X 10.6, Apple ships PHP 5.3 with PEAR, GD and PDO_MYSQL out of the box. Also, everything is now 64bit. This means that the entire effort required to get a working PHP dev environment for my work is now much easier.
/usr/local
Ensure that the following directories exist:
sudo mkdir /usr/local/include sudo mkdir /usr/local/bin sudo mkdir /usr/local/lib sudo mkdir -p /usr/local/man/man1
MySQL
- Download the 64bit DMG version of MySQL 5.1.x (or 5.5.x) for OS X 10.6 from mysql.com and install the pkg, the startup item and the pref pane.
- Add /usr/local/mysql/bin to the path: vim ~/.bash_profile and add:
export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:$PATH export EDITOR=vim
at top of file. (Note that we set EDITOR whilst we are here so that svn is happy!)
- Set up MySQL root password:
mysqladmin -u root password {new-password} mysqladmin -u root -p{new-password} -h localhost password {new-password} mysqladmin -u root -p reload
Clear the history file by typing history -c so that {new-password} isn’t in plain text on the disk.
Apache
- cd /etc/apache2
- sudo vim httpd.conf
- Find #LoadModule php5_module libexec/apache2/libphp5.so and remove the leading #
- Find AllowOverride None within the <Directory "/Library/WebServer/Documents">section and change toAllowOverride All so that .htaccess files will work.
- Restart Apache: sudo apachectl restart
- Open Finder and navigate to /Library/WebServer/Documents/
- Create a new folder called “orig” and place all files currently in the Documents folder into it.
- Create a new file called info.php with <?php phpinfo(); inside it.
- Use Safari to navigate to http://localhost/info.php and check that the PHP version is displayed (5.3.0 at the time of writing).
php.ini
- cd /etc
- sudo cp php.ini.default php.ini
- sudo chmod ug+w php.ini
- sudo chgrp admin php.ini
- vim php.ini (assuming your user is a member of the admin group) and change settings appropriately. Change:
error_reporting = E_ALL | E_STRICT display_errors = On html_errors = On extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20090626"
(I like to see my xdebug errors in bright orange!)
Also, change all instances of /var/mysql/mysql.sock to /tmp/mysql.sock
Xdebug
Can’t have a PHP development environment without xdebug!
- sudo pecl channel-update pecl.php.net
- sudo pear channel-update pear.php.net
- sudo pecl install xdebug
- Edit /etc/php.ini and add
zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
after the other extension lines.
- Restart apache: sudo apachectl restart and check in the phpinfo that xdebug is now loaded.
PHPUnit
- sudo pear channel-update pear.php.net
- sudo pear upgrade-all
- sudo pear channel-discover pear.phpunit.de
- sudo pear install phpunit/PHPUnit
It all works on this machine, anyway :)
Other options:
Thanks for the tips.
I am newbie in Mac Platform and your post was very useful.
I just uninstalled the Zend Server to use these packages shipped in Snow Leopard.
Cheers,
If any of your readers are interested in compiling Apache/MySQL/PHP on Snow Leopard from the ground up, check this out:
http://docs.google.com/View?id=ddbc374w_393r64mhgf
Sweet !
And how do I uninstall the zend Server installation from your other blog post?
Hvdklauw,
No idea :) I did a clean install.
I would expect Zend provide instructions. Zend Server is definitely the best solution for 10.5 Leopard though.
Rob…
Thanks for the tips! I especially appreciate the easy and simple xdebug instructions
you can flush your shell history with:
history -c
also see:
http://www.macosxhints.com/article.php?story=20090831101932728
Hey Rob,
A couple of juicy additions: 64 bit MySQL preference pane:
http://www.swoon.net/site/software.html
Nice tool I picked up just the other day, WebMon:
"Configures OS X's built-in web server to support WebDAV, PHP, and SSL." and "WebMon for Snow Leopard can now set up multiple virtual domains in addition to the main domain."
http://www.cutedgesystems.com/software/WebMonSnow/
It allows custom Apache directives so is quite flexible. I also use their DNS Enabler.
Sweet Jesus, yes!
I'd been hoping for two things with Snow Leopard: 1. MySQL PDO and 2. some nice person to write an all-inclusive tutorial on setting up. Winner!
Btw, I enjoyed your talk at PHPNW09; Really natural, informed and insightful. Nice to see someone not reading from their slides, too.
Cheers,
Ross
Nice.
Is there an easy way to install APC and Imagick (that's Imagick, not ImageMagick) extensions?
Goran,
Have you tried pecl install apc and pecl install imagick?
Regards,
Rob…
rossmcf,
Thank you. Have you commented on joind.in at http://joind.in/616?
Rob…
Hi, I'm a fresh newbie on Mac… your post was very helpful.
One off-topic question: how do you easily insert the tilda "~" character on regular US keyboard (e.g. for you path environment setup)? It's driving me crazy… Thank you in advance.
UJ,
No idea :) On a UK keyboard, it's "shift"+"key to the left of the Z key"
Regards,
Rob…
Thanks Rob, it worked! I must have missed it. I was looking at the "Show keyboard viewer" at least million times…
I think there might be a few things that need cleaning up in this tutorial to make it a little easier to understand.
On step 2 it says 'Add /usr/local/mysql/bin to the path: vim ~/.bash_profile and add: ' I couldn't figure out what that was supposed to mean but I just ran 'vim ~/.bash_profile' and added those two lines to the top. There was another line to include PEAR, so I got rid of that line and added the path to the first line (export PATH).
Next, I tried to start step three, but got an error saying "unknown command mysqladmin". So I skipped ahead, quit terminal, opened terminal and ran 'rm .bash_history'. I quit terminal. Opened it again, tried 'mysqladmin -u root password {new-password in clear text}' and it worked fine. So next I try 'mysqladmin -u root -p{new-password in clear text, but I put a space after the -p} -h localhost password {new-password in clear text}' and get 'Unknown command (my password)'. So I left out the first password leaving 'mysqladmin -u root -p -h localhost password {new-password in clear text}' it prompts me for my password, then finishes without complaint.
In the Apache section, I try to go to localhost and I get a 403 forbidden and message 'You don't have permission to access / on this server.'
I don't think Apache is working right. I don't know if there's anything left over from having MAMP installed that is messing things up or not.
So that's as far as I got. Any suggestions?
Hey guys, if you ran into problems getting this error:
downloading xdebug-2.1.1.tgz …
Starting to download xdebug-2.1.1.tgz (303,198 bytes)
……………………………………………………..done: 303,198 bytes
66 source files, building
running: phpize
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
ERROR: `phpize' failed
it means you have to install your XCODE package from your original OSX CD, or you can download it, but u have to pay for it or be a developer on the mac site… cheers, mick