Multiple PHP versions on OS X

Recently I’ve switched to OSX on my MacBook Air. To keep pace I used XAMPP for first twoo week but today I’ve finally configured my environment as i want.

Requirements:

  • PHP 5.3, 5.4 and 5.5
  • Apache web server
  • Sites in ~/Sites directory
  • Working “localhost” for legacy directory-based projects
  • Different PHP versions for vhosts

Looks simple, but it took me few days to solve all small and big problems.

First what you need is homebrew. On the site you’ll find installation instructions. It’s pretty straight forward.

As an Apache is installed by default in OS X we need install and configure PHP. We want to use different versions per vhost so we’ll use php-fpm.

First we need add dependencies for php-fpm:

Then we can start installing PHPs.

Link operation is linking specific version of PHP to compile paths.

Now when we have installed PHP we need to be able to use them with our default Apache installation. For this we’ll use mod_fastcgi. installation simple as in Debian-ish systems:

Now the funny part with making them work together ;) Most helpful was Gist from diemuzi.

First we need to run three php-fpm in the same time on different ports (I’m using http sockets because.) and set proper user and group for them. Edit this file for each version of PHP you installed.

Second thing is FastCgi configuration for Apache. I’m using Apache 2.2.

Last line includes all .conf files from /etc/apache2/sites-enabled directory. I’m placing there symbolic links to vhosts configured in sites-available directory. Just like you do it in Ubuntu.

Then let’s configure our localhost with PHP 5.4 (Like I said I was using XAMPP so I can live with my webpages in this directory :P)

And sample project with PHP 5.3.

Now when you have domain project.dev configured to points to 127.0.0.1 you can see your web directory.

That’s pretty much all. If I’ve forgot about something or something is not clear please leave a comment.

TODO: i need to check how this setup is working with Symfony 2 projects. I’ll write update soon.

UPDATE for Symfony 2

Basic application works fine – copy-paste config once more: