Setting up PHP & MySQL on OS X 10.6 Snow Leopard

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 version of MySQL 5.0.x for OS X 10.5 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
    

    Quit Terminal to flush the history to file. Restart Terminal and remove the history file: rm .bash_history 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 install xdebug
  2. Edit /etc/php.ini and add
    zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"

    after the other extension lines.

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

It all works on this machine, anyway :)

14 Responses to “Setting up PHP & MySQL on OS X 10.6 Snow Leopard”

  1. 1 Adler Medrado

    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. 2 Artem Nezvigin

    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

  3. 3 Hvdklauw

    Sweet !

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

  4. 4 Rob...

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

  5. 5 nwfrg

    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

  6. 6 Nick Lo

    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.

  7. 7 rossmcf

    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

  8. 8 Goran Jurić

    Nice.

    Is there an easy way to install APC and Imagick (that's Imagick, not ImageMagick) extensions?

  9. 9 Rob...

    Goran,

    Have you tried pecl install apc and pecl install imagick?

    Regards,

    Rob...

  10. 10 Rob...

    rossmcf,

    Thank you. Have you commented on joind.in at http://joind.in/616?

    Rob...

  11. 11 UJ

    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.

  12. 12 Rob...

    UJ,

    No idea :) On a UK keyboard, it's "shift"+"key to the left of the Z key"

    Regards,

    Rob...

  13. 13 UJ

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

  14. 14 Andrew Havens

    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?

The views expressed in these comments are not the views of the publisher. However, we believe in the rights of others to express their legitimate views and concerns. Any legitimate complaint emailed to rob@akrabat.com will be seriously considered and the post reviewed as desirable and necessary.

Leave a Reply

Buy now!