switch PHP Version Linux

von PHP 7.0 nach PHP 7.1:

Apache

sudo a2dismod php7.0 ; sudo a2enmod php7.1 ; sudo systemctl restart apache2

CLI

sudo update-alternatives --set php /usr/bin/php7.1

Die ganz faulen schreiben sich n Skript:

#!/bin/bash
phpVersion=$(php -r "echo PHP_VERSION;")
dotString="\."
dotOffset=2
phpTarget=$1

strposGrep() {
	occurrence=$(echo $1 | grep -aob $2 | grep -oE '[0-9]+' | head -n$3)
	occurrenceLines=$(echo $occurrence | tr " " "\n")
	offset=0
	for hit in $occurrence
	do
		let offset++
		if [ $offset -eq $3 ]
		then
			return $hit
		fi
	done
}
strposGrep $phpVersion $dotString $dotOffset
dotPosition=$?
phpCurrent=${phpVersion:0:dotPosition}
echo "Current PHP version: ${phpCurrent}"
echo "Switch to PHP version: ${phpTarget}"

if [ "$phpTarget" == "$phpCurrent" ]
then
	echo "PHP $phpTarget is allready the current version."
else
	sudo a2dismod php$phpCurrent
	sudo a2enmod php$phpTarget
	sudo systemctl restart apache2

	sudo update-alternatives --set php /usr/bin/php$phpTarget
	echo "Switched to PHP $phpTarget."
fi

Das kann man dann aufrufen:

./switchPhpVersion.sh 7.1

Das PHP PPA gibt es von https://deb.sury.org.