-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
592 lines (498 loc) · 17.3 KB
/
Copy pathinstall.sh
File metadata and controls
592 lines (498 loc) · 17.3 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#!/bin/bash
# ============================================================================
# nginx-lscache-module installer
#
# Detects installed nginx, downloads matching source, builds the module,
# installs it, and adds load_module to nginx.conf.
#
# Usage:
# sudo ./install.sh # auto-detect nginx version
# sudo ./install.sh 1.30.1 # force specific version
# ./install.sh --check # check if module is installed
# ./install.sh --uninstall # remove module
# ============================================================================
set -e
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BOLD='\033[1m'
NC='\033[0m'
MODULE_NAME="ngx_http_lscache_module"
MODULE_SO="${MODULE_NAME}.so"
MODULE_DIR="$(cd "$(dirname "$0")" && pwd)"
# Common nginx module paths
MODULES_PATHS=(
"/usr/lib/nginx/modules"
"/usr/lib64/nginx/modules"
"/usr/local/nginx/modules"
"/etc/nginx/modules"
"/opt/nginx/modules"
)
# Common nginx.conf paths
CONF_PATHS=(
"/etc/nginx/nginx.conf"
"/usr/local/nginx/conf/nginx.conf"
"/opt/nginx/conf/nginx.conf"
)
log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_step() { echo -e "${CYAN}[STEP]${NC} ${BOLD}$1${NC}"; }
# ============================================================================
# Detect system
# ============================================================================
detect_distro() {
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO="$ID"
DISTRO_VERSION="$VERSION_ID"
elif [ -f /etc/redhat-release ]; then
DISTRO="rhel"
elif [ -f /etc/debian_version ]; then
DISTRO="debian"
else
DISTRO="unknown"
fi
}
detect_nginx() {
NGINX_BIN=""
NGINX_VERSION=""
NGINX_MODULES_DIR=""
NGINX_CONF=""
NGINX_PREFIX=""
# find nginx binary
if command -v nginx &>/dev/null; then
NGINX_BIN="$(command -v nginx)"
elif [ -x /usr/sbin/nginx ]; then
NGINX_BIN="/usr/sbin/nginx"
elif [ -x /usr/local/nginx/sbin/nginx ]; then
NGINX_BIN="/usr/local/nginx/sbin/nginx"
fi
if [ -z "$NGINX_BIN" ]; then
return 1
fi
# get version
NGINX_VERSION=$($NGINX_BIN -v 2>&1 | grep -oP '\d+\.\d+\.\d+')
if [ -z "$NGINX_VERSION" ]; then
return 1
fi
# get prefix and modules path from nginx -V
local nginx_v
nginx_v=$($NGINX_BIN -V 2>&1)
NGINX_PREFIX=$(echo "$nginx_v" | grep -oP '(?<=--prefix=)\S+' | head -1)
NGINX_PREFIX=${NGINX_PREFIX:-/etc/nginx}
# detect modules directory
local modules_path
modules_path=$(echo "$nginx_v" | grep -oP '(?<=--modules-path=)\S+' | head -1)
if [ -n "$modules_path" ] && [ -d "$modules_path" ]; then
NGINX_MODULES_DIR="$modules_path"
else
for p in "${MODULES_PATHS[@]}"; do
if [ -d "$p" ]; then
NGINX_MODULES_DIR="$p"
break
fi
done
fi
# detect config
local conf_path
conf_path=$(echo "$nginx_v" | grep -oP '(?<=--conf-path=)\S+' | head -1)
if [ -n "$conf_path" ] && [ -f "$conf_path" ]; then
NGINX_CONF="$conf_path"
else
for p in "${CONF_PATHS[@]}"; do
if [ -f "$p" ]; then
NGINX_CONF="$p"
break
fi
done
fi
return 0
}
# ============================================================================
# Install build dependencies
# ============================================================================
install_deps() {
log_step "Installing build dependencies..."
detect_distro
case "$DISTRO" in
ubuntu|debian|linuxmint|pop)
apt-get update -qq
apt-get install -y -qq build-essential libpcre3-dev libpcre2-dev \
zlib1g-dev libssl-dev wget ca-certificates 2>/dev/null
;;
centos|rhel|rocky|almalinux|fedora|ol)
if command -v dnf &>/dev/null; then
dnf install -y gcc make pcre-devel pcre2-devel zlib-devel \
openssl-devel wget 2>/dev/null
else
yum install -y gcc make pcre-devel pcre2-devel zlib-devel \
openssl-devel wget 2>/dev/null
fi
;;
arch|manjaro)
pacman -S --needed --noconfirm base-devel pcre pcre2 zlib openssl wget
;;
alpine)
apk add --no-cache build-base pcre-dev pcre2-dev zlib-dev \
openssl-dev wget linux-headers
;;
*)
log_warn "Unknown distro '$DISTRO'. Please install manually:"
log_warn " gcc, make, pcre-dev, zlib-dev, openssl-dev, wget"
return 1
;;
esac
log_info "Dependencies installed."
}
# ============================================================================
# Install nginx from official repo
# ============================================================================
install_nginx() {
detect_distro
echo ""
log_step "Installing nginx from official repository..."
echo ""
case "$DISTRO" in
ubuntu|debian|linuxmint|pop)
apt-get install -y -qq curl gnupg2 ca-certificates lsb-release 2>/dev/null
if [ ! -f /etc/apt/sources.list.d/nginx.list ]; then
curl -fsSL https://nginx.org/keys/nginx_signing.key \
| gpg --dearmor -o /usr/share/keyrings/nginx-archive-keyring.gpg 2>/dev/null
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/${DISTRO} $(lsb_release -cs) nginx" \
> /etc/apt/sources.list.d/nginx.list
fi
apt-get update -qq
apt-get install -y nginx
;;
centos|rhel|rocky|almalinux|ol)
cat > /etc/yum.repos.d/nginx.repo << 'REPO'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
REPO
if command -v dnf &>/dev/null; then
dnf install -y nginx
else
yum install -y nginx
fi
;;
fedora)
dnf install -y nginx
;;
arch|manjaro)
pacman -S --needed --noconfirm nginx
;;
alpine)
apk add --no-cache nginx
;;
*)
log_error "Cannot auto-install nginx on '$DISTRO'."
log_warn "Please install nginx manually, then re-run this script."
return 1
;;
esac
log_info "nginx installed."
}
# ============================================================================
# Build module
# ============================================================================
build_module() {
local version="$1"
local build_dir="/tmp/nginx-lscache-build"
log_step "Building module for nginx $version..."
rm -rf "$build_dir"
mkdir -p "$build_dir"
cd "$build_dir"
local url="https://nginx.org/download/nginx-${version}.tar.gz"
log_info "Downloading nginx source: $url"
if ! wget -q "$url" -O nginx.tar.gz; then
log_error "Failed to download nginx $version source."
log_warn "Check version at: https://nginx.org/en/download.html"
return 1
fi
tar xzf nginx.tar.gz
cd "nginx-${version}"
# Get configure arguments from installed nginx
local configure_args=""
if [ -n "$NGINX_BIN" ]; then
configure_args=$($NGINX_BIN -V 2>&1 | grep "configure arguments:" \
| sed 's/configure arguments: //' \
| sed 's/--add-dynamic-module=[^ ]* //g' \
| sed 's/--add-module=[^ ]* //g')
fi
log_info "Configuring..."
if [ -n "$configure_args" ]; then
eval ./configure $configure_args \
--add-dynamic-module="$MODULE_DIR" 2>&1 | tail -3
else
./configure --with-compat \
--add-dynamic-module="$MODULE_DIR" 2>&1 | tail -3
fi
log_info "Compiling module..."
make -j"$(nproc)" modules 2>&1 | tail -5
if [ ! -f "objs/${MODULE_SO}" ]; then
log_error "Build failed - ${MODULE_SO} not found."
return 1
fi
BUILT_MODULE="$build_dir/nginx-${version}/objs/${MODULE_SO}"
log_info "Build successful: $BUILT_MODULE"
}
# ============================================================================
# Install module
# ============================================================================
install_module() {
log_step "Installing module..."
if [ -z "$NGINX_MODULES_DIR" ]; then
NGINX_MODULES_DIR="/usr/lib/nginx/modules"
fi
mkdir -p "$NGINX_MODULES_DIR"
if [ -f "${NGINX_MODULES_DIR}/${MODULE_SO}" ]; then
local backup="${NGINX_MODULES_DIR}/${MODULE_SO}.backup.$(date +%s)"
cp "${NGINX_MODULES_DIR}/${MODULE_SO}" "$backup"
log_info "Existing module backed up to: $backup"
fi
cp "$BUILT_MODULE" "${NGINX_MODULES_DIR}/${MODULE_SO}"
chmod 644 "${NGINX_MODULES_DIR}/${MODULE_SO}"
log_info "Module installed to: ${NGINX_MODULES_DIR}/${MODULE_SO}"
if [ -n "$NGINX_CONF" ] && [ -f "$NGINX_CONF" ]; then
if ! grep -q "$MODULE_SO" "$NGINX_CONF"; then
log_info "Adding load_module to $NGINX_CONF..."
local module_path
if [[ "$NGINX_MODULES_DIR" == */modules ]]; then
module_path="modules/${MODULE_SO}"
else
module_path="${NGINX_MODULES_DIR}/${MODULE_SO}"
fi
sed -i "1i load_module ${module_path};" "$NGINX_CONF"
log_info "Added: load_module ${module_path};"
else
log_info "load_module already present in $NGINX_CONF"
fi
fi
}
# ============================================================================
# Verify installation
# ============================================================================
verify_install() {
log_step "Verifying installation..."
if ! $NGINX_BIN -t 2>&1; then
log_error "nginx config test failed!"
log_warn "Check $NGINX_CONF for errors."
log_warn "You may need to add 'lscache on;' to a location block,"
log_warn "and 'lscache_path /var/cache/nginx/lscache;' at http level."
return 1
fi
log_info "nginx config test passed."
echo ""
echo -e "${GREEN}${BOLD}=============================================="
echo -e " Installation complete!"
echo -e "==============================================${NC}"
echo ""
echo -e " Module: ${BOLD}${NGINX_MODULES_DIR}/${MODULE_SO}${NC}"
echo -e " nginx: ${BOLD}${NGINX_BIN} (${NGINX_VERSION})${NC}"
echo -e " Config: ${BOLD}${NGINX_CONF}${NC}"
echo ""
echo -e " ${CYAN}Next steps:${NC}"
echo " 1. Create the cache root (worker user must own it):"
echo ""
echo " sudo mkdir -p /var/cache/nginx/lscache"
echo " sudo chown -R www-data:www-data /var/cache/nginx/lscache"
echo ""
echo " 2. Add to http {} in $NGINX_CONF:"
echo ""
echo " lscache_path /var/cache/nginx/lscache;"
echo " lscache_max_size 1g;"
echo " lscache_purge_allowed 127.0.0.1 ::1;"
echo ""
echo " 3. Add to your server / location block:"
echo ""
echo " location / {"
echo " lscache on;"
echo " fastcgi_pass unix:/run/php/php-fpm.sock;"
echo " }"
echo ""
echo " 4. Reload nginx:"
echo " sudo nginx -s reload"
echo ""
}
# ============================================================================
# Check / Status
# ============================================================================
check_status() {
echo ""
echo -e "${BOLD}nginx-lscache-module status${NC}"
echo "=============================="
if ! detect_nginx; then
echo -e " nginx: ${RED}not found${NC}"
echo ""
return 1
fi
echo -e " nginx: ${GREEN}${NGINX_VERSION}${NC} (${NGINX_BIN})"
local found=0
for p in "${MODULES_PATHS[@]}"; do
if [ -f "$p/$MODULE_SO" ]; then
echo -e " module: ${GREEN}installed${NC} ($p/$MODULE_SO)"
found=1
break
fi
done
if [ $found -eq 0 ]; then
echo -e " module: ${RED}not installed${NC}"
fi
if [ -n "$NGINX_CONF" ]; then
if grep -q "$MODULE_SO" "$NGINX_CONF" 2>/dev/null; then
echo -e " config: ${GREEN}loaded${NC} (in $NGINX_CONF)"
else
echo -e " config: ${YELLOW}not loaded${NC} (load_module not in $NGINX_CONF)"
fi
fi
local lscache_on
lscache_on=$(grep -r "lscache on" /etc/nginx/ 2>/dev/null | head -1)
if [ -n "$lscache_on" ]; then
echo -e " active: ${GREEN}yes${NC}"
else
echo -e " active: ${YELLOW}not configured${NC} (add 'lscache on;' to a location block)"
fi
echo ""
return 0
}
# ============================================================================
# Uninstall
# ============================================================================
uninstall_module() {
echo ""
log_step "Uninstalling nginx-lscache-module..."
if [ "$(id -u)" -ne 0 ]; then
log_error "Please run as root: sudo $0 --uninstall"
exit 1
fi
detect_nginx
for p in "${MODULES_PATHS[@]}"; do
if [ -f "$p/$MODULE_SO" ]; then
rm -f "$p/$MODULE_SO"
rm -f "$p/${MODULE_SO}.backup."* 2>/dev/null
log_info "Removed: $p/$MODULE_SO"
fi
done
if [ -n "$NGINX_CONF" ] && [ -f "$NGINX_CONF" ]; then
if grep -q "$MODULE_SO" "$NGINX_CONF"; then
sed -i "/$MODULE_SO/d" "$NGINX_CONF"
log_info "Removed load_module from $NGINX_CONF"
fi
fi
if [ -d /etc/nginx ]; then
local files
files=$(grep -rl "lscache" /etc/nginx/ 2>/dev/null || true)
if [ -n "$files" ]; then
log_warn "These files still contain 'lscache' directives:"
echo "$files" | while read -r f; do
echo " $f"
done
log_warn "Remove 'lscache on;' / 'lscache_path' / etc. manually."
fi
fi
if [ -n "$NGINX_BIN" ]; then
if $NGINX_BIN -t 2>&1; then
log_info "nginx config OK. Reload with: sudo nginx -s reload"
fi
fi
log_info "Uninstall complete."
echo ""
}
# ============================================================================
# Cleanup
# ============================================================================
cleanup() {
rm -rf /tmp/nginx-lscache-build 2>/dev/null
}
# ============================================================================
# Main
# ============================================================================
main() {
echo ""
echo -e "${BOLD}=============================================="
echo -e " nginx-lscache-module installer"
echo -e "==============================================${NC}"
echo ""
case "${1:-}" in
--check|--status|-c)
check_status
exit $?
;;
--uninstall|--remove|-u)
uninstall_module
exit 0
;;
--help|-h)
echo "Usage: $0 [OPTIONS] [NGINX_VERSION]"
echo ""
echo "Options:"
echo " (no args) Auto-detect nginx and install module"
echo " VERSION Build for specific nginx version (e.g., 1.30.1)"
echo " --check, -c Check installation status"
echo " --uninstall Remove module"
echo " --help, -h Show this help"
echo ""
exit 0
;;
esac
if [ "$(id -u)" -ne 0 ]; then
log_error "Please run as root: sudo $0"
exit 1
fi
if [ "$(uname)" != "Linux" ]; then
log_error "This module only supports Linux."
exit 1
fi
if detect_nginx; then
log_info "Found nginx ${NGINX_VERSION} at ${NGINX_BIN}"
else
log_warn "nginx is not installed."
echo ""
read -rp "Install nginx from official repository? [y/N] " answer
if [[ "$answer" =~ ^[Yy] ]]; then
install_deps
install_nginx
if ! detect_nginx; then
log_error "nginx installation failed."
exit 1
fi
log_info "Found nginx ${NGINX_VERSION} at ${NGINX_BIN}"
else
log_error "nginx is required. Install it and re-run this script."
exit 1
fi
fi
local version="${1:-$NGINX_VERSION}"
if [ -z "$version" ]; then
log_error "Could not determine nginx version."
echo "Usage: $0 [VERSION]"
exit 1
fi
if [ -n "$1" ] && [ "$1" != "$NGINX_VERSION" ]; then
log_warn "Building for nginx $1, but installed version is $NGINX_VERSION"
read -rp "Continue anyway? [y/N] " answer
[[ "$answer" =~ ^[Yy] ]] || exit 0
fi
if [ ! -f "$MODULE_DIR/config" ]; then
log_error "Module source not found at: $MODULE_DIR"
log_error "Run this script from the module directory."
exit 1
fi
install_deps
build_module "$version" || exit 1
install_module
verify_install
cleanup
echo -e "${GREEN}Done!${NC}"
echo ""
}
trap cleanup EXIT
main "$@"