forked from rapidwebltd/php-switch-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch-php.sh
More file actions
executable file
·111 lines (99 loc) · 3.58 KB
/
switch-php.sh
File metadata and controls
executable file
·111 lines (99 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
CURRENT_PHP_VERSION=$(php -v | head -n 1 | cut -d " " -f 2 | cut -f1-2 -d".")
AVAILABLE_PHP_VERSIONS=$(ls /usr/bin/php* | grep -oP 'php([0-9]+\.[0-9]+)' | sed 's/php//g' | sort -rV | uniq)
function change_version() {
local NEW_PHP_VERSION="$1"
if [[ "$NEW_PHP_VERSION" =~ ^[0-9]+$ ]]; then
NEW_PHP_VERSION="${NEW_PHP_VERSION:0:1}.${NEW_PHP_VERSION:1}"
fi
if ! echo "$AVAILABLE_PHP_VERSIONS" | grep -qw "$NEW_PHP_VERSION"; then
echo "Error: PHP version $NEW_PHP_VERSION is not installed."
echo "Available versions: $(echo "$AVAILABLE_PHP_VERSIONS" | tr '\n' ' ')"
exit 1
fi
if [ "$NEW_PHP_VERSION" = "$CURRENT_PHP_VERSION" ]; then
echo "PHP version $NEW_PHP_VERSION is already active."
exit 0
fi
echo "* Starting PHP $NEW_PHP_VERSION FPM service"
sudo service php${NEW_PHP_VERSION}-fpm start > /dev/null
echo "* Enabling Apache PHP $NEW_PHP_VERSION module"
sudo a2enconf php${NEW_PHP_VERSION}-fpm > /dev/null
echo "* Disabling Apache PHP $CURRENT_PHP_VERSION module"
sudo a2disconf php${CURRENT_PHP_VERSION}-fpm > /dev/null
echo "* Stopping PHP $CURRENT_PHP_VERSION FPM service"
sudo service php${CURRENT_PHP_VERSION}-fpm stop > /dev/null
echo "* Restarting Apache..."
sudo service apache2 restart > /dev/null
echo "* Switching CLI PHP to $NEW_PHP_VERSION"
sudo update-alternatives --set php /usr/bin/php${NEW_PHP_VERSION} > /dev/null
echo "* Current PHP version: $(php -v | head -n 1 | cut -d " " -f 2 | cut -f1-2 -d".")"
}
function interactive_menu() {
local prompt="$1" outvar="$2"
shift
shift
local options=("$@") cur=0 count=${#options[@]} index=0
local esc=$(echo -en "\e")
printf "$prompt\n"
while true
do
index=0
for o in "${options[@]}"
do
if [ "$index" == "$cur" ]
then
echo -e " >\e[7m$o\e[0m"
else
echo " $o"
fi
index=$(( $index + 1 ))
done
read -s -n3 key
if [[ $key == $esc[A ]]
then cur=$(( $cur - 1 ))
[ "$cur" -lt 0 ] && cur=0
elif [[ $key == $esc[B ]]
then cur=$(( $cur + 1 ))
[ "$cur" -ge $count ] && cur=$(( $count - 1 ))
elif [[ $key == "" ]]
then break
fi
echo -en "\e[${count}A"
done
printf -v $outvar "${options[$cur]}"
if [[ $cur -gt 0 ]]; then
echo ""
change_version "${options[$cur]}"
fi
}
for arg in "$@"; do
if [ "$arg" = "-h" ]; then
echo "Usage: $0 [php-version]"
echo "Switch the PHP version for Apache and CLI to the specified version."
echo ""
echo "Options:"
echo " <php-version> The desired PHP version (e.g., 8.4). If omitted, shows an interactive menu."
echo " -h Display this help information"
echo ""
echo "Examples:"
echo " $0 8.4 Switch directly to PHP 8.4"
echo " $0 74 Switch directly to PHP 7.4 (the dot is optional)"
echo " $0 Show interactive PHP version selection menu"
echo ""
echo "Available versions: $(echo "$AVAILABLE_PHP_VERSIONS" | tr '\n' ' ')"
exit 0
fi
done
if [ $# -eq 1 ]; then
change_version "$1"
else
options=("Cancel, stay on PHP $CURRENT_PHP_VERSION")
for version in $AVAILABLE_PHP_VERSIONS
do
if [ "$version" != "$CURRENT_PHP_VERSION" ]; then
options+=("$version")
fi
done
interactive_menu "Switch to PHP-version:" selected_choice "${options[@]}"
fi