Skip to content

Instantly share code, notes, and snippets.

@ToX82
Last active July 7, 2020 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ToX82/7298f2aa460a5f23491a95b409336d1e to your computer and use it in GitHub Desktop.
Save ToX82/7298f2aa460a5f23491a95b409336d1e to your computer and use it in GitHub Desktop.
A simple script that helps you choose which PHP version should be active - apache version
#!/bin/bash
color_off='\033[0m' # Text Reset
color_green='\033[1;32m' # Green text
versions=(/usr/bin/php*)
mapfile -t versions < <(printf '%s\n' "${versions[@]}" | egrep '[0-9]+\.[0-9]+' | cut -c 13-)
counter=0
if [ $# -eq 0 ]; then
current=$(php -v | grep --only-matching --perl-regexp "(PHP )\d+\.\\d+\.\\d+")
current=${current:4:3}
echo ""
echo 'Installed PHP versions:'
for i in "${versions[@]}"; do
if [ "$current" == "$i" ]; then
echo -e "$(($counter + 1))) PHP $i ${color_green}<- current${color_off}"
else
echo "$(($counter + 1))) PHP $i"
fi
counter=$(($counter + 1))
done
echo ""
echo 'Which version would you like to activate?'
read selected
chosen="${versions[$selected - 1]}"
else
echo ""
echo 'Installed PHP versions:'
for i in "${versions[@]}"; do
if [ $1 == $i ]; then
echo -e "$(($counter + 1))) PHP $i ${color_green}<- activating...${color_off}"
chosen="${versions[$counter]}"
else
echo "$(($counter + 1))) PHP $i"
fi
counter=$(($counter + 1))
done
fi
if [ -z "$chosen" ]; then
echo "The selected PHP version is not installed!"
exit 1
fi
for i in "${versions[@]}"; do
sudo a2dismod php$i &>/dev/null
done
echo ""
echo -e "${color_green}Activating PHP $chosen...${color_off}"
echo ""
sudo a2enmod php$chosen
sudo service apache2 restart
sudo ln -sfn /usr/bin/php$chosen /etc/alternatives/php
echo ""
echo -e "${color_green}PHP $chosen is now active!${color_off}"
@ToX82
Copy link
Author

ToX82 commented May 20, 2020

This is what it looks like:
image

@ToX82
Copy link
Author

ToX82 commented Jun 2, 2020

The script can now run with a parameter (eg. $ phpswitch 7.4) or without parameters (eg. $ phpswitch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment