Setting up PHP & MySQL on OS X 10.8 Mountain Lion

There is a Serbo-Croatian translation of this article kindly provided by Anja Skrba: Podešavanje PHP i MySQL na OS X 10.8

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…

  1. sudo pear config-set auto_discover 1
  2. sudo pear install phpunit/PHPUnit
  3. sudo pear install phpunit/phpcpd
  4. 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:

12 thoughts on “Setting up PHP & MySQL on OS X 10.8 Mountain Lion

  1. Greate article, but …

    personally we stopped using LAMP on OS X a year ago and since then we are using the Vagrant/VirtualBox/Puppet trinity which gives us:
    – the same OS and packages as the systems in production provide
    – no platform specific folded problems like case-insensitivity
    – identical, project specific, automatically provisioned development environment for all developers in our team by issuing only one command

    Regards Michael

    1. Vagrant is a really nice solution. I have found that my 2007 MacBook Pro kernel panics with VirtualBox, which hasn't endeared me to it.

      I have found that native Apache & PHP has worked extremely well for me, though.

  2. There is another alternative : php-osx.
    It's a very clean install as it configure automatically (just a bit) apache for you.
    I was using it since 2 years and I've never had no problems or headaches.

    It's deadly simple under terminal.
    This one for PHP 5.3:
    $ curl -s http://php-osx.liip.ch/install.sh | bash -s 5.3

    This one for PHP 5.4:
    $ curl -s http://php-osx.liip.ch/install.sh | bash -s 5.4

    And the unstallation is dead too.
    So check at http://php-osx.liip.ch

    Vagrant etc. is very nice but a bit overkill of resources consumption ?

  3. Since you are using brew, have you tried to "brew install mcrypt" and use that library? I do this and I can use the library for other things as well.

  4. Hi
    thks for this tuto, but i don't understand why you give the "write" right to the group Staff for the file php.ini?

    sudo chmod ug+w php.ini
    sudo chgrp staff php.ini

    it seems dangerous or not ?

    1. Hi Manu,

      As this is my laptop, it's easier if I can modify php.ini when required without worrying about sudo. It's just a convenience.

  5. this could have just been a tutorial on how to enable php.ini, it would have still rocked! Thanks.

  6. I have had the XCode forever, but I can't figure out where they stuck gcc:
    configure: error: in `/Users/regisleclerc/Downloads/pcre-8.31':
    configure: error: no acceptable C compiler found in $PATH
    True, gcc can't be found in the path, even when restarting the Terminal, and even as root.
    However, compiling a C program in XCode works perfectly… Would you have a clue?
    I'm not sure going to Mountain was too bright :-(

  7. Thanx for your article – it was very helpful!

    Just a comment. I had to change the single quote from step 7.3. to this (') to make it work.

    I was getting the following error:
    -bash: -fno-common: command not found

Comments are closed.