SQL Zone is brought to you in partnership with:

Rob Allen has been programming with PHP since 1999 and is a member of the PHP community. He is the lead author of Zend Framework in Action and is a contributor to Zend Framework, developing the Zend_Config component. Rob holds a Masters degree in Electronic Engineering from the University of Birmingham in the UK and is the Technical Director of Big Room Internet in the UK, focussing on project management and the company’s future technologies. Rob is a DZone MVB and is not an employee of DZone and has posted 31 posts at DZone. You can read more from them at their website. View Full User Profile

Setting up PHP and MySQL on OS X 10.8 Mountain Lion

08.31.2012
| 15635 views |
  • submit to reddit

With OS X 10.8, Apple continues to ship PHP 5.3 with Xdebug, PEAR, GD and PDO_MYSQL. This is how to set it up from a clean install of 10.8.

/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 "x86, 64bit" DMG version of MySQL 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{new-password} reload
    

    Clear the history file by typing history -c so that {new-password} isn't in plain text on the disk.

  4. Now ensure that the mysql.sock file can be found by PHP:
    1. Ensure that MySQL is running
    2. sudo mkdir /var/mysql
    3. sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Apache

Apple have removed the "Web Sharing" feature from System Preferences, but Apache is still installed. We just have to use the command line now to start and stop it.

  1. cd /etc/apache2
  2. Give write permission the config file to root: sudo chmod u+w httpd.conf
  3. sudo vim httpd.conf (or use BBEdit/TextWrangler to edit httpd.conf)
  4. Find #LoadModule php5_module libexec/apache2/libphp5.so and remove the leading #
  5. Find AllowOverride None within the <Directory "/Library/WebServer/Documents">section and change toAllowOverride All so that .htaccess files will work.
  6. Change permissions back: sudo chmod u-w httpd.conf
  7. Restart Apache: sudo apachectl restart
  8. Open Finder and navigate to /Library/WebServer/Documents/ using shift+cmd+g
  9. Create a new folder called "orig" and place all files currently in the Documents folder into it. (note that it will ask for your password as the Documents folder is only writable by root.
  10. Create a new file called info.php with <?php phpinfo(); inside it.
  11. Use Safari to navigate to http://localhost/info.php and check that the PHP version is displayed (5.3.13 at the time of writing).
  12. Ensure that Apache will start after a reboot:
    sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

php.ini

  1. cd /etc
  2. sudo cp php.ini.default php.ini
  3. sudo chmod ug+w php.ini
  4. sudo chgrp staff php.ini
  5. sudo vim php.ini (or edit with BBEdit/TextWrangler) and change settings appropriately.
    At a minimum, you should 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! Apple appears to agree as Mountain Lion ships with it.

  1. vim /etc/php.ini
  2. Find the line:
    ;zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
    and remove the semicolon at the start
  3. If you want to configure your xdebug settings, then scroll to the end of the file and look for the [xdebug] section. I like these settings:
    xdebug.var_display_max_children = 999
    xdebug.var_display_max_data = 99999
    xdebug.var_display_max_depth = 100
    

    (use with caution…)

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

PEAR

We need PEAR! For some reason, it's not set up ready to on Lion, but the install phar file is here, so we just need to run it.

  1. cd /usr/lib/php
  2. sudo php install-pear-nozlib.phar
  3. Edit/etc/php.ini and find the line: ;include_path = ".:/php/includes" and change it to:
    include_path = ".:/usr/lib/php/pear"
  4. sudo pear channel-update pear.php.net
  5. sudo pecl channel-update pecl.php.net
  6. sudo pear upgrade-all

PHPUnit and friends

I assume that everyone needs these...

    sudo pear config-set auto_discover 1
    sudo pear install phpunit/PHPUnit
    sudo pear install phpunit/phpcpd
    sudo pear install PHP_CodeSniffer

PECL OAuth

A couple of projects I work on use the PECL OAuth component:

  1. Ensure you have installed Xcode from the Mac App Store
  2. Download the latest PCRE source code from http://sourceforge.net/projects/pcre/files/pcre/ and unzip to a folder on your desktop
  3. cd ~/Desktop/pcre-8.12
  4. ./configure
  5. sudo cp pcre.h /usr/include/
  6. Remove the pcre folder on your desktop as you don't need it any more
  7. sudo pecl install oauth
  8. Edit/etc/php.ini add these lines to the end of the file:
    [oauth]
    extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/oauth.so"
  9. Restart apache: sudo apachectl restart and check in the phpinfo that OAuth is now loaded.

mcrypt

This is useful! Follow the details in Plugging mcrypt into PHP, on Mac OS X Lion 10.7 by Michale Gracie. Read the comments though for the 10.8 changes required.
In summary:

  1. Download libmcrypt-2.5.8 from sourceforge
  2. Download the correct PHP source code from here. At the time of writing you need version 5.3.13.
  3. Install Xcode from the Mac App Store and then install the command line tools from within Xcode
  4. Install autoconf using homebrew.
  5. Extract the libmcrypt and PHP source code into ~/Desktop/src.
  6. Compile and install libmcrypt. Type these lines into the Terminal :
    1. cd ~/Desktop/src/libmcrypt-2.5.8
    2. MACOSX_DEPLOYMENT_TARGET=10.8 CFLAGS='-O3 -fno-common -arch i386 -arch x86_64' LDFLAGS='-O3 -arch i386 -arch x86_64' CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64' ./configure --disable-dependency-tracking
    3. make -j6
    4. sudo make install
  7. Compile and install the crypt PHP extension. Type these lines into the Terminal :
    1. cd ~/Desktop/src/php-5.3.13/ext/mcrypt
    2. phpize
    3. MACOSX_DEPLOYMENT_TARGET=10.8 CFLAGS=’-O3 -fno-common -arch i386 -arch x86_64′ LDFLAGS=’-O3 -arch i386 -arch x86_64′ CXXFLAGS=’-O3 -fno-common -arch i386 -arch x86_64′ ./configure --with-php-config=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/bin/php-config
    4. make -j6
    5. sudo make install
    6. Add extension=mcrypt.so to the end of /etc/php.ini using your favourite text editor
    7. Restart Apache using sudo apachectl restart
    8. Open up the info.php file and check that crypt is installed

It all works on this machine, anyway :)

Other options

If you'd rather use a packaged version, then these are two alternatives:

Published at DZone with permission of Rob Allen, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)