• Home
  • Flailing Wildly
  • Résumé
  • Design
  • Code
  • Wishlist
  • About Me

Flailing Wildly
Too much straw, not enough camel.

Installing PHP 5.3 with mysqlnd on Mac OS X with MacPorts

by Ryan Parman • July 11, 2009 • Code, Software, Tutorials • 10 comments

Historically, I’ve always preferred to use Apple’s built-in Apache 2.2 and PHP 5.x that comes with Leopard. However, after trying to compile PHP 5.3 from scratch and connect it with Apache, I decided to just use the MacPorts installer instead. That did mean giving up control of a finely-tuned Apache installation, but in the end, I think I’ve ended up with a better localhost system.

Prerequisites

  • Install MacPorts

Also, make sure that your MacPorts install is completely up-to-date with:

sudo port -d selfupdate

Installation

Now, I’ve never used MacPorts to install PHP or Apache before, so I’m starting with a clean slate. If you’ve already installed PHP or Apache with MacPorts, your steps may be different. As always, your mileage may vary. For me, I develop several open-source projects, so I need things that others may not. Adjust these steps as necessary.

  1. Using “Web Sharing” in your Sharing Preferences should be turned off. Currently this points to the (old) Apple Apache installation, although we’ll change that later.
  2. From Terminal, install PHP 5.3 + Apache, and some other stuff. This will likely take quite a while. I’m installing SQLite, MySQL, and PostgreSQL because of my work on CacheCore, so you may or may not need those. mysqlnd is the new PHP Native Driver for MySQL and is supposed to be better, so we’ll use that. We also need to enable non-default settings for cURL.
    sudo port install curl +ssl+ipv6+ares+idn+gss+openldap+sftp_scp \
    php5 +apache2+fastcgi+pear

    You can see all available options by running port variants php5

  3. The new Apache configuration file is stored at /opt/local/apache2/conf/httpd.conf while the old one was at /etc/apache2/conf/httpd.conf. Take a moment to copy over any settings you’ll want to maintain into the new Apache installation.
  4. You’ll also want to include your extra settings. Toward the bottom of your httpd.conf file, add the following line:
    # All settings
    Include conf/extra/*.conf
  5. If you don’t have an SSL certificate, rename your SSL configuration:
    cd /opt/local/apache2;
    sudo mv conf/extra/httpd-ssl.conf conf/extra/httpd-ssl.conf-disabled
  6. You’ll also want to enable PHP in Apache:
    sudo mv conf/extras-conf/mod_php.conf conf/extra/mod_php.conf
  7. The new PHP configuration file is stored at /opt/local/etc/php5/php.ini while the old one was at /etc/php.ini. Take a moment to copy over any settings you’ll want to maintain into the new PHP installation.
  8. You’ll need to tell Apache to enable PHP support. Open your new httpd.conf file, find where the extensions are loaded (with LoadModule), and add this to the end of the line:
    LoadModule php5_module modules/libphp5.so
  9. Restart Apache. If you were using apachectl before, it still points to the old Apache, so we’ll want to point specifically to the new one.
    sudo /opt/local/apache2/bin/apachectl restart

At this point, PHP 5.3 with Apache 2.2 and the new mysqlnd extension are all installed.

Extra stuff

  1. I generally prefer to have lots more stuff installed locally so that I can worry more about developing and less about installing. Because of this, I also install a few other things.
    sudo port install memcached \
        php5-apc \
        php5-curl \
        php5-gd \
        php5-http \
        php5-iconv \
        php5-imagick \
        php5-mbstring \
        php5-memcache \
        php5-mysql \
        php5-openssl \
        php5-postgresql \
        php5-sockets \
        php5-sqlite \
        php5-tidy \
        php5-xdebug
  2. Restart Apache with:
    sudo /opt/local/apache2/bin/apachectl restart

Replacing older versions

Now, we want to continue using our command-line PHP scripts and the “Web Sharing” checkbox in the Sharing Preference Pane, so let’s make sure that those are all pointing to the new locations instead. We’ll be backing up and redirecting php, apachectl, and httpd.

  1. Open up your Sharing Preference Pane, and disable Web Sharing
  2. Run the following command on the Terminal:
    sudo mv /usr/bin/php /usr/bin/php.bak && sudo ln -s /opt/local/bin/php /usr/bin/php; \
    sudo mv /usr/sbin/apachectl /usr/sbin/apachectl.bak && sudo ln -s /opt/local/apache2/bin/apachectl /usr/sbin/apachectl; \
    sudo mv /usr/sbin/httpd /usr/sbin/httpd.bak && sudo ln -s /opt/local/apache2/bin/httpd /usr/sbin/httpd; \
  3. Re-enable Web Sharing in the preference pane.

Shortcuts

Lastly, I like to set up some shortcuts so that I can access all of my important localhost stuff from one place. I’ll create a new directory called www-config and then I’ll place symlinks into it for quick access to Apache and PHP configuration files.

sudo mkdir /www-config && \
cd /www-config && \
sudo ln -s /opt/local/apache2/bin/apachectl /www-config/apachectl && \
sudo ln -s /opt/local/apache2/conf/ /www-config/httpd-conf && \
sudo ln -s /opt/local/apache2/logs/ /www-config/logs && \
sudo ln -s /opt/local/var/db/php5/ /www-config/php-ini && \
sudo ln -s /opt/local/etc/php5/php.ini /www-config/php.ini
Ryan Parman

Ryan Parman is an entrepreneur, open source evangelist and passionate usability advocate currently living in Seattle. He is the founder and visionary behind SimplePie and CloudFusion, co-founder of WarpShare, member of the RSS Advisory Board, and is currently with Amazon. Ryan's aptly-named blog, Flailing Wildly, is where he writes about ideas longer than 140 characters.

« Run Internet Explorer 6 (or IE7, or IE8) images in VirtualBox on Mac OS X
WebKit 3D CSS Transforms »

Discussion

imtoddly

July 17, 2009

After trying every method I could including compiling from source, entropy.ch, this is the BEST way to get this done. I finally had to do an Archive & Install to fix my problems. Then this tutorial was quick, to the point, worked perfectly and now it even integrates with my old System Preferences! Woot! PS: MAMP is SO lame after seeing something like this in action.

Ryan Parman

July 21, 2009

Updated with some minor updates tonight to address issues with missing cURL and some debug vs. non-debug issues.

Ed Johnson

August 19, 2009

I’ve found adding xdebug functionality on OS 10.5.x using XAMPP to be extremely difficult.

Does xdebug work correctly with MacPorts?

Ryan Parman

August 19, 2009

It was a piece of cake for me. I did exactly what you see here, and I was up and running with Xdebug.

Pingback

Instalando Apache y PHP 5.3 en Snow Leopard « El blog de Martini

Ryan Parman

September 17, 2009

If you’ve upgraded to Snow Leopard, you may need to uninstall, clean, and re-install with MacPorts. See this: http://trac.macports.org/wiki/Migration

Chris Bloom

September 30, 2009

In step 2, where does it install MySQL, SQLite and Postgres? I don’t see that anywhere in the command line, and I don’t see MySQL in /opt/local/bin after going through the remaining steps.

Darice de Cuba

November 1, 2009

Did you upgrade to Snow Leopard? If so how did you solve the timezone error displayed here: http://www.macosxhints.com/article.php?story=20090831101932728?

Setting the timzone in the php.ini has no effect for me it keeps giving an error.

MacPorts looks interesting, maybe I’ll give it a try out. With Snow Leopard it’s the first time that just enabling PHP and Apache have worked well right away, except for that timezone issue and missing extension libmcrypt.

But currently my localhost is working right running sqlbuddy, PHP scripts and WordPress installs.

oula.juutilainen

February 21, 2010

By far, this is the best way to replace the original installation of PHP and Apache! Just remember to check your little ´hack´ after installing updates to your system with the Apple Software Update utility. It may remove your symlinks to Apache or PHP while updating them!

Ryan Parman

July 24, 2010

Updated to include the OpenSSL PHP extension.

Have your say

Login with your favorite account to leave a comment!   

Blog search

Archives
  • 2010 (7)
  • 2009 (7)
  • 2008 (12)
  • 2007 (8)
  • 2006 (18)
  • 2005 (57)
  • 2004 (104)
  • 2003 (103)
Categories
  • Apple (43)
  • Browsers (55)
  • Code (57)
  • Creating Websites (31)
  • Design (5)
  • Digital Media (3)
  • Family Life (10)
  • Just for Fun (25)
  • Music (5)
  • Personal (32)
  • Political (12)
  • Projects (44)
  • Software (60)
  • Syndication (28)
  • Technology (75)
  • Tutorials (5)
  • TV and Movies (16)
  • Video Games (5)
  • Website (62)
  • Work and Business (3)
  • Writing (4)
Socially-aware
  • Twitter
  • Facebook
  • Flickr
  • Last.fm
  • Glue
  • YouTube
  • Delicious
  • LinkedIn
Claim to fame
  • Amazon Web Services
  • WarpShare
  • CloudFusion
  • SimplePie
Legal mumbo-jumbo

All ideas, opinions and comments I post are my own and are in no way affiliated with anybody I work with. If you quote and/or reprint something I've written or said, please direct folks back to this site as a form of attribution. I promise I'll do the same for you. Unless otherwise noted, all content on this site is copyright © 1979–2010 Ryan Parman. Powered by Rocket Sauce.