Install PHP5, lighttpd and Imagick on Mac OS X Leopard

It’s a little tricky getting a nice clean install of PHP5 for OS X 10.5. The packages that I always used to depend on over at Entropy don’t seem to work anymore. At least not for Leopard. A great shame.

I’m now switching to lighttpd for my development server, so have come up with the following recipe to install PHP5 and the great Imagick extension for image manipulation.

Follow these instructions at your own risk! They worked fine on my setup but I can’t guarantee they will for you.

  1. Install Xcode.
  2. Install MacPorts.
  3. Now update MacPorts:

    sudo port selfupdate
    
  4. Install lighttpd:

    sudo port install lighttpd
    

    Install the startup item and copy the default configuration file:

    sudo launchctl load -w /Library/LaunchDaemons/org.macports.lighttpd.plist
    sudo cp /opt/local/etc/lighttpd/lighttpd.conf.default /opt/local/etc/lighttpd/lighttpd.conf
    
  5. Now we install PHP with the FastCGI, MySQL and Pear variants.

    sudo port install php5 +fastcgi +mysql5 +pear
    

    In order for this to work, I had to disable the port perl5.8 by using the following command:

    sudo port deactivate perl5.8
    sudo port activate perl5
    

    Then run the command again and it should succeed:

    sudo port install php5 +fastcgi +mysql5 +pear
    
  6. Copy over the default PHP configuration:

    sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini
    
  7. Now open up the PHP configuration file:

    sudo mate /opt/local/etc/php.ini
    

    Add the following line to the file:

    cgi.fix_pathinfo=1
    

    Find extension_dir = "./" and replace it with:

    extension_dir = "/opt/local/lib/php/extensions"
    extension=imagick.so
    
  8. Edit the lighttpd configuration file:

    sudo mate /opt/local/etc/lighttpd/lighttpd.conf
    

    Ensure that the following is uncommented:

    fastcgi.server = ( ".php" =>
        ( "localhost" =>
            (
                "socket" => "/var/run/lighttpd/php-fastcgi.socket",
                "bin-path" => "/opt/local/bin/php-cgi"
            )
        )
    )
    

    And also make lighttpd run as the user www:

    server.username = "www"
    server.groupname = "www"
    

    Add the following if you want to be able to use your Sites directory in your home directory. I also changed the default document root to the OS X default:

    server.modules += ("mod_userdir")
    userdir.path = "Sites"
    userdir.basepath = "/Users/"
    server.document-root = "/Library/WebServer/Documents"
    

    Ensure that "mod_fastcgi", is uncommented.

  9. Now we need to setup a few directories, and make the user www the owner for them:

    sudo mkdir /var/run/lighttpd
    sudo mkdir /var/log/lighttpd
    sudo chown www:www /var/run/lighttpd
    sudo chown www:www /var/log/lighttpd
    
  10. Now install ImageMagick:

    sudo port install imagemagick
    
  11. Now we need to download the Imagick PHP module. I was hoping this would be as easy as sudo pecl install imagick, however I got an error for that, and so a manual build and install of the module is necessary:

    curl -O http://pecl.php.net/get/imagick-2.2.1.tgz
    tar xvfz imagick-2.2.1.tgz
    cd imagick-2.2.1/
    phpize
    ./configure --with-imagick=/opt/local
    make
    sudo make install
    sudo cp /opt/local/lib/php/extensions/no-debug-non-zts-20060613/imagick.so /opt/local/lib/php/extensions
    

And finally, you can then run LightTPD with the following. The -D flag tells lighttpd not to run in the background–you don’t need to use it:

lighttpd -f /opt/local/etc/lighttpd/lighttpd.conf -D

You should then be able to visit a PHP file in your browser to test that it works. A simple test file would be:

<?php
$img = new Imagick('pic.jpg');
$img->rotateImage('#fff', 45);
header('Content-type: image/jpeg');
echo $img;
?>

This is just a simple Imagick rotate on an image.

2008-11-22 [, , , , ]

