-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphp-switch.sh
executable file
·339 lines (260 loc) · 7.88 KB
/
php-switch.sh
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/bin/bash
_php_versions=(
'5.6' '7.0' '7.1' '7.2'
)
_php_exts=(
'bcmath' 'bz2' 'cli' 'common' 'curl' 'dba' 'fpm' 'gd' 'imap'
'interbase' 'intl' 'json' 'ldap' 'mbstring'
'mcrypt'
'mysql' 'opcache' 'pgsql'
'readline' 'soap' 'sqlite3' 'xml' 'xmlrpc' 'xsl' 'zip'
)
_php_ext_shared=(
'redis' 'imagick' 'geoip'
)
args=("$@")
# Messages
export NC='\033[0m' # No Color
export COLOR_WHITE='\033[1;37m'
export COLOR_BLUE='\033[0;34m'
export COLOR_GREEN='\033[0;32m'
export COLOR_RED='\033[0;31m'
export STYLE_BOLD=$(tput bold)
export STYLE_NORMAL=$(tput sgr0)
indent() {
printf '%*s' $@
}
echoe() {
echo -e $@
}
echo_li ()
{
indent 2 && echoe " - $@"
}
msg_error()
{
local msg=${1:-An Error Occured.}
msg=${msg}
echoe ""
indent 1 && \
echoe "${STYLE_BOLD}${COLOR_RED}ERROR: ${COLOR_WHITE}${msg}${NC}" >&2
}
msg_info()
{
local msg=${1:-Info}
msg=${msg}
echoe ""
indent 1 && \
echoe "${STYLE_BOLD}${COLOR_BLUE} ✔ ${COLOR_WHITE}${msg}${NC}" >&2
}
php-switch-add-repo () {
if find /etc/apt/ -name *.list | xargs cat | grep ondrej/php | grep -q .
then
echo "- Repo already ppa:ondrej/php installed"
else
echo "- Updating repos ..."
sudo apt-get update > /dev/null
echo "- Installing Repo ppa:ondrej/php ..."
sudo apt-get install -y software-properties-common > /dev/null
sudo add-apt-repository -y ppa:ondrej/php > /dev/null
echo "- Updating repos ..."
sudo apt-get update
fi
}
php-switch-repo-pkg () {
local repo='/var/lib/apt/lists/ppa.launchpad.net_ondrej_php_ubuntu_dists_xenial_main_binary-i386_Packages'
local php_ver=${args[1]}
case ${php_ver} in
5.6|7.0|7.1|7.2)
msg_info "Available Packages for ${php_ver}: \n"
grep "Package: php${php_ver}-" "${repo}" | sed -n "s/Package:/ /p"
echo ""
;;
all)
echo "Uninstalling All PHP Versions"
for i in ${_php_versions[*]}; do
php-switch-repo-pkg "${i}"
done
php-switch-repo-pkg "shared"
;;
shared)
msg_info "Available Shared Packages: "
grep "Package: php-" "${repo}" | sed -n "s/Package:/ /p"
echo ""
;;
*)
msg_error "Unknown PHP Version"
;;
esac
}
php-switch-info () {
php-switch-ver
msg_info "Avaialble PHP Versions :"
for i in ${_php_versions[*]}; do
echo_li "$i"
done
echo -e ""
msg_info "Avaialble Extentions :"
for i in ${_php_exts[*]}; do
echo_li "$i"
done
echo -e ""
}
php-switch-uninstall () {
echo ""
local php_ver=${args[1]}
case ${php_ver} in
5.6|7.0|7.1|7.2)
msg_info "Uninstalling PHP " ${php_ver} "\n"
echo ""
sudo apt-get remove -y php${php_ver}-*
sudo apt-get purge -y php${php_ver}-*
sudo apt-get remove -y libapache2-mod-php${php_ver}
sudo apt-get purge -y libapache2-mod-php${php_ver}
;;
all)
msg_info "Uninstalling All PHP Versions"
for i in ${_php_versions[*]}; do
php-switch-uninstall "${i}"
done
;;
*)
msg_error "Unknown PHP Version"
;;
esac
}
php-switch-install () {
echo ""
local php_ver=${args[1]}
local pkgs
case ${php_ver} in
5.6|7.0|7.1|7.2)
msg_info "Installing PHP " ${php_ver}
echo ""
pkgs=$(printf "php${php_ver}-%s " "${_php_exts[@]}")
if [[ "7.2" == "${php_ver}" ]]; then
msg_info "Skip mcrypt ...."
pkgs=`echo -e ${pkgs} | sed -n "s/php${php_ver}-mcrypt//p"`
fi
sudo apt-get install -y --ignore-missing ${pkgs}
sudo apt install libapache2-mod-php${php_ver}
echo ""
# msg_info "Installing Shared PHP Extensions"
# for i in ${_php_ext_shared[*]}; do
# php-switch-install ext "${i}"
# done
;;
all)
msg_info "Installing All PHP Versions"
for i in ${_php_versions[*]}; do
php-switch-install "${i}"
done
;;
ext)
msg_info "Installing PHP Ext ${args[1]}..."
sudo apt-get install -y --ignore-missing "php-${args[1]}"
;;
*)
msg_error "Unknown PHP Version"
;;
esac
}
php-switch-ver () {
local VERSION
VERSION="$(php --version | head -n 1 | cut -d " " -f 2 | cut -c 1,3)"
printf "\n"
msg_info "$COLOR_GREEN PHP Version $NC ${VERSION}"
echo ""
}
php-switch-to () {
local php_ver=${args[1]}
if [[ $@ ]]; then
if [[ ! -f "/usr/bin/php${php_ver}" ]]; then
msg_error "PHP ${php_ver} Not installed \n ${NC}"
echo_li "Use php-switch-install ${php_ver}"
return
fi
msg_info "Switching to PHP Version $php_ver ... \n"
if [[ $(sudo service --status-all | grep "apache2") ]]; then
msg_info "Disabling Apache Modules ..."
for i in ${_php_versions[*]}; do
if [[ -f "/usr/bin/php${i}" ]]; then
msg_info "Disabling Apache Module php${i}"
sudo a2dismod php${i} > /dev/null
fi
done
echo ""
msg_info "Enabling Apache Module php${php_ver}"
sudo a2enmod php${php_ver} > /dev/null
echo ""
msg_info "Restarting Apache ..."
sudo service apache2 restart
else
echo ""
msg_info "Apache Missing : Skip \n"
fi
if [[ $(sudo service --status-all | grep "php") ]]; then
echo ""
msg_info "Disabling PHP-FPM Services ..."
for i in ${_php_versions[*]}; do
if [[ -f "/usr/bin/php${i}" ]]; then
msg_info "Disabling PHP FPM php${i}"
# sudo service php${i}-fpm stop
sudo systemctl stop php${i}-fpm.service --no-pager
sudo systemctl disable php${i}-fpm.service --no-pager
fi
done
echo ""
msg_info "Enabling PHP FPM ${php_ver} ..."
# sudo service php${php_ver}-fpm start
sudo systemctl enable php${i}-fpm.service --no-pager
sudo systemctl start php${i}-fpm.service --no-pager
fi
msg_info "Updating PHP Cli ..."
sudo update-alternatives --set php "/usr/bin/php${php_ver}" > /dev/null
if [[ $(sudo update-alternatives --get-selections \
| grep "phpize") ]]; then
msg_info "Updating PHPize ..."
sudo update-alternatives \
--set phpize "/usr/bin/phpize${php_ver}" > /dev/null
fi
if [[ $(sudo update-alternatives --get-selections \
| grep "php-config") ]]; then
msg_info "Updating PHP Config ... "
sudo update-alternatives \
--set php-config "/usr/bin/php-config${php_ver}" > /dev/null
fi
php-switch-ver
else
msg_error "No version specified"
fi
}
php-switch-help() {
echo ""
indent 2
echo "Usage: php-switch.sh [ver|info|install|uninstall|repo-pkg|add-repo]"
echo ""
indent 2
echo "Example : php-switch.sh install 7.0"
indent 2
echo "Example : php-switch.sh to 7.0"
}
cmd=${args[0]}
if [ "$cmd" = "ver" ]; then
php-switch-ver
elif [ "$cmd" = "repo-pkg" ]; then
php-switch-repo-pkg
elif [ "$cmd" = "add-repo" ]; then
php-switch-add-repo
elif [ "$cmd" = "info" ]; then
php-switch-info
elif [ "$cmd" = "install" ]; then
php-switch-install
elif [ "$cmd" = "uninstall" ]; then
php-switch-uninstall
elif [ "$cmd" = "to" ]; then
php-switch-to
else
php-switch-help
php-switch-info
fi