With OS X 10.7, Apple continues to ship PHP 5.3 with PEAR, GD and PDO_MYSQL out of the box. This is how to set it up from a clean install of 10.7.
/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
- Download the 64bit DMG version of MySQL 5.1.x (or 5.5.x) for OS X 10.6 from mysql.com and install the pkg, the startup item and the pref pane.
- 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!)
- 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} reloadClear 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
- cd /etc/apache2
- Give write permission the config file to root: sudo chmod u+w httpd.conf
- sudo vim 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.
- Change permissions back: sudo chmod u-w httpd.conf
- Restart Apache by unticking and then ticking again the Web Sharing checkbox in System Preferences -> Sharing
- 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. (note that it will ask for your password as the Documents folder is only writable by root.
- 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.6 at the time of writing).
php.ini
- cd /etc
- sudo cp php.ini.default php.ini
- sudo chmod ug+w php.ini
- sudo chgrp admin php.ini
- vim php.ini (assuming your user is a member of the admin group) and change settings appropriately. 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 Lion ships with it.
- vim /etc/php.ini
- Find the line:
;zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
and remove the semicolon at the start - 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…)
- 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.
- 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
PHPUnit and friends
I assume that everyone needs these…
- sudo pear channel-discover pear.phpunit.de
- sudo pear channel-discover components.ez.no
- sudo pear channel-discover pear.symfony-project.com
- sudo pear install phpunit/PHPUnit
- sudo pear install phpunit/phpcpd
- sudo pear install PHP_CodeSniffer
PECL OAuth
A couple of projects I work on use the PECL OAuth component:
- Ensure you have installed Xcode from the Mac App Store
- Download the latest PCRE source code from http://sourceforge.net/projects/pcre/files/pcre/ and unzip to a folder on your desktop
- cd ~/Desktop/pcre-8.12
- ./configure
- sudo cp pcre.h /usr/include/
- Remove the pcre folder on your desktop 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="/usr/lib/php/extensions/no-debug-non-zts-20090626/oauth.so"
- Restart apache: sudo apachectl restart and check in the phpinfo that OAuth is now loaded.
mcrypt
This is useful! Follow the installation details by Michale Gracie here: http://michaelgracie.com/2011/07/21/plugging-mcrypt-into-php-on-mac-os-x-lion-10-7/)
It all works on this machine, anyway :)
Other options
If you'd rather use a packaged version, then these are two alternatives:



