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:
- 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:
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
- 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:
export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:$PATH
at top of file.
- 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.
- 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: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:
- Edit /etc/php.ini and add this line to the end:
zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so"
- 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
- 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 ~/.bash_profile and add
export PATH=~/.composer/vendor/bin/:$PATH
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:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
- brew install autoconf
or, manually compile and install autoconf from source.
- Install Homebrew:
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:
[oauth] extension=oauth.so
- Restart apache: sudo apachectl restart and check in the phpinfo that OAuth is now loaded.
It all works on this machine, anyway :)
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
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
Hi! Why not use a VM for proper development?
I created a GUI to make this pretty easy: http://www.puphpet.com
VMs cause the fans to spin up on on my 2007 laptop with 4GB RAM…
I always wondered why developers don't use Linux and decide to make it painful for themselves. Or why not use Vagrant anyways?
Hi,
The most simplier for me to have a clean PHP is php-osx (http://php-osx.liip.ch).
A single command line to install : curl -s http://php-osx.liip.ch/install.sh | bash -s 5.5
After that, I only have to install MySQL/Postgres/Mongo/Redis …
For Vagrant, I use https://github.com/adrienbrault/hhvm-vagrant (testing hiphop) and
Thanks for posting. I should've anticipated the update would move things around a bit. I develop locally as well. Running smoothly now. Cheers.
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
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 {} ;
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 "+"
After several days of roadblocks, I finally got a breakthrough with this guide. Thank you very much.
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.
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.*'
Thanks for the post. Worked perfectly!
hi , It is stuck in this section:
checking for C++ compiler default output file name...
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?
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!