Running Multiple PHP Versions with Homebrew in OS X

Assuming you have xcode and homebrew installed and running, follow these steps. This setup will use the system Apache web server, but not the system PHP.

Adjust your unix PATH environment variable to point to homebrew’s bin and sbin directories in ~/.profile

export PATH=/usr/local/sbin:/usr/local/bin:$PATH

Add the php tap to homebrew, so it can find the various parts.

brew update
brew upgrade
brew tap homebrew/php

Start by installing php56. I need a few extensions, included below. Note that imagick gets slightly special treatment.

brew install php56
brew install -s php56-imagick
brew install php56-intl
brew install php56-mcrypt
brew install php56-xdebug

Set a timezone in /usr/local/etc/php/5.6/php.ini:

date.timezone = America/Vancouver

Set a port in /usr/local/etc/php/5.6/php-fpm.conf:

listen = 127.0.0.1:9056

To launch php-fpm on startup

mkdir -p ~/Library/LaunchAgents
ln -s /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
brew services start homebrew/php/php56

Make apache listen on port 8056 and run all PHP scripts via php56-fpm first by adding this line to /etc/apache/httpd.conf

Listen 8056

Then configure a virtual host for the port in /etc/apache/extra/httpd-vhosts.conf

<VirtualHost localhost:8056>
   ServerName localhost
   DocumentRoot /Users/michael/Sites/
   <FilesMatch "\.php">
     SetHandler proxy:fcgi://127.0.0.1:9056
   </FilesMatch>
</VirtualHost>

Restart Apache and visit a localhost site on port 8056

sudo apachectl restart

You may need to configure the directory where the development sites are hosted. Add a directive to /etc/apache/httpd.conf or one of the configuration files in /etc/apache/extra

<Directory /Users/michael/Sites>
 Options All FollowSymLinks Multiviews
 MultiviewsMatch Any
 AllowOverride All
 Require all granted
</Directory>

Adding other versions of PHP is easy. Replace 56 with whatever version of PHP you need. Just remember to unlink your current version of PHP before installing the new one. Each PHP will need to be configured independently.

  • brew unlink php56
  • brew install php70
  • have php70-fpm listen on port 9070
  • configure php.ini with a timezone
  • make apache listen on port 8070
  • setup a virtual host listen on port 8070 and sent all PHP scripts to fpm on port 9070
  • restart apache and do some work.