Pragmatism in the real world

Setting up PHP & MySQL on OS X 10.7 Lion

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

  1. 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.
  2. 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!)

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

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

  1. cd /etc/apache2
  2. Give write permission the config file to root: sudo chmod u+w httpd.conf
  3. sudo vim httpd.conf
  4. Find #LoadModule php5_module libexec/apache2/libphp5.so and remove the leading #
  5. Find AllowOverride None within the <Directory "/Library/WebServer/Documents">section and change toAllowOverride All so that .htaccess files will work.
  6. Change permissions back: sudo chmod u-w httpd.conf
  7. Restart Apache by unticking and then ticking again the Web Sharing checkbox in System Preferences -> Sharing
  8. Open Finder and navigate to /Library/WebServer/Documents/ using shift+cmd+g
  9. 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.
  10. Create a new file called info.php with <?php phpinfo(); inside it.
  11. 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

  1. cd /etc
  2. sudo cp php.ini.default php.ini
  3. sudo chmod ug+w php.ini
  4. sudo chgrp admin php.ini
  5. 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.

  1. vim /etc/php.ini
  2. Find the line:
    ;zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
    and remove the semicolon at the start
  3. 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…)

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

  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

PHPUnit and friends

I assume that everyone needs these…

  1. sudo pear channel-discover pear.phpunit.de
  2. sudo pear channel-discover components.ez.no
  3. sudo pear channel-discover pear.symfony-project.com
  4. sudo pear install phpunit/PHPUnit
  5. sudo pear install phpunit/phpcpd
  6. sudo pear install PHP_CodeSniffer

PECL OAuth

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

  1. Ensure you have installed Xcode from the Mac App Store
  2. Download the latest PCRE source code from http://sourceforge.net/projects/pcre/files/pcre/ and unzip to a folder on your desktop
  3. cd ~/Desktop/pcre-8.12
  4. ./configure
  5. sudo cp pcre.h /usr/include/
  6. Remove the pcre folder on your desktop as you don’t need it any more
  7. sudo pecl install oauth
  8. 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"
  9. 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:

47 thoughts on “Setting up PHP & MySQL on OS X 10.7 Lion

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

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

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

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

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

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

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

  7. Eduardo,

    Possibly :) I have no idea. I would expect it to, or something quite similar.

    Chris,

    Good catches! Thank you very much.

    Regards,

    Rob..

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

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

  10. 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?

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

  12. 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!

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

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

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

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

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

  18. Thanks for the explanation. I need to install a phpunderControl but I have some problems. Do you could explain the steps to configure ? Thanks

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

  20. 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!

  21. 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?

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

  23. 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?

    1. Max,

      Yes you can use BBEdit.

      Add

      export PATH=~/bin:/usr/local/bin:/usr/local/mysql/bin:$PATH
      export EDITOR=vim

      To the top of the file.

  24. 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?

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

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

  27. 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!

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

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

  30. @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.

  31. Thank you very much for a clear, no nonsense guide to getting these pieces installed.

  32. 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?

  33. 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!

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

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

Comments are closed.