Pragmatism in the real world

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

  1. 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.
  2. 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!)

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

  1. cd /etc/apache2
  2. sudo vim httpd.conf
  3. Find #LoadModule php5_module libexec/apache2/libphp5.so and remove the leading #
  4. Find AllowOverride None within the <Directory "/Library/WebServer/Documents">section and change toAllowOverride All so that .htaccess files will work.
  5. Restart Apache: sudo apachectl restart
  6. Open Finder and navigate to /Library/WebServer/Documents/
  7. Create a new folder called “orig” and place all files currently in the Documents folder into it.
  8. Create a new file called info.php with <?php phpinfo(); inside it.
  9. 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

  1. cd /etc
  2. sudo cp php.ini.default php.ini
  3. sudo chmod ug+w php.ini
  4. sudo chgrp admin php.ini
  5. 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!

  1. sudo pecl channel-update pecl.php.net
  2. sudo pear channel-update pear.php.net
  3. sudo pecl install xdebug
  4. Edit /etc/php.ini and add
    zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"

    after the other extension lines.

  5. Restart apache: sudo apachectl restart and check in the phpinfo that xdebug is now loaded.

PHPUnit

  1. sudo pear channel-update pear.php.net
  2. sudo pear upgrade-all
  3. sudo pear channel-discover pear.phpunit.de
  4. sudo pear install phpunit/PHPUnit

It all works on this machine, anyway :)

Other options:

15 thoughts on “Setting up PHP & MySQL on OS X 10.6 Snow Leopard

  1. 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,

  2. Sweet !

    And how do I uninstall the zend Server installation from your other blog post?

  3. 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…

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

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

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

  7. Thanks Rob, it worked! I must have missed it. I was looking at the "Show keyboard viewer" at least million times…

  8. 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?

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

Comments are closed.