Skip to content

Instantly share code, notes, and snippets.

@arm1n
Created February 2, 2013 13:31
Show Gist options
  • Save arm1n/4697397 to your computer and use it in GitHub Desktop.
Save arm1n/4697397 to your computer and use it in GitHub Desktop.
This is a convenience script to switch between different php5 versions installed via macports. The script simply changes the httpd.conf file located in /opt/local/apache2/conf/httpd.conf with sed. It's believed that you have installed your php versions with 'sudo port install php5x-apache2handler'.
#!/bin/bash
#-------------------------------------------------------------------------------
# PHPSWAP for changing php5 versions installed via macports in apache2 config.
#
# @author [email protected]
# @license MIT License
#
# Kindly referring to the following useful help pages:
# http://mark-story.com/posts/view/maintaining-two-versions-of-php-with-macports
# https://gist.github.com/2721719
#
#-------------------------------------------------------------------------------
# manual
USAGE=""\
"-------------------------------------------------------------------------------\n"\
"PHPSWAP for changing php5 versions installed via macports in apache2 config\n"\
"-------------------------------------------------------------------------------\n\n"\
"This is a convenience script to switch between different php5 versions installed via macports.\n"\
"The script simply changes the httpd.conf file located in /opt/local/apache2/conf/httpd.conf with sed.\n"\
"It's believed that you have installed your php versions with 'sudo port install php5x-apache2handler'.\n"\
"Depending on your installed php versions from macports you can use the script as follows:\n\n"\
"sudo phpswap -v php5x ... specify php version to be activated.\n"\
# dependents
VERSION=
# variables
BASE="/opt/local"
PORT="$BASE/bin/port"
APACHE="$BASE/apache2"
MODULES="$APACHE/modules"
SERVER="$APACHE/bin/apachectl"
CONFIG="$APACHE/conf/httpd.conf"
# arguments
while getopts ":hv:" opt; do
case "$opt" in
v)
VERSION=$OPTARG
;;
h)
echo -e $USAGE
exit 1
;;
\?)
echo "Invalid option: -$OPTARG"
exit 1
;;
:)
echo "Option -$OPTARG requires an argument."
exit 1
;;
*)
echo "Option -$OPTARG not implemented."
exit 1
;;
esac
done
# check for macports
if [ ! -f "$PORT" ] ; then
echo "Cannot find macport installation: $PORT"
exit 1
fi
# check for apache2 config
if [ ! -f "$CONFIG" ] ; then
echo "Cannot find apache2 config file: $CONFIG"
exit 1
fi
# check -v option general
if [ -z "$VERSION" ] ; then
echo "Option -v is mandatory."
exit 1
fi
# check -v option pattern
if [[ ! "$VERSION" =~ ^php5[0-9]{1}$ ]] ; then
echo "Option -v needs pattern 'php5x'."
exit 1
fi
# check if given php version module exists
if [ ! -f "$APACHE/modules/mod_$VERSION.so" ] ; then
echo "Cannot find php module for given version: $VERSION"
exit 1
fi
# check sudo privileges
if [[ $EUID -ne 0 ]] ; then
echo "This script has to be executed with sudo."
exit 1
fi
# update 'LoadModule mod_php5x.so'
# within apache2's httpd.conf file
NEWMODULE="modules\/mod_$VERSION\.so"
OLDMODULE="modules\/mod_php5[0-9]\{1\}\.so"
sed -i '' -e "s/$OLDMODULE/$NEWMODULE/" "$CONFIG"
# we're done, apply changes
if [ -f "$SERVER" ] ; then
$SERVER restart # try to restart server
echo "SUCCESS: Activated '$VERSION' in '$CONFIG'"
else
echo -e "Cannot find apache2 server: $SERVER.\n\nPlease restart manually to apply changes."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment