2013-11-04

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 two alternatives:

PHP 5.3/5.4/5.5 for OS X 10.6/10.7/10.8/10.9 as binary package by Liip

Zend Server (Free Edition)

Homebrew has PHP.

Let's go!

/usr/local

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

MySQL

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.

Open the pref pane and start the MySQL Server.

Update the path by editing ~/.bash_profile and add:

at top of file.

Set up MySQL root password:

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

Now ensure that the mysql.sock file can be found by PHP:

Ensure that MySQL is running

sudo mkdir /var/mysql

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.

cd /etc/apache2

Give write permission the config file to root: sudo chmod u+w httpd.conf

Edit /etc/apache2/httpd.conf

Find #LoadModule php5_module libexec/apache2/libphp5.so and remove the leading #

Find AllowOverride None within the <Directory "/Library/WebServer/Documents">section and change toAllowOverride All so that .htaccess files will work.

Restart Apache: sudo apachectl restart

Give yourself permissions to the /Library/WebServer/Documents/ folder using the terminal commands:

sudo chgrp staff /Library/WebServer/Documents

sudo chmod g+rws /Library/WebServer/Documents

sudo chmod g+rw /Library/WebServer/Documents/*

Open Finder and navigate to /Library/WebServer/Documents/ using shift+cmd+g

Create a new folder called "orig" and place all files currently in the Documents folder into it.

Create a new file called info.php with <?php phpinfo(); inside it.

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

Ensure that Apache will start after a reboot:
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

php.ini

cd /etc

sudo cp php.ini.default php.ini

sudo chmod ug+w php.ini

sudo chgrp staff php.ini

Edit php.ini and change settings appropriately.
At a minimum, you should change:

Xdebug

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

Edit /etc/php.ini and add this line to the end:

If you want to configure your xdebug settings, then add an [xdebug] section. I like these settings (use with caution…):

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

Composer

Install Composer into /usr/local/bin:

cd /usr/local/bin

curl -sS https://getcomposer.org/installer | php

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.

cd /usr/lib/php

sudo php install-pear-nozlib.phar

Edit/etc/php.ini and find the line: ;include_path = ".:/php/includes" and change it to:
include_path = ".:/usr/lib/php/pear"

sudo pear channel-update pear.php.net

sudo pecl channel-update pecl.php.net

sudo pear upgrade-all

sudo pear config-set auto_discover 1

PHPUnit and friends

I assume that everyone needs these...

Edit ~/.bashp_profile and add

and now restart Terminal

composer global require 'phpunit/phpunit=3.7.*'

composer global require 'phpmd/phpmd=1.*'

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.

Install the command line tools: xcode-select --install and press the Install button.

Compile and install autoconf:

Install Homebrew:

brew install autoconf

or, manually compile and install autoconf from source.

Intl extension

If you need Locale:

brew install icu4c

sudo pecl install intl
The path to the ICU libraries and headers is: /usr/local/opt/icu4c/

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:

Download libmcrypt-2.5.8 from sourceforge

Download the correct PHP source code from here. At the time of writing you need version 5.4.17.

Extract the libmcrypt and PHP source code into ~/Desktop/src.

Compile and install libmcrypt. Type these lines into the Terminal:

cd ~/Desktop/src/libmcrypt-2.5.8

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

make -j6

sudo make install

Compile and install the mcrypt PHP extension. Type these lines into the Terminal :

cd ~/Desktop/src/php-5.4.17/ext/mcrypt

phpize

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

make -j6

sudo make install

Add extension=mcrypt.so to the end of /etc/php.ini using your favourite text editor

Restart Apache using sudo apachectl restart

Open up the info.php file and check that crypt is installed

Delete the src folder from the Desktop

PECL OAuth

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

Download the latest PCRE source code from http://sourceforge.net/projects/pcre/files/pcre/ to your Downloads folder and uncompress it

cd ~/Downloads/pcre-*

./configure

sudo cp pcre.h /usr/include/

Remove the pcre folder and tgz file on your Downloads folder as you don't need it any more

sudo pecl install oauth

Edit/etc/php.ini add these lines to the end of the file:

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

It all works on this machine, anyway :)

Show more