Flexible PHP-Development-Setup on MacOS X with Homebrew

Many (all?) php developers face the challenge that they have to test their work using different PHP versions. For MacOSX 10.10 Yosemite I found a  real simple solution that is setup in within about hour (excluding compile time ;-) )

Thanks to Tobias Liebig (@etobi auf Twitter) I came across the very good and well thought blog post of Alan Ivey about his php development setup on MacOSX. Until a couple of days ago I had a rather similar setup except the following features:

  • homebrew based apache server
  • auto virtual host feature for apache
  • php installation using fpm

The really cool thing of this setup is that you can create / use virtual hosts and switch php versions without touching the apache and / or php configuration! I can only recommend this setup.

While installing I came across two small glitches:

  • If you are switching from a mod_php- and homebrew-based installation, you must uninstall and remove every prior php installation and install all php versions from scratch. Otherwise the usage with fpm will not work. I recommend to compile and install all available php versions, as it is quite simple to switch between them at any time later.
  • The “mysql”-database in version 5.6 together with MacOS X 10.10 (Yosemite) may cause some headaches, resulting in the error: “Lost connection to MySQL server at ‘sending authentication information’, system error: 32”.  The reason seem to be the “Open File Limits” in MacOSX Yosemite. The solution provided on “docs.basho.com” worked for me. I had to create the two mentioned files and to restart my computer.

As already mentioned, one of the key features of this howto is to switch php versions without touching any configuration, neither Apache config nor php config or the need to switch to the root user. But you still have to remember all the cli commands changing the fpm pool. Another detail, that the post does not touch, that you might end up with different php version on the command line and the fpm pool, which can have unwanted side effects. If you want to use the same php version, you will have to issue some more commands in the terminal.

But as you know,  programmers are lazy ;-) So I made a small script that helps you and me to switch the php versions in a way that you can easy remember. :-) Installing and using it is quite simple:

  • Create a location that you can remember, f.e.
mkdir ~/Devel/Scripts
  • Create an empty text file there
  • Copy the following content to this file

#!/bin/sh

echo "\nUnload all possible php fpm pools"

if launchctl list | grep php53  >/dev/null
        then echo "==> Shutdown php53" && launchctl unload -Fw ~/Library/LaunchAgents/homebrew.mxcl.php53.plist >/dev/null;
fi

if launchctl list | grep php54  >/dev/null
        then echo "==> Shutdown php54" && launchctl unload -Fw ~/Library/LaunchAgents/homebrew.mxcl.php54.plist >/dev/null;
fi

if launchctl list | grep php55  >/dev/null
        then echo "==> Shutdown php55" && launchctl unload -Fw ~/Library/LaunchAgents/homebrew.mxcl.php55.plist >/dev/null;
fi

if launchctl list | grep php56  >/dev/null
        then echo "==> Shutdown php56" && launchctl unload -Fw ~/Library/LaunchAgents/homebrew.mxcl.php56.plist >/dev/null;
fi

echo "\nUnlink all possible php cli versions"
brew unlink php53 >/dev/null
brew unlink php54 >/dev/null
brew unlink php55 >/dev/null
brew unlink php56 >/dev/null

echo "\nLink and start php$1"
brew link php$1 >/dev/null
launchctl load -Fw ~/Library/LaunchAgents/homebrew.mxcl.php$1.plist                                                                 

  • save the file as “usephp”
  • exit your editor
  • open the terminal
  • make the file executable with the following command
chmod 750 ~/Devel/Scripts/usephp

Now you can switch at any time between the php versions, just using the command

~/Devel/Scripts/usephp 55

where “55” refers to the wanted php version. If you replace it, with “56”, php 5.6 will be loaded and linked. Same is valid for the version 5.4 and 5.3.

Probably this script can be nicer and a little bit more sophisticated. But I hope it helps you developing and testing your php applications If you have any suggestions, please leave them in the comments.

 

This Post Has 2 Comments

  1. Peter

    Thx for the hint. Unfortunately Apache Installation fails on OS X 10.11 El Capitan with this error message:

    ~  brew install -v homebrew/apache/httpd22 --with-brewed-openssl --with-mpm-event
    ==> Installing httpd22 from homebrew/homebrew-apache
    Error: The following formula:
      httpd22
    cannot be installed as a a binary package and must be built from source.
    To continue, you must install Xcode from the App Store,
    or the CLT by running:
      xcode-select --install

    Any idea how to fix this?

    1. Marcus Schwemer

      Hi Peter,

      I think this is related to MacOS Update. As stated you must install the new XCode and then run “xcode-select –install” in the terminal. The last command installs the commandline tools of XCode.

      Marcus

Leave a Reply