A friend is thinking about buying MAMP Pro so he can test on different versions of PHP. Since I just set up version-switching myself, for free, here’s how I did it. Hopefully it’ll save someone a bit of money or frustration.
This is happening on a Mac, with the latest version of OS X, (Currently 10.11.3 El Capitan), using the pre-installed version of Apache, and Homebrew as a package manager.
Step 1: Install more than one version of PHP on your system
Using Homebrew you can install a version of PHP, then “unlink” it so it’s not currently being used:
brew install php7 brew unlink php7
Step 2: Make it easy to change the PHP version Apache is using
First, stop the main Apache config, (
/etc/apache2/httpd.conf), file from loading PHP. Find any line that mentions PHP modules and comment it out, (put a
# at the beginning of the line). For example:
#LoadModule php5_module libexec/apache2/libphp5.so #LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so
Now we need an Apache config file for each PHP version that you want to run. I’ve put these files in
/etc/apache2/other/ and used the file extension “
.conffile” to prevent Apache from auto-loading them:
Each of your
.conffile files needs to have the location of the PHP library, and any extra PHP info. This is the contents of my
LoadModule php7_module /usr/local/opt/php70/libexec/apache2/libphp7.so <IfModule php7_module> AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps </IfModule> <IfModule dir_module> DirectoryIndex index.html index.php </IfModule>
php-5.conffile is very similar, but has the path of the PHP 5
.so file and the
IfModule conditional checks for
If you have any existing
php*.conf files in
/etc/apache2/other/ they can be blank.
Next we need to make an easy way to tell Apache to use one of the
.conffiles, which I’ve done by creating a symbolic link to the one I want to use. Since I don’t like typing out ln commands I’ve created a script, which I call
#!/bin/bash if [ $1 = 7 ]; then PHP_VERSION=7 else PHP_VERSION=5 fi `sudo ln -Fs /etc/apache2/other/php-$PHP_VERSION.conffile /etc/apache2/other/php.conf` `sudo apachectl graceful` echo "PHP version switched to $PHP_VERSION"
This script symlinks
/etc/apache2/other/php.conf to the
.conffile that I want to use, (using the
-F switch to overwrite the old symlink), and restarts Apache. It needs sudo so it’ll ask for your password.
Finally, it’s nice to have
php-v.sh available from everywhere, so I’ve created a symlink from
~/bin/php-v, which is in my
PATH, to the actual script:
ln -s /Users/John/Developer/Scripts/php-v.sh /Users/John/bin/php-v
And now I can switch PHP versions at will: