Ramblings on technology with a dash of social commentary
RSS icon Email icon Home icon
  • Extending Apple Apache & PHP with Homebrew

    Posted on November 18th, 2014 PHP Guru No comments

    Quick start

    I recently got a new Macbook Pro (Mavericks) and decided to use the bundled Apache 2.2.6 and PHP 5.4.30.

    All you have to do is uncomment the PHP extension:

    LoadModule php5_module libexec/apache2/libphp5.so

    in /private/etc/apache2/httpd.conf and start Apache:

    sudo apachectl start

    This had me up and running in no time, but after a few days I realized I needed some additional extensions, including MySQL, Mcrypt, Mongo and Redis.

    Brew to the rescue

    There’s no better package manager for Mac OS X than Homebrew (except possibly Macports). So after installing Homebrew:

    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

    I set off to install some additional software and PHP-native extensions:

    brew install redis
    ...
    redis-server start
    brew install php54-redis
    ==> Installing php54-redis from homebrew/homebrew-php
    ==> Downloading https://github.com/nicolasff/phpredis/archive/2.2.5.tar.gz
    ######################################################################## 100.0%
    ==> PHP_AUTOCONF="/usr/local/opt/autoconf/bin/autoconf" PHP_AUTOHEADER="/usr/local/opt/autoconf/bin/autoheader" /usr/local/Cellar/php54
    ==> ./configure --prefix=/usr/local/Cellar/php54-redis/2.2.5 --with-php-config=/usr/local/Cellar/php54/5.4.33/bin/php-config
    ==> make
    ==> Caveats
    To finish installing redis for PHP 5.4:
    * /usr/local/etc/php/5.4/conf.d/ext-redis.ini was created,
    do not forget to remove it upon extension removal.
    * Validate installation via one of the following methods:
    *
    * Using PHP from a webserver:
    * - Restart your webserver.
    * - Write a PHP page that calls "phpinfo();"
    * - Load it in a browser and look for the info on the redis module.
    * - If you see it, you have been successful!
    *
    * Using PHP from the command line:
    * - Run "php -i" (command-line "phpinfo()")
    * - Look for the info on the redis module.
    * - If you see it, you have been successful!
    ==> Summary
    (beer) /usr/local/Cellar/php54-redis/2.2.5: 3 files, 216K, built in 9 seconds

    Okay, now I should be able to use Redis commands (PHPRedis) in PHP, right?

    Wrong! Whoops:

    sudo apachectl restart

    How about now?

    Nope!

    php -i | grep redis

    Nope, phpinfo() doesn’t show it.

    php -m | grep redis

    Nada; no Redis extension. Rat’s why didn’t that work?

    Solution

    Let’s look back at the output from the brew php54-redis installer. This part in particular:

    * /usr/local/etc/php/5.4/conf.d/ext-redis.ini was created,
    

    So, what gives? That seems pretty legit. At least for an Apache & PHP config installed by Homebrew.

    Oh, right, we didn’t do that.

    Here’s the problem: That’s not where Apple’s default PHP build is configured to load extra parsed ini files from.

    That path, by default, is:

    /Library/Server/Web/Config/php/

    To make the situation even more confusing, that path doesn’t even exist! And, it doesn’t even exist in any config file! It’s actually supplied as an argument (by someone at Apple who worked on the Mac OS X Mavericks Rom supplied to the Macbook Pro factory) when building the PHP binary:

    Configure Command => ... '--with-config-file-scan-dir=/Library/Server/Web/Config/php' ...

    which means we can’t even alter it without re-compiling PHP. Not that we couldn’t, but we don’t need to. Here’s why…

    Symlinks for fun and profit

    All we need to do is get the ini files created by homebrew installers to be loaded by the built-in Apache/PHP that ships with Apple and we are good to go.

    Create this lame, un-changeable non-existent directory tree (-p is recursive; go figure):

    sudo mkdir -p /Library/Server/Web/Config/php

    Next, symlink in there the redis.ini

    sudo ln -s /usr/local/etc/php/5.4/conf.d/ext-redis.ini /Library/Server/Web/Config/php/redis.ini

    Finally, restart Apache

    sudo apachectl restart

    Did that work?

    php -m | grep redis
    redis

    Yep, I take that as a good sign.

    All future installs of PHP extensions can be sudo symlinked into this same config-file-scan-dir and we’re good to go.