July 25th, 2011 at 11:41 #
Isn't it a bit too much trouble? Some good (and easy to keep up-to-date) options:
1. MacPorts - http://www.macports.org/ (haven't tried on lion but says it's supported).
2. Run your favourite Linux distro (use minimal install without gui) in VirtualBox (run in headless mode in background). Very nice for experimenting, just take snapshot and do whatever you want.
July 25th, 2011 at 13:45 #
Depends on what you consider to be trouble, I suppose. It took me, maybe 15 minutes to get it set up. I generally prefer to use what's built in if I can as then Apple keep it up to date for me.
Never been much of a fan of using VMs for development - it's that much more extra effort and on my 2007 MacBook Pro, the fans go high whenever I start using VirtualBox or VMware Fusion.
I pointed out two alternatives in the article for people who want to just install something anyway :)
July 25th, 2011 at 17:33 #
Hi Rob,
I have just finished a clean install of lion and have my dev environment set up fine, I was just following the final parts of this tutorial but I have an error with the oAuth part.
sudo pecl install pecl_oauth
throws the error
No releases available for package "pecl.php.net/pecl_oauth"
install failed
July 25th, 2011 at 18:10 #
I have managed to get oauth install using the command
sudo pecl install oauth
rather than
sudo pecl install pecl_oauth
Just in case anyone else was having the same issues.
July 25th, 2011 at 18:24 #
Sam,
That's really odd! Did you do the sudo pecl channel-update pecl.php.net bit?
Regards,
Rob...
July 25th, 2011 at 18:28 #
Hey Rob,
Yeah, I followed the tutorial from after the php.ini part as I had already done the steps before that.
I tried to do the channel update a few times just to make sure that it had stuck but it kept saying it was up to date.
Everything seems fine in phpinfo so I think that its all ok at any rate.
Thanks for the tutorial by the way. Nice to find a relevant up to date one in amoungst the hundreds of 2-8 year old guides google loves to pop up!
July 25th, 2011 at 18:31 #
Thanks Sam, I've fixed the article!
July 25th, 2011 at 19:02 #
Hi Rob,
Excellent post. That's what I've been looking since Lion is up :-)
Just one doubt: you mention that it works on a clean install. Is it suppose to work also in an upgrade from 10.6.8 as well, right?
Tx a lot.
rgs,
Eduardo
July 25th, 2011 at 19:11 #
Really nice article, worked first time for me. A few quick points:
- Point 4.3 of the "MySQL" section requires sudo, I think?
- Points 2 and 6 of the "Apache" section seem to duplicate each other? Should the second be "chmod u-w"?
- To navigate to "/Library/WebServer/Documents/" in point 8 of the "Apache" section, I think you need CMD+SHIFT+G, rather than just CMD+G
- Points 2 and 4 of the "Xdebug" section seem to duplicate each other...?
Just thought some feedback might be useful! Thanks again for a great article.
July 25th, 2011 at 19:31 #
Eduardo,
Possibly :) I have no idea. I would expect it to, or something quite similar.
Chris,
Good catches! Thank you very much.
Regards,
Rob..
July 26th, 2011 at 13:47 #
The guys on liip.ch provide a binary package for OS X 10.7 which makes it very easy to intall PHP5.3 with some useful extensions: http://php-osx.liip.ch
July 26th, 2011 at 14:37 #
Nasumi,
I know; that's why I linked to them :)
Rob...
July 26th, 2011 at 14:44 #
Oh, your right! I missed that one. #facepalm ;-)
July 28th, 2011 at 18:56 #
I got an error with zsce, lion removes /usr/lib/libltdl.3.dylib :S, but seems like reating a symlink to /usr/lib/libltdl.7.dylib works, here's the error:
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/zend/lib/php_extensions/mcrypt.so' - dlopen(/usr/local/zend/lib/php_extensions/mcrypt.so, 9): Library not loaded: /usr/lib/libltdl.3.dylib
Referenced from: /usr/local/zend/lib/php_extensions/mcrypt.so
Reason: image not found in Unknown on line 0
July 29th, 2011 at 10:34 #
Thanks for the great tutorial, Rob. Something worth pointing out with regards to php.ini: After upgrading from Snow Leopard, my existing/etc/php.ini file was moved to /etc/php.ini-5.2-previous. So rather than using the default file, as you suggested, I just restored my previous settings using the following command:
sudo cp /etc/php.ini-5.2-previous /etc/php.ini
July 31st, 2011 at 14:56 #
Hi,
I am using Zend CE under Lion and I am getting the error below
Unable to load dynamic library '/usr/local/zend/lib/php_extensions/mcrypt.so' - dlopen(/usr/local/zend/lib/php_extensions/mcrypt.so, 9): Library not loaded: /usr/lib/libltdl.3.dylib
Are there any other instructions to solve this?
August 2nd, 2011 at 19:46 #
Hello Rob,
I have tried following this tutorial and have run into an issue. It appears to happen when I try to install PHPUnit. I get all the way to the part where I install that but it fails with exceptions complaining about Archive/Tar.php
I made sure everything is up to date:
jcrawford@macpro/usr/lib/php: sudo pear channel-update pear.php.net
Updating channel "pear.php.net"
Channel "pear.php.net" is up to date
But I keep getting errors like this:
Starting to download DbUnit-1.0.2.tgz (39,231 bytes)
..........done: 39,231 bytes
Warning: require_once(Archive/Tar.php): failed to open stream: No such file or directory in PEAR/PackageFile.php on line 305
PHP Warning: require_once(Archive/Tar.php): failed to open stream: No such file or directory in /Users/jcrawford/pear/share/pear/PEAR/PackageFile.php on line 305
PHP Stack trace:
PHP 1. {main}() /Users/jcrawford/pear/share/pear/pearcmd.php:0
PHP 2. PEAR_Command_Common->run() /Users/jcrawford/pear/share/pear/pearcmd.php:305
PHP 3. PEAR_Command_Install->doInstall() /Users/jcrawford/pear/share/pear/PEAR/Command/Common.php:271
PHP 4. PEAR_Downloader->download() /Users/jcrawford/pear/share/pear/PEAR/Command/Install.php:661
PHP 5. PEAR_Downloader_Package->download() /Users/jcrawford/pear/share/pear/PEAR/Downloader.php:454
PHP 6. PEAR_Downloader_Package->_fromUrl() /Users/jcrawford/pear/share/pear/PEAR/Downloader/Package.php:263
PHP 7. PEAR_PackageFile->fromAnyFile() /Users/jcrawford/pear/share/pear/PEAR/Downloader/Package.php:1606
PHP 8. PEAR_PackageFile->fromTgzFile() /Users/jcrawford/pear/share/pear/PEAR/PackageFile.php:474
Any thoughts?
Thanks,
Joseph Crawford
August 5th, 2011 at 13:05 #
I am not sure what the issue was but I re-installed PEAR from scratch and it appeared to work this time. Thanks for the helpful article.
August 6th, 2011 at 16:32 #
Thanks for this!
The last MySQL instruction about mysql.sock isn't needed if you follow the last PHP instruction. Or vice versa I guess!
August 9th, 2011 at 15:11 #
Great set of tips! For PHPUnit, there were some prerequisites (YAML) that couldn't be installed until I added pear.symfony-project.com:
sudo pear channel-discover pear.symfony-project.com
August 11th, 2011 at 09:54 #
Hey.. Thank you very much!
Would you also explain how I can move the default dir of mysql (where my databases reside) to my other partition?
There seems to be info out there, but so far when I try it breaks.. PID not found etc... I've tried using workbench and many *nix forums out there but nothing strictly for osx and lion
Cheers
Steve
August 11th, 2011 at 18:19 #
Rob,
For someone who just need a webserver running out there, should I go over all those setup steps or only Mysql, apache and perhaps php.ini would be enough?
Thanks a lot.
August 19th, 2011 at 21:41 #
Hi Rob,
Thank you so much for the great instructions.
However I need some help. I used your instructions to install 5.5 and it worked great except for one thing, it does not support Mydac. So I have tried to remove it, and have followed the removal advice that you have for Snow Leopard.
But I cannot install 5.1 as it fails before the installation finishes. There must be something else that has to be removed for Lion. Can you please tell me what that is?
Many thanks
RCS
August 21st, 2011 at 04:57 #
Something I only noticed recently is that OS X 10.7 has a wonderfully sane way of setting the system path: add them to the /etc/paths file (in order), or create additional path files in /etc/paths.d, which are loaded in name order. At least bash and zsh shells make use of this out of the box. I wish other OSs did this - they always seem to use the "PATH=$PATH:..." anti-pattern, which is another way of saying, "we have no idea what originally set this"...
During the pear installation it's worth issuing "pear config-set php_ini /etc/php.ini" so that subsequent builds of pecl extensions can enable themselves.
I've found the installation of libs from homebrew, coupled with building extensions from a downloaded PHP source tarball works very nicely with the stock PHP build (I've used it for gmp and mcrypt). It would be nice if the apache and php config system were upgraded to be more like the excellent system that's on Debian/Ubuntu.
So far I've utterly failed to get PHPStorm to talk to xdebug though :( Worked fine on Snow Leopard.
August 24th, 2011 at 15:16 #
Thanks for the explanation. I need to install a phpunderControl but I have some problems. Do you could explain the steps to configure ? Thanks
August 31st, 2011 at 18:27 #
I have followed the instructions to configure Apache, PHP and MySQL on
a dev machine with OS X Lion, but I'm hetting
following error when I try to run my application (uses Zend Framework):
Fatal error: Cannot redeclare class Bootstrap
Incude path is well defined.
Any ideas ?
On other machines it works fine ( linux and windows ).
With other Zend Framework apps it works fine.
Thank you,
Sam
September 1st, 2011 at 16:29 #
Thanks for taking the time to post this. I already had a working MySQL/PHP installation in Snow Leopard and then upgraded to Lion. It still works fine but I need to add the ability to connect to Oracle databases with PHP as well. In the process of trying to use pecl to install the oci8 stuff I get error messages, which seem to be related to something missing in my PHP installation (which is why I came here)? Yet it works fine for PHP scripts acting on MySQL.
===========================
sudo pecl install oci8
downloading oci8-1.4.6.tgz ...
Starting to download oci8-1.4.6.tgz (167,182 bytes)
....................................done: 167,182 bytes
10 source files, building
running: phpize
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
ERROR: `phpize' failed
===========================
I'm over my head here, I have no idea what the problem could be. Do you have any ideas?
Thanks in advance!
September 3rd, 2011 at 22:17 #
I have a problem that when I remove the # from #loadmodule php5_module libexec/apache2/libphp5.so it automatically turns off web sharing and doesn't allow me to turn it back on. Has anyone had a problem similar to this?
September 11th, 2011 at 13:04 #
Great, thanks. Easy to follow, just what I was looking for. I too agree, useful to use the Apple Bundled stuff, let them keep it up to date. Now it's set up just right too. thanks.
September 18th, 2011 at 11:29 #
Newbie question here ...
I have no problem following these types of step by step instructions for the Terminal, but it's not clear to me what I should do with the #2 MySQL instructions. It seem to have some instructions implied.
Can I use BBEdit to edit the .bash_profile file ?
And if so what should the proper content be?
September 18th, 2011 at 11:39 #
Max,
Yes you can use BBEdit.
Add
To the top of the file.
September 18th, 2011 at 12:37 #
Thanks, Rob
I also have some questions for the Apache part.
#9 Create a new folder called "orig" and place all files currently in the Documents folder into it.
"place" ... do you mean "copy" or "move" the content to the orig folder?
#10 Create a new file called info.php with <?php phpinfo(); inside it.
"it" - is "it" referring to /documents or to /documents/orig?
September 18th, 2011 at 12:53 #
Max,
9: Move
10: info.php should be created in /documents
Regards,
Rob...
September 18th, 2011 at 13:20 #
Rob,
When I go to localhost/info.php I get a Not found error.
If I move the info.php file to /Users/max/Sites and load localhost/~max/info.php it works.
I can see in the info that DOCUMENT_ROOT = /Users/max/Sites
I probably inherited this from my Snow Leopard installation. Should I accept this or will it create a problem? I prefer to use my accounts sites folder if possible.
September 18th, 2011 at 13:25 #
correction...
when I move the info.php file to /Users/max/Sites it also works go to localhost/info.php (no need to add the /~max/ part)
September 25th, 2011 at 02:15 #
Just FYI for those doing a clean Lion install
sudo cp /etc/php.ini.default /etc/php.ini
September 26th, 2011 at 09:31 #
Rob,
I just got my first mac today (generally do dev on linux or windows) and I seem to be stuck on PHP. I have an issue where I can get .php files from requests but they come back as text files (presumably they are not being served up to PHP). I have uncommented the library reference in httpd.conf and copied the php.ini file but left everything as default so I would only be working with one issue at a time. Apache is working and I did reset the apache server (several times). Is it possible that there is something else in a fresh OOB build of OSX that would stop this? I'm using regular lion and not server but I would presume that is not necessary. Any Ideas would be greatly appreciated!
September 28th, 2011 at 02:24 #
All I want is a webserver on OSX Lion 10.7 with PHP and MySQL. MAMP appears not to work with Lion, it did work with Snow Leopard ...
Has anyone with a simple mind an explanation of what to do. I am a developer, but am now getting fed up of all the complications that I read on various forums.
Brian
September 28th, 2011 at 10:02 #
Hi -
I'm having all sorts of difficulties here! :)
(1) I'm fine with the download, installs and usr/local steps.
(2) I've created .bash_profile in my home dir - it didn't exist. I opened in TextEdit, and included the following:
export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:$PATH
export EDITOR=vim
It doesn't seem to be doing much for me; is this right?
(3) The password steps keep resulting in errors like this:
./mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
I presume I am supposed to have typed:
./mysqladmin -u root password mypassword and not included the {}. Is this right? Why am I having the password problems?!!
My apologies for the dumb questions.
Nick
October 4th, 2011 at 01:28 #
@Brian Holland
MAMP does work with Lion - I had a working MAMP installation on 10.6, updated to 10.7 and everything just transferred over and works.
Most do not consider me to have a simple mind and even if I did I could not explain what to do because I didn't have to do anything! 10.6 to 10.7 was rather painless - a couple days to get used to scrolling the other way (which now feels totally natural) was it .
@Rob - thanks for the write up - I think I will give it go.
October 10th, 2011 at 16:34 #
Thank you very much for a clear, no nonsense guide to getting these pieces installed.
October 13th, 2011 at 14:57 #
Great Tutorial, as always.
Cheers
October 28th, 2011 at 12:49 #
Hi, Thanks for this great tutorial.
I still have a problem with mcrypt module. I had followed all the istruction but no chanches to see mcrypt in my phpinfo.
Any suggestion?
October 31st, 2011 at 05:15 #
Excellent! I was having issues with my Snow Leopard -> Lion upgrade and just did a clean install. Thanks to this being at the top of the Google search results, I had my local database and web apps up and running faster than ever before. Many thanks!
November 23rd, 2011 at 09:54 #
Followed the instructions and all worked fine - many thanks. I did get one issue which was when I tried to run:
mysqladmin -u root password {new-password}
i got mysqladmin: command not found
but it worked by changing to
/usr/local/mysql/bin/mysqladmin -u root password {new-password}
(and likewise adding /usr/local/mysql/bin/ for the other 2 commands in that section)
November 29th, 2011 at 00:26 #
Thanks for the instructions. Really great. One other gotcha with the php.ini file is to make sure you set the "date.timezone" variable.
December 7th, 2011 at 21:19 #
Hello,
what I have done : install successfully MySQL.
I have install php from http://php-osx.liip.ch/
but I try to go on localhost/phpinfo.php it gives me an error (I put my phpinfo.php into /library/webserver/documents/)
is there something wrong ?
Thanks,
Alex