Last active
November 16, 2017 21:32
-
-
Save Jamiewarb/de90a919c6df36b7be2b4f9f135761d2 to your computer and use it in GitHub Desktop.
PHP Switcher Script (For Homebrew installations)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# If user id not equal to root user id | |
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 | |
# If no params are passed | |
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 | |
# Run php code to grab the phpversion(), grab the first 3 characters (e.g. 7.2) | |
# remove the dot, and echo it. This puts it into the variable | |
#currentversion="`php -r \"error_reporting(0); echo str_replace('.', '', substr(phpversion(), 0, 3));\"`" | |
currentversion="`php -v | grep -o -E 'PHP [0-9].[0-9]' | grep -o '[0-9]' | awk '{print}' ORS=''`" | |
# Grab the new version from the args passed in | |
newversion="$1" | |
# Split into the Major and Minor version e.g. 72 becomes 7 and 2 | |
majorOld=${currentversion:0:1} | |
majorNew=${newversion:0:1} | |
minorNew=${newversion:1:1} | |
# Check this version exists in brew list, but output stdout and stderr to /dev/null | |
brew list php$newversion &> /dev/null | |
# If last command exit status ($?) equals 0 (success) | |
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 | |
echo "Unlinking old binaries..." | |
brew unlink php$currentversion &> /dev/null | |
if [ $? -ne 0 ]; then | |
echo "Problem unlinking old binaries. Exiting" | |
fi | |
echo "Linking new binaries..." | |
brew link php$newversion | |
if [ $? -ne 0 ]; then | |
echo "Problem linking new binaries. Exiting" | |
fi | |
# This should already be done when you run brew install phpxx --with-httpd | |
#echo "Linking new modphp addon..." | |
#sudo ln -sf `brew list php$newversion | grep libphp` /usr/local/opt/php${newversion}/libexec/apache2/libphp${majorNew}.so | |
#echo /usr/local/lib/libphp${majorNew}.so | |
echo "Fixing LoadModule..." | |
# Get the apachectl config file path | |
apacheConf=`apachectl -V | grep -i server_config_file | cut -d '"' -f 2` | |
# Put a # in front of all php modules matching the major version, that don't | |
# already have a hash in front of them, in the apache config file | |
sudo sed -i -e "/LoadModule php${majorOld}_module/s/^#*/#/" $apacheConf | |
# Add the new Load Module, if it doesn't exist, or uncomment it if it does | |
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..." | |
apache=`which apachectl` | |
# Check if native system httpd is running, and restart it if so | |
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 | |
# Check if homebrew httpd is running, and restart it if so | |
pgrep -f /usr/local/"Cellar|opt"/"*"/httpd &> /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 | |
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