-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall
executable file
·270 lines (248 loc) · 8.33 KB
/
install
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
#!/usr/bin/env bash
set -eo pipefail; [[ -n "$PLUSHU_TRACE" ]] && set -x
echo_install_information () {
local distro=""
if [[ -f /etc/os-release ]]; then
distro=`sed -n 's/^ID=\(.*\)$/\1/p' /etc/os-release`
fi
case $distro in
arch)
cat <<"EOF"
To install Nginx on Arch Linux, run
# pacman -S nginx
EOF
;;
ubuntu)
cat <<"EOF"
To install the latest stable Nginx 1.4.x with WebSocket support, run
# add-apt-repository -y ppa:nginx/stable
# apt-get update
# apt-get install -y nginx dnsutils
See http://wiki.nginx.org/Install#Ubuntu_PPA for more info.
EOF
;;
*)
cat <<"EOF"
Look up how to install Nginx on your system:
http://wiki.nginx.org/Install
EOF
;;
esac
}
setup_sudoers () {
if [[ "$EUID" == 0 ]]; then
if fgrep -qx '#includedir /etc/sudoers.d' /etc/sudoers; then
echo "/etc/sudoers includes /etc/sudoers.d... good."
echo "Installing plushu-reload-nginx rule as a file in sudoers.d."
# Write the file in a umasked subshell
(umask 0226 && printf '%s\n' \
"$PLUSHU_USER ALL=(ALL)NOPASSWD:`command -v nginx` -s reload" \
>/etc/sudoers.d/plushu-reload-nginx)
else
echo "Your /etc/sudoers doesn't include /etc/sudoers.d."
included_dirs=`sed -n 's/^#includedir\s*//p' /etc/sudoers`
if [[ -n "$included_dirs" ]]; then
echo "It does include these directories:"
echo
echo "$included_dirs"
echo
echo "Please post an issue, including which distribution you are"
echo "installing on and which directories it includes, at:"
echo "https://github.com/plushu/plushu-nginx/issues"
echo
fi
echo "This is a common directive in most modern sudo configurations"
echo "(Debian has shipped with it since 1.7.2p1-1 in 2009)."
echo "Either add this line to your configuration:"
echo
echo "#includedir /etc/sudoers.d"
echo
echo "and then reinstall this plugin (as root) to install the file for"
echo "the relevant directive to reload Nginx, or add the line for"
echo "the directive to /etc/sudoers directly:"
echo
echo "$PLUSHU_USER ALL=(ALL)NOPASSWD:`command -v nginx` -s reload"
echo
fi
else
echo "The plushu user needs to be granted sudoers permission"
echo "to reload the Nginx configuration."
echo "To create it, reinstall this plugin as root, or add"
echo "this line to your sudoers configuration:"
echo
echo "$PLUSHU_USER ALL=(ALL)NOPASSWD:`command -v nginx` -s reload"
echo
fi
}
check_for_sudo () {
if command -v sudo >/dev/null 2>&1; then
echo "sudo appears to be installed... good."
setup_sudoers
else
echo "sudo needs to be installed for this user to reload nginx."
# If you're using a distro that doesn't come with sudo, you don't need me
# to explain how to install it to you
echo "Install sudo, then reinstall this plugin."
fi
}
check_nginx_run_status () {
if command -v systemctl >/dev/null 2>&1; then
if systemctl -q is-enabled nginx.service; then
echo "Nginx is enabled via systemctl... good."
else
echo "Nginx does not appear to be enabled. Enable it with:"
echo
echo "# systemctl enable nginx.service"
echo
fi
if systemctl -q is-active nginx.service; then
echo "Nginx is currently running... good."
else
echo "Nginx does not appear to be currently running. Start it with:"
echo
echo "# systemctl start nginx.service"
echo
fi
else
if pgrep -x nginx >/dev/null 2>&1; then
echo "Nginx appears to be running... good."
else
echo "Nginx doesn't appear to be running."
echo "You should start it with something like:"
echo
echo "# service start nginx"
echo
fi
echo "Make sure Nginx is set to run automatically at startup."
fi
}
nginx_plushu_conf="include $PLUSHU_ROOT/nginx/nginx.conf;"
copy_nginx_conf () {
local http_depth=0
local quote=
# File we save existing http block content to
local existing_save=$PLUSHU_ROOT/nginx/http/existing.conf.save
local http_blocks=0
# Parsing routine for lines when inside the http block
inside_http_block () {
# For each character on this line
for (( i=0; i<${#1}; i++ )); do
# Skip any backslash-escaped character
if [[ "${1:$i:1}" == "\\" ]]; then
i=$((i+1))
# If we are within a string
elif [[ -n "$quote" ]]; then
# If this is the closing quote
if [[ "${1:$i:1}" == "$quote" ]]; then
# Clear the open-quote marker
quote=
fi
# If we are not currently in a string
else
case "${1:$i:1}" in
# Increase our block depth if it's an open-brace
'{')
http_depth=$((http_depth+1))
;;
# Reduce our block depth if it's a close brace
'}')
http_depth=$((http_depth-1))
# Stop here if we've exited the http block
if (( $http_depth == 0 )); then
break
fi
;;
# Enter string if it's a quote
'"'|"'")
quote="${1:$i:1}"
;;
# Stop parsing if it's a comment
'#')
break
;;
esac
fi
done
# If we exited the http block
if (( $http_depth == 0 )); then
# Write everything before this brace to the existing http save
printf '%s\n' "${1:0:i}" >>"$existing_save"
# Parse everything after this brace as a line outside of the block
outside_http_block "${1:$((i+1))}"
# Use a different filename for the next http block, if any
existing_save=$PLUSHU_ROOT/nginx/http/existing.$((http_blocks)).conf.save
# If this whole line was within the http block
else
# Write the line to the existing http save
printf '%s\n' "$1" >>"$existing_save"
fi
}
# Parsing routine for lines when outside the http block
outside_http_block() {
# If this line enters the http block
if grep -q 'http *{' <<<"$1"; then
# Write everything before the start of the block to the config
sed 's/http *{.*//' <<<"$1" >>"$PLUSHU_ROOT/nginx/nginx.conf"
# Increment the http block depth and number of blocks
http_depth=$((http_depth+1))
http_blocks=$((http_blocks+1))
# Continue parsing within the http block
inside_http_block `sed 's/^.*http *{//g' <<<"$1"`
else
# Write all directives outside the http block to the config
printf '%s\n' "$1" >>"$PLUSHU_ROOT/nginx/nginx.conf"
fi
}
# Parse every line of the existing /etc/nginx/nginx.conf
while read -r line; do
if (( $http_depth > 0 )); then
inside_http_block "$line"
else
outside_http_block "$line"
fi
done </etc/nginx/nginx.conf
# Add an http block to our new conf
printf '%s\n' "http { include http/*.conf; }" \
>>"$PLUSHU_ROOT/nginx/nginx.conf"
}
setup_nginx_conf () {
mkdir -p "$PLUSHU_ROOT/nginx/http"
if [[ "$EUID" == 0 ]]; then
if [[ `readlink /etc/nginx` != "$PLUSHU_ROOT/nginx" &&
! -e /etc/nginx.old ]]; then
if [[ ! -f "$PLUSHU_ROOT/nginx/nginx.conf" ]]; then
echo "Copying existing Nginx configuration..."
copy_nginx_conf
fi
# Replace the existing /etc/nginx
echo "Replacing existing Nginx configuration..."
mv /etc/nginx /etc/nginx.old
ln -s "$PLUSHU_ROOT/nginx" /etc/nginx
mkdir -p "$PLUSHU_ROOT/nginx/ssl"
chown "$PLUSHU_USER:" "$PLUSHU_ROOT/nginx/ssl"
if [[ ! -e "$PLUSHU_ROOT/nginx/ssl" ]]; then
echo "Symlinking SSL dir into Nginx configuration..."
ln -s "$PLUSHU_ROOT/ssl" "$PLUSHU_ROOT/nginx/ssl"
fi
fi
else
echo "plushu-nginx moves the Nginx configuration to the Plushu root so"
echo "Plushu plugins can configure Nginx. Reinstall this plugin as root to"
echo "perform this replacement."
echo
echo "If you wish to integrate Plushu into your existing Nginx setup"
echo "rather than having Plushu control it, you'll need to figure out a"
echo "way to include $PLUSHU_ROOT/nginx/http/*.conf in your configuration,"
echo "keeping in mind Plushu uses relative paths in its Nginx conf files."
fi
}
# if Nginx is present
if command -v nginx >/dev/null 2>&1; then
echo "Nginx appears to be installed... good."
check_nginx_run_status
check_for_sudo
setup_nginx_conf
else
echo "Nginx does not appear to be installed."
echo_install_information
fi