1. PHP 5.3 on Snow Leopard

    My old post on compiling PHP for Mac OS 10.5 (Leopard) continues to top my most-viewed page statistics. Sadly, that article is old and doesn't apply very well to Snow Leopard (10.6).

    I've been meaning to post instructions on how to compile PHP for Snow Leopard since last summer when I picked up the DVD, but hadn't found the time or opportunity to build PHP from a completely fresh start, until a few weeks ago.

    This time, I took notes on how to reliably compile PHP and Apache from scratch on this system.

    1. Download and install Xcode. You're on your own for the details of this one, but frankly, if you can't figure it out, you'll find the next steps too difficult. Think of it as a prerequisite.

    2. Create a working directory. I use ~/src, but you can use whatever you like.

      
      $ mkdir ~/src
      $ cd ~/src
        

    3. Install Homebrew. Homebrew is a truly great software packager for OS X. Think Macports, but not as ugly; Fink, but not as broken (and not as binary). Designed for Mac. It's Ruby, but we don't have to hold that against them. (-:

      
      $ curl http://gist.github.com/raw/323731/572b315c4f7ee78244de70e7ad703c8ae324da7a/install_homebrew.rb > install_homebrew.rb
      $ ruby install_homebrew.rb
         

    4. Install your own iconv. I don't know what Apple did to theirs, but it's a huge headache. You're best installing your own, in my experience.

      
      $ curl http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz | tar -zx -
      $ cd libiconv-1.13.1
      $ ./configure --prefix=/opt && make && make install
      $ cd ..
        

    5. Install Apache-HTTPD from source. This isn't absolutely necessary, but Apple seems to have done some weird stuff with their Apache, and in my experience, it's best to build your own. If you skip over this step, you'll need to change the apxs in the PHP configure command, below.

      First, find your closest mirror.

      
      $ curl http://apache.mirror.iweb.ca/httpd/httpd-2.2.15.tar.bz2 | tar -jxf -
      $ cd httpd-2.2.15/
      $ ./configure --enable-rewrite --enable-ssl && make && make install
      $ cd ..
         

    6. Install PHP dependencies using Homebrew. Easy, huh?

      
      $ echo "gd jpeg libpng libxml2 libzzip mcrypt mysql" | xargs brew install
      $ echo "libpng libxml2 readline" | xargs brew link
        

    7. Install PHP from source by first selecting a mirror.

      Note: you will need to use a really nasty patch to get this to build properly. See the note on iconv above. Even Apple's own iconv patch for PHP doesn't work (at least not for me).

      
      $ curl -L http://ca2.php.net/get/php-5.3.2.tar.bz2/from/this/mirror | tar -jxf -
      $ cd php-5.3.2
      $ curl http://www.php.net/~scoates/patches/php-5.3.1-Makefile.global-iconv.patch | patch -p0
      $ ./configure --prefix=/usr/local --with-xsl --with-gd --with-zlib-dir \
        --enable-sockets --enable-exif --with-mcrypt --enable-soap \
        --enable-embedded-mysqli --with-mysql --with-pdo-mysql --with-curl \
        --with-libedit --with-apxs2=/usr/local/apache2/bin/apxs --enable-mbstring \
        --with-openssl --with-iconv=/opt && make && make install
      $ cd ..
        

    8. Configure Apache. If you've done this on other platforms, this step should look familiar.

      1. In /usr/local/apache2/conf/httpd.conf, in the <IfModule mime_module> block, add the following:
        
        AddType application/x-httpd-php .php
        AddType application/x-httpd-php-source .phps
            
      2. Optionally, add PHP to DirectoryIndex by changing
        
        DirectoryIndex index.html
            
        to
        
        DirectoryIndex index.php index.html
            

      You can now test Apache + PHP by creating a phpinfo() page, and restarting Apache:

      
      $ echo "<?php phpinfo(); ?>" > /usr/local/apache/htdocs/info.php
      $ ln -s /usr/local/apache2/bin/apachectl /usr/local/bin/apachectl
      $ sudo /usr/local/bin/apachectl restart
        

      Now, visit localhost/info.php, and you should have an independent, custom-compiled Apache-PHP stack.

    I hope this has been helpful. If I've given bad instructions, or if something doesn't work for some reason, please let me know in the comments.

    Belorussian translation provided by Patricia.

    13 Responses

    Feed for this Entry
    • I find that crossing your fingers helps, especially after typing 'make'. It's at least helped my build success rate on Mac by 20%. Seems like it at least.

      Also, this is fixed in SVN (i.e., 5.3.3+) but seemingly isn't perfect yet? I don't know anymore, but it works for me (usually). Ugh! Also for fun, try compiling in the intl extension with --enable-int :)

      And lastly, the aforementioned patch simply moves $(MH_BUNDLE_FLAGS) to the end before -o within the $(CC) line in Makefile, so users should feel free to do that by hand if needed. I probably did that 1,000 times over the past year or two and even have a script that does it somewhere (it also adds -lresolv to EXTRA_LIBS but that task is not needed anymore at least).

    • Stas Malyshev

      2010 May 24 15:57

      Worked for me pretty good with 5.3, thanks (I used macPorts instead of homebrew, works too). For intl (and other C++ exts I guess) easy solution seems to be to add -lstdc++ to EXTRA_LIBS - at least that did the trick for me.

    • all worked sweet thanks heaps,

      is there an easy way to install apache modules now? homebrew perhaps?

    • You might want to suggest using the new MySQL Native Driver in the ./configure such as :
      --with-mysql=mysqlnd \
      --with-mysqli=mysqlnd \
      --with-pdo-mysql=mysqlnd \

      You could also point out that people can modify that ./configure to enable any other extensions they need. For example, I need the Command Line Interface and Process Control and many others, so, I've added --enable-pcntl, --enable-cli, etc.

      Otherwise, thanks for the great writeup.

    • I'm on 10.5, and I'm being lazy and installing along with the system apache.

      First run through:

      ./configure --with-openssl --with-apxs2 --prefix=/usr/local/php532

      Works great, but two gotchas:

      - apxs will clobber the system libphp5.so on install, regardless of your --prefix selection; you may want to back yours up beforehand (whoops!)
      - that builds a 32-bit php, but my 10.5 system wants a 64-bit binary.

      To get a 64-bit build to work, I ran into problems with iconv. Resolution there is to remove the #define iconv libiconv line as in the apple patch, and then:

      CFLAGS="-arch x86_64" ./configure --with-openssl --with-apxs2 --prefix=/usr/local/php532

    • works great, thanks for sharing!

    • Thanks for the great writeup.

      I had some iconv problems too. I managed to get PHP to build by first adding

      CFLAGS="-arch x86_64"

      ...before the configure step of libiconv, then using Wez Furlong's tweak to the PHP source and adding the CFLAGS in the configure step of PHP.

    • just a note that I was logged in as an AD user, that was also an admin, and I got errors on installing Homebrew.  I had to use a local admin account.

      thanks for the article!

    • Couldn't get homebrew to install libpng (it claimed os x shipped with it), after linking to the os x version in X11, i discovered that os x's libpng is out of date.

      In the end, manually installed libpng from the brew formula: https://github.com/mxcl/homebrew/blob/master/Library/Formula/libpng.rb into the /usr/local prefix and everything went smoothly.

    • Scott Benton

      2012 Jan 03 23:23

      Just wanted to say thanks for all of your hard work on this blog. It's helped me out a few times over the years -- and I just now realized you blog about beer as well, so I have more reason to come back. I'm getting ready to crack open some Anchor Christmas Ale here while installing a new system and LAMP. Thanks again!

    • Anna Filina

      2012 Jul 13 10:47

      It's worth mentioning that the step 6 will fail because Homebrew removed "libpng" support. libpng now comes standard with OSX.