Switching between php versions with macports
March 9th, 2013
As thing are moving along in PHP versions, and as we start seeing more and more of php 5.5, I find it very useful to be able to switch between PHP versions. If you're on OSX, and using macports, switching is easy enough. There is a port called "php_select" that will help you choose the currently active php version.
sudo port install php_select
Once installed, it will allow you to select the active php version. Using it is simple:
sudo port select php php54
If you want to know more about port select, run:
sudo port help select
Now, while being able to select the "active" php port is nice, that's really only half the picture. Selecting the active php port sets the correct symlinks for:
- /opt/local/bin/php
- /opt/local/bin/php-config
- /opt/local/bin/phpize
But usually, when I want to switch PHP versions, I want to check some functionality right away. I want to reload the web browser and check if everything works (preparing for the upgrade to PHP5.4 on production for example), or check some profiler output (to see if there are major differences in performance with PHP5.5).
That's where the following bash script comes in:
#!/usr/bin/env bash
SUDO=/usr/bin/sudo
PORT=/opt/local/bin/port
EGREP=/usr/bin/egrep
if [ $# -lt 1 ]; then
echo "Usage: $0 phpversion"
echo "Example: $0 54 (for php 5.4)"
exit;
fi
VERSION=$1;
# Unload all installed PHP versions
declare INSTALLED=`$SUDO $PORT select php | $EGREP -v 'none|Available'`
while read -r line; do
line=${line% *}"-fpm"
$SUDO $PORT unload $line > /dev/null 2>&1
done <<< "$INSTALLED"
$SUDO $PORT load php$VERSION-fpm;
$SUDO $PORT select php php$1
To use it, save the contents to a file called “phpswitch” (or something else of your choice), save it in your path and make in executable. Then you can do:
phpswitch 54
Obviously, this will also work for 53, 55 or even 60. (yes php6 has existed for almost four years now!). The script will stop (port unload) any versions of PHP that you have installed. Then it will start (port load) the version you requested, and switch to that version so any commandline calls are directed to the correct php version. Make sure you point all your php versions to the same port or socket file in /opt/local/etc/php{version}/php-fpm.ini
listen = /opt/local/var/run/php-fpm.sock
Pointy haired boss
Do you have similar issues? Contact us at Future500
We can tackle your technical issues while you run your business.
Check out what we do or write us at info@future500.nl