Skip to content

Instantly share code, notes, and snippets.

@moismailzai
Forked from w00fz/sphp.sh
Last active April 5, 2017 01:50
Show Gist options
  • Save moismailzai/765007c45f908085ec5807e4463606b1 to your computer and use it in GitHub Desktop.
Save moismailzai/765007c45f908085ec5807e4463606b1 to your computer and use it in GitHub Desktop.
PHP switcher
#!/bin/bash
# Check if command was ran as root.
if [[ $(id -u) -eq 0 ]]; then
echo "The command \"sphp\" should not be executed as root or via sudo directly."
echo "When a service requires root access, you will be prompted for a password as needed."
exit 1
fi
# Usage
if [ $# -ne 1 ]; then
echo "Usage: sphp [phpversion]"
echo "Versions installed:"
brew list | grep '^php[0-9]\{2,\}$' | grep -o -E '[0-9]+' | while read -r line ; do
echo " - phpversion: $line"
done
exit 1
fi
currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`"
newversion="$1"
majorOld=${currentversion:0:1}
majorNew=${newversion:0:1}
minorNew=${newversion:1:1}
homebrewMariaDB=false
# Check to see if MariaDB is running (do it now because switching php versions will cause it to stop)
if pgrep -f /usr/local/Cellar/mariadb/*/bin/mysqld 2> /dev/null > /dev/null; then
homebrewMariaDB=true
fi
brew list php$newversion 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
echo "PHP version $newversion found"
# Check if new version is already the current version.
# if [ "${newversion}" == "${currentversion}" ]; then
# echo -n "PHP version ${newversion} is already being used. Continue by reloading? (y/n) "
# while true; do
# read -n 1 yn
# case $yn in
# [Yy]* ) echo && break;;
# [Nn]* ) echo && exit 1;;
# esac
# done
# fi
# Check for orphaned ini files in php's conf.d (which will throw and break the script otherwise)
FILES="/etc/php/$majorNew.$minorNew/conf.d/*.ini";
for f in $FILES
do
# sed regex courtesy of @benkmugo
SHARED_EXTENSION=`grep extension= "$f" | sed 's/\(^.*extension="\)\(.*\)"/\2/'`;
if [ ! -f $SHARED_EXTENSION ];
then
echo "Warning: The file $f points to a shared extension that doesn't exist ($SHARED_EXTENSION). This will likely cause PHP errors and usually happens as a result of an incomplete installation. Automatically rename this file to $f.BAK?"
select yn in "Yes" "No"; do
case $yn in
Yes ) mv $f $f.BAK; break;;
No ) break;;
esac
done
fi
done
echo "Unlinking old binaries..."
brew unlink php$currentversion 2> /dev/null > /dev/null
echo "Linking new binaries..."
brew link php$newversion
# Add a link to the loaded php version's php.ini file @ /usr/local/etc/php/php.ini
echo "Linking new php.ini...(from /usr/local/etc/php/$majorNew.$minorNew/php.ini to /usr/local/etc/php/php.ini)"
ln -sf /usr/local/etc/php/$majorNew.$minorNew/php.ini /usr/local/etc/php/php.ini
echo "Linking new modphp addon..."
sudo ln -sf `brew list php$newversion | grep libphp` /usr/local/lib/libphp${majorNew}.so
echo /usr/local/lib/libphp${majorNew}.so
echo "Fixing LoadModule..."
apacheConf=`httpd -V | grep -i server_config_file | cut -d '"' -f 2`
sudo sed -i -e "/LoadModule php${majorOld}_module/s/^#*/#/" $apacheConf
if grep "LoadModule php${majorNew}_module .*php${newversion}" $apacheConf > /dev/null
then
sudo sed -i -e "/LoadModule php${majorNew}_module .*php${newversion}/s/^#//" $apacheConf
else
sudo sed -i -e "/LoadModule php${majorNew}_module/s/^#//" $apacheConf
fi
echo "Updating version file..."
pgrep -f /usr/sbin/httpd 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
echo "Restarting system Apache..."
sudo pkill -9 -f /usr/sbin/httpd
sudo /usr/sbin/apachectl -k restart > /dev/null 2>&1
fi
pgrep -f /usr/local/"Cellar|opt"/*/httpd 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
echo "Restarting homebrew Apache..."
sudo pkill -9 -f /usr/local/"Cellar|opt"/*/httpd
sudo /usr/local/bin/apachectl -k restart > /dev/null 2>&1
fi
# pgrep -x httpd 2> /dev/null > /dev/null
# if [ $? -eq 0 ]; then
# echo "Restarting non-root homebrew Apache..."
# httpd -k restart > /dev/null 2>&1
# fi
# Restart homebrew MariaDB if it was running when the switcher script was called
if $homebrewMariaDB; then
echo "Restarting homebrew MariaDB..."
mysql.server restart > /dev/null 2>&1
fi
echo "Done."
# Show PHP CLI version for verification.
echo && php -v
else
echo "PHP version $majorNew.$minorNew was not found."
echo "Try \`brew install php${newversion}\` first."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment