Using different PHP versions on OSX with Apache and Homebrew
February 4, 2015
By default Apple's OSX (Mavericks) comes with PHP 5.4 installed but if you want to work on a different version you're pretty stuck.
My solution to this is to use homebrew to install PHP5.3, 5.4, 5.5 and 5.6 and then simply change the PHP module loaded by apache. Here's how to do it.
Step 1: Install Homebrew
Go to http://brew.sh/ and follow the instructions.
Step 2: Install the different PHP versions
$ brew install php53 ... stuff happens for a while $ brew install php54 ... stuff happens for a while $ brew install php55 ... stuff happens for a while $ brew install php56 ... stuff happens for a while
You may also need to install version specific PHP modules. To find these you should use homebrew's search feature. For example, to install the Mcrypt module:
$ brew search php5 | grep mcrypt php53-mcrypt php54-mcrypt php55-mcrypt php56-mcrypt
Install these for the PHP version you need (or all of them is so required)
$ brew install php53-mcrypt php54-mcrypt php55-mcrypt php56-mcrypt
Step 3: Switching PHP versions for command line usage
Note: If you only develop in the browser you don't need to do this, you can simply switch the Apache PHP modules as show in step 4.
To switch between versions you should use the link and unlink features of homebrew. For example, to switch from the current PHP version to php5.5:
$ php -v PHP 5.4.35 (cli) (built: Dec 4 2014 12:37:20) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies $ brew unlink php54 Unlinking /usr/local/Cellar/php54/5.4.35... 38 symlinks removed $ brew link php55 Linking /usr/local/Cellar/php55/5.5.20... 38 symlinks created $ php -v PHP 5.5.20 (cli) (built: Feb 4 2015 14:15:18) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
Step 4: Using different PHP version in Apache
Edit /etc/apache2/httpd.conf and look for the line that loads the PHP module. On my installation this is around line 120. Duplicate the current line and change the path to the module for the version of PHP yopu want to use. Comment out the other versions (using the hash key '#'). Here's my current Apache config file:
119 120 # LoadModule php5_module libexec/apache2/libphp5.so 121 # LoadModule php5_module /usr/local/Cellar/php53/5.3.29/libexec/apache2/libphp5.so 122 LoadModule php5_module /usr/local/Cellar/php54/5.4.36/libexec/apache2/libphp5.so 123 # LoadModule php5_module /usr/local/Cellar/php55/5.5.20/libexec/apache2/libphp5.so 124 # LoadModule php5_module /usr/local/Cellar/php56/5.6.4/libexec/apache2/libphp5.so 125
As you can see, it is currently set to use PHP5.4.36.
Once you've set up the module you want, you will need to restart the Apache Web Server:
sudo apachectl restart
Verify this is correct by creating a PHP file in your web root with the call to
phpinfo() and then visiting the page: