Skip to content

Instantly share code, notes, and snippets.

@ivanweiler
Created April 24, 2019 07:21
Show Gist options
  • Save ivanweiler/7de864bc1f45b57ee14580d31e5711dc to your computer and use it in GitHub Desktop.
Save ivanweiler/7de864bc1f45b57ee14580d31e5711dc to your computer and use it in GitHub Desktop.
#!/bin/bash
###
# Script for switching PHP versions
# Ivan Weiler
#
# Reference:
# http://robosparrow.github.io/2016/12/10/php-5-on-ubuntu16.html
# https://labbots.com/bash-script-to-switch-php-versions-in-ubuntu/
###
GREEN="$(tput setaf 2)"
RED="$(tput setaf 1)"
RESET="$(tput sgr0)"
# CURRENT="$(php --version | head -n 1 | cut -d " " -f 2 | cut -c 1,3)"
# $(php --version | head -n 1 | cut -d " " -f 2 | cut -c-3)
# php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;'
CURRENT="$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;')"
if [ -z "$1" ]
then
echo "${RED}Enter a PHP version: 5.6|7.0|7.1|7.2${RESET}";
exit;
fi
if [ $1 = $CURRENT ]
then
echo "${GREEN}This version is running!${RESET}";
exit;
fi
printf "${GREEN}Switching to PHP %s..${RESET}\n" "$1"
sudo a2dismod php$CURRENT
sudo a2enmod php$1
sudo update-alternatives --set php /usr/bin/php$1
sudo service apache2 restart && echo "${GREEN}$(php --version | head -n 1)${RESET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment