-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile
More file actions
executable file
·483 lines (383 loc) · 14.6 KB
/
compile
File metadata and controls
executable file
·483 lines (383 loc) · 14.6 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/bin/sh
# ------------------------------------------------------------------------------
# ERROR CODES
# ------------------------------------------------------------------------------
readonly EX_SUCCESS=0
readonly EX_CATCHALL=1
readonly EX_MISUSE=2
readonly EX_USAGE=64
readonly EX_DATAERR=65
readonly EX_NOINPUT=66
readonly EX_NOUSER=67
readonly EX_NOHOST=68
readonly EX_UNAVAILABLE=69
readonly EX_SOFTWARE=70
readonly EX_OSERR=71
readonly EX_OSFILE=72
readonly EX_CANTCREAT=73
readonly EX_IOERR=74
readonly EX_TEMPFAIL=75
readonly EX_PROTOCOL=76
readonly EX_NOPERM=77
readonly EX_CONFIG=78
readonly EX_NGX_DL_FAILED=79
readonly EX_PCRE_DL_FAILED=80
readonly EX_OPENSSL_DL_FAILED=81
readonly EX_NALM_DL_FAILED=82
readonly EX_GPSNM_DL_FAILED=83
# ------------------------------------------------------------------------------
# INTERNAL VARIABLES (DO NOT TOUCH)
# ------------------------------------------------------------------------------
# Name of this script.
readonly __FILENAME__="${0##*/}"
# Directory where this script resides (not necessarily the same as CWD).
readonly __DIRNAME__="$(cd -- "$(dirname -- "${0}")"; pwd)"
# Used to speed-up compilation time.
readonly CPU_COUNT=$(lscpu -aeCORE | wc -l)
# Used to collect additional modules that should be added to nginx.
ADD_MODULES=''
# ------------------------------------------------------------------------------
# FUNCTIONS
# ------------------------------------------------------------------------------
# Add additional module to nginx.
#
# ARGS:
# $1 - The name of the directory within the source directory.
add_module()
{
log_info "Adding nginx module ${YELLOW}${1}${NORMAL} ..."
ADD_MODULES="${ADD_MODULES}--add-module=${SOURCE_DIRECTORY}/${1} "
return 0
}
# Create directory and set nginx owner.
#
# ARGS:
# $1 - Absolute path to the directory.
create_directory()
{
log_info "Creating directory ${YELLOW}${1}${NORMAL} ..."
mkdir --parents -- "${1}" || return 1
chmod -- 0755 "${1}" || return 1
chown -- "${USER}":"${GROUP}" "${1}" || return 1
return 0
}
# Exit script with an error code.
#
# ARGS:
# $1 - Desriptive human readable error message.
# $2 - Exit code.
# RETURNS:
# ALWAYS exits the script with the code provided as second argument.
die()
{
log_failure "${1}"
exit ${2}
}
# Download and extract given compressed, versioned tar archive.
#
# ARGS:
# $1 - The URL to the file on the remote server.
# $2 - The name of the file.
# $3 - The version string.
download_and_extract()
{
local SOURCE_PATH="${SOURCE_DIRECTORY}/${2}"
local SOURCE_PATH_VERSIONED="${SOURCE_PATH}-${3}"
# Use existing source files if version matches.
[ -d "${SOURCE_PATH_VERSIONED}" ] && return 0
log_info "Downloading ${YELLOW}${2} ${3}${NORMAL} ..."
local ARCHIVE_FILENAME="${2}-${3}.tar.gz"
local ARCHIVE_PATH="${SOURCE_DIRECTORY}/${ARCHIVE_FILENAME}"
# Delete any left overs of old versions (note the star at the end).
rm --force --recursive -- "${SOURCE_PATH}"* || return 1
# Download, extract, and delete source archive.
wget --output-document="${ARCHIVE_PATH}" -- "${1}${ARCHIVE_FILENAME}" || return 1
tar --extract --file="${ARCHIVE_PATH}" || return 1
rm --force -- "${ARCHIVE_PATH}" || return 1
# Make sure files belong to root and create a symbolic link without the
# version number for unified access to the source files.
chown --recursive -- root:root "${SOURCE_PATH_VERSIONED}" || return 1
ln --symbolic -- "${SOURCE_PATH_VERSIONED}" "${SOURCE_PATH}" || return 1
return 0
}
# Clone git repository (or pull changes if it already exists).
#
# ARGS:
# $1 - User name
# $2 - Repository name
git_clone()
{
local REPOSITORY_PATH="${SOURCE_DIRECTORY}/${2}"
if [ -d "${REPOSITORY_PATH}/.git" ]
then
log_info "Updating ${YELLOW}${2}${NORMAL} via git ..."
git -C "${REPOSITORY_PATH}" pull || return 1
else
log_info "Downloading ${YELLOW}${2}${NORMAL} via git ..."
rm --recursive --force -- "${REPOSITORY_PATH}" || return 1
git clone "https://github.com/${1}/${2}.git" "${REPOSITORY_PATH}" || return 1
fi
return 0
}
# Log message.
#
# ARGS:
# $1 - Message.
log()
{
printf -- '%s\n' "${1}"
}
# Log failure message but do not exit.
#
# ARGS:
# $1 - Failure message.
log_failure()
{
printf -- '[%sfail%s] %s\n' "${RED}" "${NORMAL}" "${1}" >&2
}
# Log informational message.
#
# ARGS:
# $1 - Informational message
log_info()
{
printf -- '[%sinfo%s] %s\n' "${YELLOW}" "${NORMAL}" "${1}"
}
# Log success message.
#
# ARGS:
# $1 - Success message.
log_ok()
{
printf -- '[%s ok %s] %s\n' "${GREEN}" "${NORMAL}" "${1}"
}
# Log to do message.
#
# ARGS:
# $1 - To do message.
log_todo()
{
printf -- '[%stodo%s] %s\n' "${BLUE}" "${NORMAL}" "${1}" >&2
}
# Print usage text.
usage()
{
cat << EOT
Usage: ${__FILENAME__} [OPTION]... [TLS_LIBRARY_NAME]
Compile and install nginx from source.
-h -? Display this help and exit.
Report bugs to richard@fussenegger.info
GitHub repository: https://github.com/Fleshgrinder/nginx-compile
For complete documentation, see: README.md
EOT
}
# ------------------------------------------------------------------------------
# BOOTSTRAP
# ------------------------------------------------------------------------------
if [ $(id -u) -ne 0 ]
then die 'Super user only!' ${EX_NOPERM}
fi
# Check for possibly passed options.
while getopts 'h' OPT
do
case "${OPT}" in
h|[?]) usage && exit 0 ;;
*) usage >2 && exit ${EX_USAGE} ;;
esac
# We have to remove found options from the input for later evaluations of
# passed arguments in subscripts that are not interested in these options.
shift $(( $OPTIND - 1 ))
done
# Remove possibly passed end of options marker.
if [ "${1}" = '--' ]
then shift $(( $OPTIND - 1 ))
fi
# Load the configuration.
. "${__DIRNAME__}/config.sh"
log 'Starting nginx compilation, this might take several minutes ...'
# ------------------------------------------------------------------------------
# PREPARE DEPENDENCIES AND SOURCE FILES
# ------------------------------------------------------------------------------
log_info 'Installing dependencies ...'
if command -v apt-get 1>&-
then apt-get --yes -- install build-essential git || die 'Installing dependencies failed.' ${EX_UNAVAILABLE}
fi
cd -- "${SOURCE_DIRECTORY}" || die "Could not change to source directory '${SOURCE_DIRECTORY}', check configuration." ${EX_CONFIG}
if [ ${NGINX_VERSION} = false ]
then
git_clone nginx nginx || die 'Could not download nginx sources.' ${EX_NGX_DL_FAILED}
else
download_and_extract 'http://nginx.org/download/' 'nginx' "${NGINX_VERSION}" || die 'Could not download nginx sources.' ${EX_NGX_DL_FAILED}
fi
download_and_extract 'ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/' 'pcre' "${PCRE_VERSION}" || die 'Could not download PCRE sources.' ${EX_PCRE_DL_FAILED}
git_clone madler zlib || die 'Could not download ZLIB sources.' ${EC_DL_ZLIB_FAIL}
case "${TLS_LIBRARY_NAME}" in
openssl)
download_and_extract 'http://www.openssl.org/source/' 'openssl' "${TLS_LIBRARY_VERSION}" || die 'Could not download OpenSSL sources.' ${EX_OPENSSL_DL_FAILED}
;;
boringssl|libressl)
log_todo "Implement ${TLS_LIBRARY_NAME} support, please open an issue if you are interested in this."
exit ${EX_SOFTWARE}
;;
*)
die "Unsupported TLS library '${RED}${TLS_LIBRARY_NAME}${NORMAL}' specified, check configuration." ${EX_USAGE}
;;
esac
if [ ${ACCEPT_LANGUAGE} = true ]
then git_clone Fleshgrinder nginx_accept_language_module || die 'Could not download nginx-accept-language-module sources.' ${EX_NALM_DL_FAILED}
fi
if [ ${GOOGLE_PAGESPEED} = true ]
then
readonly PSOL="${SOURCE_DIRECTORY}/ngx_pagespeed/psol"
readonly PSOL_ARCHIVE="${SOURCE_DIRECTORY}/ngx_pagespeed/${GOOGLE_PAGESPEED_VERSION}.tar.gz"
git_clone pagespeed ngx_pagespeed || die 'Could not download Google PageSpeed module sources.' ${EX_GPSNM_DL_FAILED}
if [ !-d "${PSOL}" ]
then
log_info "Downloading Google ${BLUE}PSOL ${GOOGLE_PAGESPEED_VERSION}${NORMAL} ..."
wget -- "https://dl.google.com/dl/page-speed/psol/${GOOGLE_PAGESPEED_VERSION}.tar.gz" || die 'Could not download PSOL sources.' ${EC_DL_PSOL_FAIL}
tar --extract --file="${PSOL_ARCHIVE}" || exit ${EX_UNAVAILABLE}
rm --force -- "${PSOL_ARCHIVE}" || exit ${EX_IOERR}
chown --recursive -- root:root "${PSOL}" || exit ${EX_NOPERM}
chmod --recursive -- 0755 "${PSOL}" || exit ${EX_NOPERM}
find "${PSOL}" -type f -exec chmod 644 {} \; || exit ${EX_NOPERM}
fi
fi
# ------------------------------------------------------------------------------
# CONFIGURE & COMPILE
# ------------------------------------------------------------------------------
[ -d "${NGINX_CONF_PATH}" ] && mv "${NGINX_CONF_PATH}" ~/nginx-configuration
cd -- "${SOURCE_DIRECTORY}/nginx"
[ ! -f configure ] && [ -f auto/configure ] && cp auto/configure configure
[ ! -f configure ] && die 'Configure file missing.' ${EX_IOERR}
./configure \
--user="${USER}" \
--group="${GROUP}" \
--prefix="${NGINX_PREFIX}" \
--sbin-path="${NGINX_SBIN_PATH}" \
--conf-path="${NGINX_CONF_PATH}/nginx.${NGINX_CONF_SUFFIX}" \
--pid-path="${NGINX_PID_PATH}" \
--lock-path="${NGINX_LOCK_PATH}" \
--error-log-path="${NGINX_LOG_PATH}/error.log" \
--http-client-body-temp-path="${NGINX_TMP_PATH}/nginx_client_body" \
--http-fastcgi-temp-path="${NGINX_TMP_PATH}/nginx_fastcgi" \
--http-log-path="${NGINX_LOG_PATH}/access.log" \
--with-cc-opt="${NGINX_CFLAGS}" \
--with-ld-opt="${NGINX_LDFLAGS}" \
--with-file-aio \
--with-ipv6 \
--with-http_gzip_static_module \
--with-http_ssl_module \
--with-http_v2_module \
--with-openssl-opt="${TLS_LIBRARY_OPTIONS}" \
--with-openssl="${SOURCE_DIRECTORY}/${TLS_LIBRARY_NAME}" \
--with-md5="${SOURCE_DIRECTORY}/${TLS_LIBRARY_NAME}" \
--with-md5-asm \
--with-sha1="${SOURCE_DIRECTORY}/${TLS_LIBRARY_NAME}" \
--with-sha1-asm \
--with-pcre="${SOURCE_DIRECTORY}/pcre" \
--with-pcre-jit \
--with-threads \
--with-zlib="${SOURCE_DIRECTORY}/zlib" \
${ADD_MODULES} \
--without-http_access_module \
--without-http_auth_basic_module \
--without-http_autoindex_module \
--without-http_empty_gif_module \
--without-http_geo_module \
--without-http_memcached_module \
--without-http_proxy_module \
--without-http_referer_module \
--without-http_scgi_module \
--without-http_split_clients_module \
--without-http_ssi_module \
--without-http_upstream_ip_hash_module \
--without-http_userid_module \
--without-http_uwsgi_module \
|| die 'Could not configure nginx.' ${EX_CONFIG}
# Compile configured nginx.
make -j ${CPU_COUNT} || die 'Could not compile nginx.' ${EX_CONFIG}
# Restore existing nginx configuration.
[ -d "${NGINX_CONF_PATH}" ] && mv "${NGINX_CONF_PATH}" "${NGINX_CONF_PATH}/dist"
[ -d ~/nginx-configuration ] && mv ~/nginx-configuration "${NGINX_CONF_PATH}"
# ------------------------------------------------------------------------------
# INSTALL
# ------------------------------------------------------------------------------
# Install SysVinit script if applicable.
if [ ${NGINX_INITD} = true ] && [ ! -f "${INITD_PATH}" ]
then
cd "${SOURCE_DIRECTORY}"
if [ -d nginx-sysvinit-script ]
then git -C nginx-sysvinit-script pull
else git clone https://github.com/Fleshgrinder/nginx-sysvinit-script.git
fi
cd nginx-sysvinit-script
make
fi
# Check if an nginx is already installed.
if command -v nginx 1>&-
then
# We'll only try an on the fly upgrade if the PID locations of the old and
# new nginx are the same. Otherwise the SysVinit script that we provide will
# most certainly not pick the PID file up and other edge cases may arise.
if [ -f "${NGINX_PID_PATH}" ]
then readonly OLDBIN_PID_PATH="${NGINX_PID_PATH}.oldbin"
else
# Could be it is not running right now.
log_info 'Could not find PID file of old nginx installation.'
fi
if [ ${NGINX_ONTHEFLY_UPGRADE} = true ] && [ -n "${OLDBIN_PID_PATH}" ]
then
# Move the old executable to a new place and avoid text file busy errors.
mv "${OLDBIN}" "${OLDBIN}.oldbin" || die 'Could not move old nginx.' ${EX_NOPERM}
OLDBIN="${OLDBIN}.oldbin"
# Now we can install the new nginx.
make -j ${CPU_COUNT} install || die 'Could not install nginx.' ${EX_NOPERM}
# Retrieve the actual PID of the old binary.
readonly OLDBIN_PID=$(cat "${NGINX_PID_PATH}")
# Next we need to send the USR2 signal to the old nginx installation. For
# more details please refer to: http://nginx.org/en/docs/control.html#upgrade
kill -12 "${OLDBIN_PID}" || die 'Could not send USR2 signal to old nginx.' ${EX_NOPERM}
cat << EOT
[${GREEN} ok ${NORMAL}] Finished on-the-fly upgrade.
You now have two nginx instances up and running serving your websites.
If you are happy with your new nginx, execute the following script:
${GREEN}${__DIRNAME__}/quit-old.sh${NORMAL}*
If you want to roll back to your old nginx, execute the following script:
${GREEN}${__DIRNAME__}/rollback.sh${NORMAL}*
For more information please refer to the official documentation:
${YELLOW}http://nginx.org/en/docs/control.html#upgrade${NORMAL}
EOT
else
if [ -f "${INITD_PATH}" ] && service nginx status 2>/dev/null 1>/dev/null
then
# Old nginx is up and running but also has an init script, we have no
# clue where the PID file resides, let's give it a shot ...
log_info 'Attempting to stop old nginx process and install new one ...'
service nginx stop || die 'Could not stop old nginx.' ${EX_NOPERM}
make install || die 'Could not install nginx.' ${EX_NOPERM}
else
# Hopefully old nginx is simply not running, in which case this will
# work; other possibilities include PID file in non-standard directory,
# lingering daemon, and much more ...
log_info 'Attempting to install new nginx ...'
make install || die 'Could not install nginx.' ${EX_NOPERM}
fi
if [ -f "${INITD_PATH}" ]
then service nginx start
fi
fi
else
make -j ${CPU_COUNT} install || die 'Could not install nginx.' ${EX_NOPERM}
fi
# ------------------------------------------------------------------------------
# SUCCESS
# ------------------------------------------------------------------------------
cat << EOT
[${GREEN} ok ${NORMAL}] Succesfully installed nginx.
CONF: ${BLUE}${NGINX_CONF_PATH}${NORMAL}
BIN: ${GREEN}${NGINX_SBIN_PATH}/nginx${NORMAL}*
PID: ${BLUE}${NGINX_PID_PATH}${NORMAL}
You might want to check out ${YELLOW}https://github.com/Fleshgrinder/nginx-configuration${NORMAL}.
You also might want to delete the source files in ${BLUE}${SOURCE_DIRECTORY}${NORMAL}.
EOT
exit ${EX_SUCCESS}