Comments

  1. [...] Install PHP5, lighttpd and Imagick on Mac OSX Leopard (talks about using MacPorts to install these things) [...]

    Guozheng’s Programming Notebook :: imagick for XAMPP on Mac Leopard :: December :: 2008 (2008-12-27 @ 08:20)
  2. Mac OS X Leopard 10.5.6, Mac port 1.7.0, Perl 5.8.8, PHP5.2.8, ImageMagick 6.4.8, gwak 3.1.6, re2c 0.13.5

    1. install php5 from Macport with variants

      $ sudo port -v install php5 +fastcgi +mysql5 + pear +readline

    2. install ImageMagick from Macport with variants

      $ sudo port -v install ImageMagick +no_x11 +perl +q32 +hdri +jpeg

    3. install gawk from Macport

      $ sudo port -v install gawk

    4. install re2c from Macport

      $ sudo port -v install 0.13.5

    5. Everything is ok , now can build library

      $ sudo pecl install imagick

      The config script will ask for location of ImageMagick, I answer: [/opt/local]

    The following statement appears after process complete.

    Build process completed successfully Installing ‘/opt/local/lib/php/extensions/no-debug-non-zts-20060613/imagick.so’ install ok: channel://pecl.php.net/imagick-2.2.1 configuration option “php_ini” is not set to php.ini location

    You should add “extension=imagick.so” to php.ini

    1. Edit /opt/local/etc/php.ini for ImageMagick extension
    Hon-Yu Lawrence Cheung (2008-12-28 @ 18:59)
  3. Hello,

    I have followed your steps, but i have this problem :

    “opening pid-file failed: /opt/local/var/run/lighttpd.pid Permission denied”

    Someone known which permission i must modified ?

    Thanks.

    Laurent (2009-01-15 @ 20:28)
  4. When I come to run lighttpd I get this error with the config file.

    Duplicate config variable in conditional 0 global: server.document-root 2009-02-03 14:14:18: (configfile.c.855) source: /opt/local/etc/lighttpd/lighttpd.conf line: 218 pos: 15 parser failed somehow near here: (EOL)

    Do you have any idea what is wrong? I can email you the file if you would like to see. I’m keen to get this running as it is for my dissertation.

    Daniel Lee (2009-02-03 @ 15:18)
  5. Ok I fixed that issue. Now I don’t know why it isn’t binding any clues why? can’t bind to port: 80 Permission denied

    Daniel Lee (2009-02-03 @ 17:28)
  6. Are you starting lighttpd using sudo?

    David (2009-02-08 @ 18:19)
  7. I’ve been struggling with this for a few days … I found your instructions and hoped my problems were over. But, using the current version of php5 and ImageMagick on MacPorts and the imagick php5 plugin, I get the following error in the server log when I try to start up:

    dyld: NSLinkModule() error dyld: Symbol not found: zendce_iterator Referenced from: /opt/local/lib/php/extensions/imagick.so Expected in: flat namespace

    I’m thinking that someone got rid of zendceiterator in the latest php5, or I might have some binding error, or maybe my ImageMagick compiled against the wrong headers (I also have php4 installed?) Any ideas?

    Mitsu Hadeishi (2009-02-26 @ 18:33)
  8. sorry it’s italicizing some of the symbols, above. The symbol not found is underscore zend underscore ce underscore iterator.

    Mitsu Hadeishi (2009-02-26 @ 18:34)
  9. I figured it out. The problem was I am also loading the php4 module and it is using the same php.ini as php5, so when it attempts to load the imagick.so compiled against php5 it barfs. The solution to running both side by side will have to be separating the php.ini files somehow.

    Mitsu Hadeishi (2009-02-26 @ 19:54)
  10. After step 6. I attempt sudo mate /opt/local/etc/php.ini but get sudo: mate: command not found I’m a bit of a newb…

    Geoff Allen (2009-02-27 @ 03:12)
  11. After: lighttpd -f /opt/local/etc/lighttpd/lighttpd.conf -D I get… 2009-02-27 21:58:41: (log.c.97) server started 2009-02-27 21:58:41: (modfastcgi.c.924) bind failed for: unix:/var/run/lighttpd/php-fastcgi.socket-0 Permission denied 2009-02-27 21:58:41: (modfastcgi.c.1365) [ERROR]: spawning fcgi failed. 2009-02-27 21:58:41: (server.c.897) Configuration of plugins failed. Going down.

    I have no idea how to fix this, hope you can help, thanks.

    Geoff Allen (2009-02-27 @ 23:02)

Powered by WP Hashcash