21
0
0

Setting up PHP & MySQL on OS X Mavericks

Rob...viaRob Allen's DevNotes
November 04, 2013

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:

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 ~/.bashp_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/mxcl/homebrew/go)"
    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 :)

Discussion

-