forked from shivammathur/php-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch_sapi
More file actions
208 lines (188 loc) · 5.59 KB
/
Copy pathswitch_sapi
File metadata and controls
208 lines (188 loc) · 5.59 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
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
#!/usr/bin/env bash
#
# MIT License
#
# Copyright (c) Shivam Mathur <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Function to print usage.
usage() {
read -r -d '' USAGE <<EOM
Usage: switch_switch COMMANDS...
Configure PHP SAPIs with servers.
COMMANDS:
-v=PHP_VERSION PHP version
-s=SAPI|SAPI:SERVER SAPI or SAPI:SERVER {apache:apache|fpm:apache|cgi:apache|fpm:nginx|apache|fpm|embed|cgi|phpdbg}
-h Print this help message.
EXAMPLES:
switch_sapi -v 8.0 -s cgi:apache
switch_sapi -v 8.1 -s fpm:nginx
EOM
echo "$USAGE"
exit 1
}
# Function to print a warning.
warning() {
[ -n "$1" ] && echo "WARNING: $1"
exit 1
}
# Function to check if a package is installed.
check_installed() {
dpkg --get-selections "$1" 2>&1 | grep -q '\sinstall'
}
# Function to check if there is a package with input name.
check_package() {
! dpkg --get-selections "$1" 2>&1 | grep -q 'no package'
}
# Function to restart the server.
service_restart() {
services=("$@")
for service in "${services[@]}"; do
service "$service" restart >/dev/null 2>&1 ||
service "$service" restart >/dev/null 2>&1 ||
service "$service" start >/dev/null 2>&1
done
}
# Function to install apache2.
install_apache2() {
service nginx stop 2>/dev/null || true
if ! check_installed apache2; then
$apt_install apache2-bin apache2 libapache2-mod-fcgid
else
if ! [[ "$(apache2 -v 2>/dev/null | grep -Eo "([0-9]+\.[0-9]+)")" =~ 2.[4-9]+|2.[1-9][0-9]+ ]]; then
apt-get purge apache* apache-* >/dev/null
$apt_install apache2-bin apache2 libapache2-mod-fcgid
fi
fi
sed -i -e 's/60/5/' -e 's/20/5/' /etc/init.d/apache2
. /etc/apache2/envvars
echo "ServerName localhost" | tee -a /etc/apache2/apache2.conf >/dev/null 2>&1
}
# Function to install nginx.
install_nginx() {
service apache2 stop 2>/dev/null || true
if ! check_installed nginx; then
$apt_install nginx
fi
}
# Function to install a PHP SAPI package.
install_php_sapi() {
local sapi=$1
phpquery -S -v "$php" | grep -q "$sapi" && return
package="php$php-$sapi"
if [ "$sapi" = "apache" ]; then
package="libapache2-mod-php$php"
elif [ "$sapi" = "embed" ]; then
package="libphp$php-embed"
fi
if check_package "$package" && ! check_installed "$package"; then
$apt_install "$package"
fi
}
# Function to set up apache2handler SAPI with Apache2.
setup_apache_apache() {
install_apache2
install_php_sapi apache
a2disconf php"$php"-fpm || true
a2dismod mpm_event proxy_fcgi 2>/dev/null || true
a2enmod mpm_prefork php"$php"
service_restart apache2
}
# Function to set up fpm SAPI with Apache2.
setup_fpm_apache() {
install_apache2
install_php_sapi fpm
a2dismod php"$php" 2>/dev/null || true
a2enmod proxy_fcgi
a2enconf php"$php"-fpm
service_restart php"$php"-fpm apache2
}
# Function to set up cgi SAPI with Apache2.
setup_cgi_apache() {
install_apache2
install_php_sapi cgi
a2dismod php"$php" mpm_event 2>/dev/null || true
a2enmod mpm_prefork actions cgi
a2disconf php"$php"-fpm 2>/dev/null || true
a2enconf php"$php"-cgi
service_restart apache2
}
# Function to set up fpm SAPI with Nginx.
setup_fpm_nginx() {
install_nginx
install_php_sapi fpm
service_restart php"$php"-fpm nginx
}
apt_mgr='apt-get'
command -v apt-fast >/dev/null && apt_mgr='apt-fast'
apt_install="$apt_mgr -y -o Dpkg::Options::=--force-confdef --no-install-recommends install"
echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections 2>/dev/null ||
warning "Current user does not have the permission to perform this action"
# Read inputs
while getopts 'v:s:h' opt; do
case $opt in
v)
if [[ -n "$version" ]]; then
warning "Please specify a single valid PHP versions."
fi
php="$OPTARG"
;;
s)
if [[ -n "$sapi" ]]; then
warning "Please specify a single valid SAPI or SAPI:SERVER."
fi
sapi="$OPTARG"
;;
\? | h) usage ;;
esac
done
# Exit on no input.
[[ -z "$php" ]] && warning "Please specify a valid PHP versions."
[[ -z "$sapi" ]] && warning "Please specify a valid SAPI or SAPI:SERVER."
# Invoke functions based on input.
case $sapi in
apache*:apache*)
setup_apache_apache >/dev/null 2>&1
;;
fpm:apache*)
setup_fpm_apache >/dev/null 2>&1
;;
cgi:apache*)
setup_cgi_apache >/dev/null 2>&1
;;
fpm:nginx)
setup_fpm_nginx >/dev/null 2>&1
;;
apache*)
install_php_sapi apache
service_restart apache2 >/dev/null 2>&1
;;
fpm)
install_php_sapi fpm
service_restart php"$php"-fpm >/dev/null 2>&1
;;
embed | cgi | phpdbg)
install_php_sapi "$sapi"
;;
*)
echo "Invalid sapi/sapi:server."
usage
;;
esac
exit 0