Skip to content

Instantly share code, notes, and snippets.

@Michael-Stokoe
Forked from judgej/setphp
Last active August 26, 2021 18:22
Show Gist options
  • Save Michael-Stokoe/df5e2422008bb7c4878e4939aa99836b to your computer and use it in GitHub Desktop.
Save Michael-Stokoe/df5e2422008bb7c4878e4939aa99836b to your computer and use it in GitHub Desktop.
bash script to switch between PHP versions in Plesk
# Installation: Add this file to the bottom of your ~/.bashrc
# Or: $ curl https://gist.githubusercontent.com/judgej/318b59b1e57867ffd2968dfa1521421a/raw >> ~/.bashrc
# Usage: $ setphp {version}
# Example: $ setphp 7.4
# It will look for the path to the current PHP version in your PATH and switch to
# the new PHP version.
# If you do not have a path to a php version, then it will add one.
# If the version you specify does not exist, it will list the versions that are available.
# I wrote this for a Plesk server with various applications running under different versions
# of PHP, allowing me to quickly swicth between them on the command line.
# https://stackoverflow.com/questions/8063228/how-do-i-check-if-a-variable-exists-in-a-list-in-bash
function list_include_item {
local list="$1"
local item="$2"
if [[ $list =~ (^|[[:space:]])"$item"($|[[:space:]]) ]] ; then
# yes, list include item
result=0
else
result=1
fi
return $result
}
function setphp() {
# The location of the PHP versions supplied by Plesk.
local BASEDIR="/opt/plesk/php/"
# The requested PHP version.
local VERSION=$1
# The available PHP versions.
local VERSIONS=$(ls $BASEDIR)
if $(list_include_item "$VERSIONS" "$VERSION") ; then
echo $PATH
if [[ $PATH = *"$BASEDIR"* ]]; then
PATH=$(echo $PATH | sed 's#'$BASEDIR'[0-9.]*[^/]/bin#'$BASEDIR$VERSION'/bin#')
echo "Updated:"
else
echo "Added:"
PATH="$BASEDIR$VERSION/bin:$PATH"
fi
echo $PATH
else
echo Usage: $(basename $0) php-version
echo Versions are: $VERSIONS
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment