While Apple’s included PHP configuration included with Apache is getting better and better, it is still far from complete.

First thing that it’s missing is support for GD library, which, if you are looking to manipulate images via PHP, is simply essential.

While there are a bunch of other ways to run PHP (XAMPP, MAMP and others), I like using the Apache and PHP that come with Leo and build on top of that (like a true geek).

Here’s how to compile GD for your PHP…

Requirements

  • You must have Apple’s Dev Tools installed first (meaning X-Code). This is available on your OSX Install DVD or from Apple’s Web Site.
  • You also need X11 SDK installed.  The best way to do it: install X11 client from OSX DVD and then install the Dev Tools - this way SDK will be installed for you.
  • Backup your system… We don’t want you screwing things up and having no safe point to return to, right?
  • I assume you have not manually updated anything related to GD, libpng, libjpeg and freetype so far (if you have, make sure you adapt these instructions to the changes you made).
  • I also assume that you have PHP5 ENABLED on your Leopard Machine. It’s quite easy to do and Tyler can help a bunch on this. In short: move the /etc/php.ini.default to /etc/php.ini, and edit /etc/apache2/httpd.conf and remove # in front of LoadModule php5_module.

Installation process

  • Open a terminal window and create a new folder for storing your source files and building future apps. While most people use /SourceCache, I like using a folder called “.sources” in my Home directory. So, let’s first create it and then go into it by typing the following in terminal:cd ~
    mkdir .sources
    cd .sources
  • Next step is to download libjpeg (the only one missing) so GD can work properly, type:curl -O http://www.ijg.org/files/jpegsrc.v6b.tar.gzNote: that’s an O as in “oh”, not a 0 as in “zero”
  • We also need the PHP source so we can compile the GD from there. 10.5 ships with PHP version 5.2.4 originally, but if you run 10.5.5 like I do, chances are you have 5.2.6! Make sure you know which version you have first - so you can download the correct PHP source. Do so by typing:php -vNote: the top line should tell you something like:
    - PHP 5.2.6 (cli) (built: Jul 15 2008 23:16:51)
  • Now that you know the version you got, get the correct file below:For 5.2.4 type:
    curl -O http://www.opensource.apple.com/darwinsource/10.5/apache_mod_php-43/php-5.2.4.tar.bz2

    And if you got 5.2.6:
    curl -O http://www.opensource.apple.com/darwinsource/10.5.5/apache_mod_php-44.1/php-5.2.6.tar.bz2

  • Now it’s fun time, let’s take care of libjpeg first:Unpack it by typing:
    tar -xzvf jpegsrc.v6b.tar.gz

    Enter the folder and copy the config files to it:
    cd jpeg-6b
    cp /usr/share/libtool/config.sub .
    cp /usr/share/libtool/config.guess .

    Now we need to configure it, make it and install it, so copy and paste the following command into the terminal (this will make the 64 bit version - which is what works for default Apache installation):

    MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os  -pipe -no-cpp-precomp” CCFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os  -pipe” CXXFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os  -pipe” LDFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load” ./configure –enable-shared

    That’s all one line, when complete, type:
    make

    Now, before installation we have to make sure couple of crucial directories exist, so type this:
    sudo mkdir -p /usr/local/include
    sudo mkdir -p /usr/local/bin
    sudo mkdir -p /usr/local/lib
    sudo mkdir -p /usr/local/man/man1

    And it’s time to install libjpeg by typing:
    sudo make install

    Note: you will have to enter the password when calling commands with sudo since installation has to proceed as root.

  • Now that we got that out of the way, we need to get back into the .sources folder, unpack the PHP source, get into the GD folder, and do some fun stuff:cd ..
    tar -xvf php-5.2.6.tar.bz2

    cd php-5.2.6/ext/gd

    Note: above I assume you got 5.2.6, if you downloaded the 5.2.4, kindly replace 6 with of 4 in the above commands .

    Now run:
    phpize

    And then this one line command:

    MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os  -pipe -no-cpp-precomp” CCFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os  -pipe” CXXFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os  -pipe” LDFLAGS=”-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load” ./configure –with-zlib-dir=/usr –with-jpeg-dir=/usr/local/lib –with-png-dir=/usr/X11R6 –with-freetype-dir=/usr/X11R6 –with-xpm-dir=/usr/X11R6 –with-gd

    When it all compiles, type:
    make
    sudo make install

  • Now the fun part is over. I will assume that you already have PHP5 enabled on your MacOS as mentioned under requirements, so:Edit the php.ini file
    sudo pico /etc/php.ini

    Add the following after all “extension=blahBlah” directives:
    extension=gd.so

    Then find:
    extension_dir = “./”

    And put a semicolon in front to disable it, so it now looks like:
    ;extension_dir = “./”

    Save your php.ini file (Press CTRL + O and then enter).

  • That’s it. Restart your Apache Server (System Preferences -> Sharing - and uncheck and check back the Web Sharing item).
  • Test that GD is installed by typing this in console:
    php -r ‘phpinfo();’ | grep GD

    If GD is there, you should see this output:
    GD Support => enabled
    GD Version => bundled (2.0.34 compatible)

    If it isn’t - make sure you followed all the instructions correctly: Especialy the part of editing php.ini file and restarting your server. You should also check the Apache Log since it may help telling you if the compiled GD object is corrupt. Do so by typing:
    tail /var/log/apache2/error_log

Hope this helps you out My next tutorial, MySQL and mcrypt, so stay tuned .

Much thanks to: Kenior and TopicDesk.