Pragmatism in the real world

Setting up PHP & MySQL on OS X Mavericks

With OS X 10.9 Mavericks, Apple chose to ship PHP 5.4.17. This is how to set it up from a clean install of Mavericks.

Note: If you don’t want to use the built-in PHP or want to use version 5.5, then these are some alternatives:

Let’s go!

/usr/local

Ensure that the following directories exist and have the right permissions:

sudo mkdir /usr/local/include
sudo mkdir /usr/local/bin
sudo mkdir /usr/local/lib
sudo mkdir -p /usr/local/man/man1
sudo chgrp -R staff /usr/local
cd /usr/local
find . -type d -exec chmod g+rws {} \;

MySQL

  1. Download the “x86, 64bit” DMG version of MySQL 5.6.x for OS X 10.7 from mysql.com and install the pkg, the startup item and the pref pane.
  2. Open the pref pane and start the MySQL Server.
  3. Update the path by editing ~/.bash_profile and add:
    export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:$PATH

    at top of file.

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

  5. 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. Edit /etc/apache2/httpd.conf
    1. Find #LoadModule php5_module libexec/apache2/libphp5.so and remove the leading #
    2. Find AllowOverride None within the <Directory "/Library/WebServer/Documents">section and change toAllowOverride All so that .htaccess files will work.
  4. Restart Apache: sudo apachectl restart
  5. Give yourself permissions to the /Library/WebServer/Documents/ folder using the terminal commands:
    1. sudo chgrp staff /Library/WebServer/Documents
    2. sudo chmod g+rws /Library/WebServer/Documents
    3. sudo chmod g+rw /Library/WebServer/Documents/*
  6. Open Finder and navigate to /Library/WebServer/Documents/ using shift+cmd+g
  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.13 at the time of writing).
  10. 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. Edit php.ini and change settings appropriately.
    At a minimum, you should change:
    date.timezone = "Europe/London"
    error_reporting  =  E_ALL
    display_errors = On
    html_errors = On
    extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20100525"

Xdebug

Can’t have a PHP development environment without xdebug! Fortunately, Mavericks ships with it ready to enable:

  1. Edit /etc/php.ini and add this line to the end:
    zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so"
  2. If you want to configure your xdebug settings, then add an [xdebug] section. I like these settings (use with caution…):
    xdebug.var_display_max_children = 999
    xdebug.var_display_max_data = 99999
    xdebug.var_display_max_depth = 100
  3. Restart apache: sudo apachectl restart and check in the phpinfo that xdebug is now loaded.

Composer

Install Composer into /usr/local/bin:

  1. cd /usr/local/bin
  2. curl -sS https://getcomposer.org/installer | php
  3. mv composer.phar composer

PEAR

We need PEAR! For some reason, it’s not set up ready to on Mavericks, 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
  7. sudo pear config-set auto_discover 1

PHPUnit and friends

I assume that everyone needs these…

  1. Edit ~/.bash_profile and add
    export PATH=~/.composer/vendor/bin/:$PATH

    and now restart Terminal

  2. composer global require 'phpunit/phpunit=3.7.*'
  3. composer global require 'phpmd/phpmd=1.*'
  4. composer global require 'squizlabs/php_codesniffer=1.*'

You can update these tools in the future using composer global update

Compiling extensions

To compile extensions, you need the Xcode command line tools and autoconf.

  1. Install the command line tools: xcode-select --install and press the Install button.
  2. Compile and install autoconf:
    1. Install Homebrew:
      ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
    2. brew install autoconf

    or, manually compile and install autoconf from source.

Intl extension

If you need Locale:

  1. brew install icu4c
  2. sudo pecl install intl
    The path to the ICU libraries and headers is: /usr/local/opt/icu4c/
  3. Edit /etc/php.ini and add extension=intl.so to the end.

Mcrypt extension

Follow the details in Plugging mcrypt into PHP, on Mac OS X Mavericks 10.9 by Michale Gracie. 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.4.17.
  3. Extract the libmcrypt and PHP source code into ~/Desktop/src.
  4. Compile and install libmcrypt. Type these lines into the Terminal:
    1. cd ~/Desktop/src/libmcrypt-2.5.8
    2. MACOSX_DEPLOYMENT_TARGET=10.9 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
  5. Compile and install the mcrypt PHP extension. Type these lines into the Terminal :
    1. cd ~/Desktop/src/php-5.4.17/ext/mcrypt
    2. phpize
    3. MACOSX_DEPLOYMENT_TARGET=10.9 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=/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
    9. Delete the src folder from the Desktop

PECL OAuth

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

  1. Download the latest PCRE source code from http://sourceforge.net/projects/pcre/files/pcre/ to your Downloads folder and uncompress it
  2. cd ~/Downloads/pcre-*
  3. ./configure
  4. sudo cp pcre.h /usr/include/
  5. Remove the pcre folder and tgz file on your Downloads folder as you don’t need it any more
  6. sudo pecl install oauth
  7. Edit/etc/php.ini add these lines to the end of the file:
    [oauth]
    extension=oauth.so
  8. Restart apache: sudo apachectl restart and check in the phpinfo that OAuth is now loaded.

It all works on this machine, anyway :)

17 thoughts on “Setting up PHP & MySQL on OS X Mavericks

  1. Actually it's even simplier to use macports – you don't have to tweak the mac os settings and you still have the most up-to-date versions of apache and PHP and everything else

  2. Hi,

    Thanks for ur article. But like Rasta, I think it's easier to use Macports. Macports allows to install multiple versions of PHP, MySQL or PostgreSQL for example, it's easy to switch between versions and It can be very usefull.

    Fabd

  3. I always wondered why developers don't use Linux and decide to make it painful for themselves. Or why not use Vagrant anyways?

  4. Thanks for posting. I should've anticipated the update would move things around a bit. I develop locally as well. Running smoothly now. Cheers.

  5. If you're coming from a complete fresh install of OSX 10.9 you can get this error message when trying to make the folders:

    mkdir: /usr/local: No such file or directory

    So you just have to add it aswell like such:
    sudo mkdir /usr/local

    Thanks for yet another great tutorial, always so handy when setting up a new mac <3

  6. Hi Rob,

    Thanks for taking the time to write this great guide.

    Just a small note that I needed to add sudo to the beginning of the line:
    find . -type d -exec chmod g+rws {} ;

    1. I had to add a backslash to escape the semicolon at the end of this line for it to work. Without escaping the semicolon, I was getting the error
      find: -exec: no terminating ";" or "+"

  7. After several days of roadblocks, I finally got a breakthrough with this guide. Thank you very much.

  8. Thanks for this guide.
    I add to escape some characters in the command at the beginning :
    find . -type d -exec chmod g+rws \{} \;
    to make it works.

  9. Thanks for this guide, very helpful :)

    Have most working but getting

    [RuntimeException] Too many arguments.
    create-project [-s|–stability…

    after running – composer global require 'phpunit/phpunit=3.7.*'

  10. Sorry to comment on an old post, but I had this one in my notes for setting up MySQL. I am curious about the reason(s) for changing group and permissions on the /usr/local folder?

  11. Michael,

    Mainly laziness. Admin users on OS X are members of staff, so by changing the group to staff and ensuring, that all files are owned by the staff group, I don't have to type sudo so often!

Comments are closed.