-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasyinstall.shold11
More file actions
1325 lines (1191 loc) · 54.6 KB
/
Copy patheasyinstall.shold11
File metadata and controls
1325 lines (1191 loc) · 54.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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# ============================================
# EasyInstall WordPress Maximum Performance Installation Script v6.4
# HYBRID EDITION: Bash = Dependencies | Python = Configuration
# Ultra-Optimized WordPress Setup with Advanced Auto-Tuning (10 Phases)
# RAM Auto-Detection: 512MB to 16GB
# Compatible with Debian 12 and Ubuntu 24.04/22.04
#
# ARCHITECTURE:
# easyinstall.sh — Bash: all apt installs, system deps, repo setup,
# service start/enable, lock/logging, entry point
# easyinstall_config.py — Python: all server config file generation,
# WordPress setup, Nginx/PHP/MySQL/Redis config,
# autotune, firewall rules, monitoring scripts
# ============================================
set -eE
trap 'error_handler ${LINENO} "$BASH_COMMAND" $?' ERR
# ── Color Codes ─────────────────────────────────────────────────────────────
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'
# ── Global Variables ─────────────────────────────────────────────────────────
SCRIPT_VERSION="6.4"
LOCK_FILE="/var/run/easyinstall.lock"
LOG_FILE="/var/log/easyinstall/install.log"
ERROR_LOG="/var/log/easyinstall/error.log"
STATUS_FILE="/var/lib/easyinstall/install.status"
BACKUP_DIR="/root/easyinstall-backups/$(date +%Y%m%d-%H%M%S)"
USED_REDIS_PORTS_FILE="/var/lib/easyinstall/used_redis_ports.txt"
INSTALL_START_TIME=$(date +%s)
PYTHON_CONFIG_SCRIPT="/usr/local/lib/easyinstall_config.py"
# ============================================
# SECTION 1 — LOGGING (pure bash, no deps)
# ============================================
log() {
local level=$1
local message=$2
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
mkdir -p /var/log/easyinstall /var/lib/easyinstall 2>/dev/null || true
echo "[$timestamp] [$level] $message" >> "$LOG_FILE"
case $level in
"ERROR") echo -e "${RED}❌ $message${NC}" ;;
"WARNING") echo -e "${YELLOW}⚠️ $message${NC}" ;;
"SUCCESS") echo -e "${GREEN}✅ $message${NC}" ;;
"INFO") echo -e "${BLUE}ℹ️ $message${NC}" ;;
"STEP") echo -e "${PURPLE}🔷 $message${NC}" ;;
*) echo -e "$message" ;;
esac
}
log_error() {
local line=$1 command=$2 exit_code=$3
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
mkdir -p /var/log/easyinstall 2>/dev/null || true
echo "[$timestamp] [ERROR] Failed at line $line: $command (exit: $exit_code)" >> "$ERROR_LOG"
local i=0
while caller $i >> "$ERROR_LOG" 2>/dev/null; do ((i++)); done
log "ERROR" "Installation failed at line $line"
log "INFO" "Check error log: $ERROR_LOG"
}
error_handler() {
local line=$1 command=$2 exit_code=$3
log_error "$line" "$command" "$exit_code"
if [ -d "$BACKUP_DIR" ] && [ "$(ls -A $BACKUP_DIR 2>/dev/null)" ]; then
log "WARNING" "Attempting to rollback configuration changes..."
perform_rollback
fi
exit $exit_code
}
update_status() {
local step=$1 status=$2
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "$timestamp: $step - $status" >> "$STATUS_FILE"
}
# ============================================
# SECTION 2 — SAFE COMMAND EXECUTION
# ============================================
run_cmd() {
local cmd="$@"
log "INFO" "Running: $cmd"
if eval "$cmd"; then
log "SUCCESS" "Completed: ${cmd:0:60}..."
return 0
else
local exit_code=$?
log "ERROR" "Failed (code $exit_code): $cmd"
return $exit_code
fi
}
run_cmd_retry() {
local max_attempts=$1 delay=$2
local command="${@:3}"
local attempt=1
while [ $attempt -le $max_attempts ]; do
log "INFO" "Attempt $attempt/$max_attempts: ${command:0:60}..."
if eval "$command"; then
log "SUCCESS" "Succeeded on attempt $attempt"
return 0
fi
if [ $attempt -lt $max_attempts ]; then
log "WARNING" "Attempt $attempt failed. Retrying in ${delay}s..."
sleep $delay
delay=$((delay * 2))
fi
attempt=$((attempt + 1))
done
log "ERROR" "Failed after $max_attempts attempts: $command"
return 1
}
# reload_or_restart_service <service> [syntax_check_cmd]
# Reloads a service; if the reload fails, falls back to a full restart and
# logs a warning. If a syntax_check_cmd is supplied it's run first — on
# failure the reload/restart is skipped entirely so a known-bad config never
# gets pushed onto a running service (the previous config stays active).
reload_or_restart_service() {
local service="$1" syntax_check_cmd="${2:-}"
if [ -n "$syntax_check_cmd" ]; then
if ! eval "$syntax_check_cmd" >/dev/null 2>&1; then
log "ERROR" "$service: config syntax check failed ('$syntax_check_cmd') — skipping reload/restart"
return 1
fi
fi
if systemctl reload "$service" 2>/dev/null; then
return 0
fi
log "WARNING" "$service: 'systemctl reload' failed — falling back to 'systemctl restart'"
if systemctl restart "$service" 2>/dev/null; then
log "SUCCESS" "$service: restarted successfully after reload failure"
return 0
fi
log "ERROR" "$service: both reload and restart failed — service may be down, check manually"
return 1
}
# ============================================
# SECTION 3 — LOCK FILE MANAGEMENT
# ============================================
check_lock() {
log "STEP" "Checking for existing installation..."
if [ -f "$LOCK_FILE" ]; then
local pid=$(cat "$LOCK_FILE" 2>/dev/null)
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
log "ERROR" "Another installation already running (PID: $pid)"
exit 1
else
log "WARNING" "Removing stale lock file"
rm -f "$LOCK_FILE"
fi
fi
echo $$ > "$LOCK_FILE"
log "SUCCESS" "Lock acquired"
trap 'rm -f "$LOCK_FILE"; log "INFO" "Lock released"' EXIT
}
# ============================================
# SECTION 4 — CONFIGURATION BACKUP & ROLLBACK
# ============================================
backup_config() {
local files=("$@")
log "STEP" "Creating configuration backup"
mkdir -p "$BACKUP_DIR"
for file in "${files[@]}"; do
if [ -f "$file" ]; then
local dest_dir="$BACKUP_DIR$(dirname "$file")"
mkdir -p "$dest_dir"
cp -p "$file" "$dest_dir/"
log "SUCCESS" "Backed up: $file"
fi
done
cat > "$BACKUP_DIR/MANIFEST.txt" <<EOF
EasyInstall Backup
Date: $(date)
Version: $SCRIPT_VERSION
Files:
$(for f in "${files[@]}"; do echo " - $f"; done)
EOF
log "SUCCESS" "Backup created at: $BACKUP_DIR"
}
perform_rollback() {
log "STEP" "Performing configuration rollback"
[ -d "$BACKUP_DIR" ] || { log "ERROR" "No backup directory found"; return 1; }
find "$BACKUP_DIR" -type f -not -name "MANIFEST.txt" | while read -r backup_file; do
local original_file="${backup_file#$BACKUP_DIR}"
mkdir -p "$(dirname "$original_file")"
cp -p "$backup_file" "$original_file"
log "SUCCESS" "Restored: $original_file"
done
log "SUCCESS" "Rollback completed"
for service in nginx php8.3-fpm php8.2-fpm mariadb redis-server; do
systemctl restart "$service" 2>/dev/null || true
done
}
# ============================================
# SECTION 5 — SYSTEM VALIDATION (BASH)
# ============================================
check_root() {
log "STEP" "Checking root privileges"
[ "$EUID" -ne 0 ] && { log "ERROR" "Please run as root"; exit 1; }
log "SUCCESS" "Running as root"
}
check_network() {
log "STEP" "Checking network connectivity"
if ! host -W 5 google.com >/dev/null 2>&1; then
log "WARNING" "DNS resolution failed, trying alternative"
nslookup google.com 8.8.8.8 >/dev/null 2>&1 || { log "ERROR" "Network connectivity issue"; return 1; }
fi
if ! ping -c 1 -W 5 8.8.8.8 >/dev/null 2>&1; then
log "WARNING" "Ping failed, checking HTTP"
curl -s --head https://google.com | head -n 1 | grep -q "200" || { log "ERROR" "No internet connectivity"; return 1; }
fi
log "SUCCESS" "Network connectivity OK"
return 0
}
check_disk_space() {
local required_mb=${1:-5120}
log "STEP" "Checking disk space (required: ${required_mb}MB)"
local available_mb=$(df -m / | awk 'NR==2 {print $4}')
if [ "$available_mb" -lt "$required_mb" ]; then
log "ERROR" "Insufficient disk space. Need ${required_mb}MB, have ${available_mb}MB"
df -h / >> "$LOG_FILE"
return 1
fi
log "SUCCESS" "Disk space OK: ${available_mb}MB available"
return 0
}
check_memory() {
log "STEP" "Checking available memory"
local total_mem=$(free -m | awk '/Mem:/ {print $2}')
local available_mem=$(free -m | awk '/Mem:/ {print $7}')
log "INFO" "Total RAM: ${total_mem}MB, Available: ${available_mem}MB"
[ "$total_mem" -lt 512 ] && log "WARNING" "Low memory system (${total_mem}MB). Performance may be limited."
return 0
}
check_os_compatibility() {
log "STEP" "Checking OS compatibility"
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_ID=$ID; OS_NAME=$NAME; OS_VERSION=$VERSION_ID
fi
[[ ! "$OS_ID" =~ ^(debian|ubuntu)$ ]] && { log "ERROR" "Unsupported OS: $OS_NAME"; return 1; }
if [ "$OS_ID" = "debian" ] && [ "${OS_VERSION%%.*}" -lt 11 ]; then
log "ERROR" "Debian 11+ required (detected: $OS_VERSION)"; return 1
fi
if [ "$OS_ID" = "ubuntu" ] && [[ ! "$OS_VERSION" =~ ^(20.04|22.04|24.04)$ ]]; then
log "ERROR" "Ubuntu 20.04/22.04/24.04 required (detected: $OS_VERSION)"; return 1
fi
log "SUCCESS" "OS compatible: $OS_NAME $OS_VERSION"
return 0
}
# ============================================
# SECTION 6 — SERVICE HEALTH CHECKS (BASH)
# ============================================
wait_for_service() {
local service=$1 max_attempts=${2:-30} attempt=1
log "INFO" "Waiting for $service to start..."
while [ $attempt -le $max_attempts ]; do
systemctl is-active --quiet "$service" 2>/dev/null && {
log "SUCCESS" "$service is running"; return 0; }
[ $((attempt % 5)) -eq 0 ] && log "INFO" "Still waiting for $service... ($attempt/$max_attempts)"
sleep 2; attempt=$((attempt + 1))
done
log "ERROR" "$service failed to start within timeout"
journalctl -u "$service" --no-pager -n 50 >> "$ERROR_LOG" 2>/dev/null || true
systemctl status "$service" --no-pager >> "$ERROR_LOG" 2>/dev/null || true
return 1
}
test_mysql_connection() {
local attempt=1
log "INFO" "Testing MySQL connection..."
while [ $attempt -le 10 ]; do
mysql -e "SELECT 1" 2>/dev/null && { log "SUCCESS" "MySQL connection OK"; return 0; }
sleep 3; attempt=$((attempt + 1))
done
log "ERROR" "Cannot connect to MySQL"; return 1
}
test_redis() {
local port=${1:-6379}
redis-cli -p "$port" ping 2>/dev/null | grep -q "PONG" && {
log "SUCCESS" "Redis on port $port is healthy"; return 0; }
log "WARNING" "Redis on port $port not responding"; return 1
}
validate_nginx_config() {
nginx -t 2>/dev/null && { log "SUCCESS" "Nginx configuration valid"; return 0; }
log "ERROR" "Nginx configuration invalid"
nginx -t 2>&1 | head -20 >> "$ERROR_LOG"
return 1
}
# ============================================
# SECTION 7 — RAM AUTO-DETECT & TUNE (BASH)
# Exported as env vars consumed by Python config
# ============================================
detect_ram_and_tune() {
log "STEP" "Auto-tuning based on RAM"
TOTAL_RAM=$(free -m | awk '/Mem:/ {print $2}')
TOTAL_CORES=$(nproc)
# ── Defensive fallback ──────────────────────────────────────────────────
# `free`/`nproc` can occasionally return empty or non-numeric output
# (containers with restricted /proc, exotic init systems, etc). Under
# `set -eE` a numeric comparison against an empty/non-numeric value would
# abort the whole install via the ERR trap, so validate and fall back to
# a conservative 1GB/2-core tier rather than crashing.
if ! [[ "$TOTAL_RAM" =~ ^[0-9]+$ ]] || [ "$TOTAL_RAM" -le 0 ]; then
log "WARNING" "Could not reliably detect total RAM (got: '${TOTAL_RAM}') — defaulting to 1024MB tier"
TOTAL_RAM=1024
fi
if ! [[ "$TOTAL_CORES" =~ ^[0-9]+$ ]] || [ "$TOTAL_CORES" -le 0 ]; then
log "WARNING" "Could not reliably detect CPU core count (got: '${TOTAL_CORES}') — defaulting to 2 cores"
TOTAL_CORES=2
fi
log "INFO" "Detected ${TOTAL_RAM}MB RAM with ${TOTAL_CORES} cores"
if [ "$TOTAL_RAM" -le 512 ]; then
PHP_MAX_CHILDREN=5; PHP_START_SERVERS=2; PHP_MIN_SPARE=1; PHP_MAX_SPARE=3
PHP_MEMORY_LIMIT="128M"; PHP_MAX_EXECUTION=60
MYSQL_BUFFER_POOL="64M"; MYSQL_LOG_FILE="64M"
REDIS_MAX_MEMORY="64mb"; NGINX_WORKER_CONNECTIONS=512
elif [ "$TOTAL_RAM" -le 1024 ]; then
PHP_MAX_CHILDREN=10; PHP_START_SERVERS=3; PHP_MIN_SPARE=2; PHP_MAX_SPARE=5
PHP_MEMORY_LIMIT="128M"; PHP_MAX_EXECUTION=120
MYSQL_BUFFER_POOL="128M"; MYSQL_LOG_FILE="64M"
REDIS_MAX_MEMORY="128mb"; NGINX_WORKER_CONNECTIONS=1024
elif [ "$TOTAL_RAM" -le 2048 ]; then
PHP_MAX_CHILDREN=20; PHP_START_SERVERS=5; PHP_MIN_SPARE=3; PHP_MAX_SPARE=8
PHP_MEMORY_LIMIT="256M"; PHP_MAX_EXECUTION=180
MYSQL_BUFFER_POOL="256M"; MYSQL_LOG_FILE="128M"
REDIS_MAX_MEMORY="256mb"; NGINX_WORKER_CONNECTIONS=2048
elif [ "$TOTAL_RAM" -le 4096 ]; then
PHP_MAX_CHILDREN=40; PHP_START_SERVERS=8; PHP_MIN_SPARE=4; PHP_MAX_SPARE=12
PHP_MEMORY_LIMIT="512M"; PHP_MAX_EXECUTION=240
MYSQL_BUFFER_POOL="512M"; MYSQL_LOG_FILE="256M"
REDIS_MAX_MEMORY="512mb"; NGINX_WORKER_CONNECTIONS=4096
elif [ "$TOTAL_RAM" -le 8192 ]; then
PHP_MAX_CHILDREN=80; PHP_START_SERVERS=12; PHP_MIN_SPARE=6; PHP_MAX_SPARE=18
PHP_MEMORY_LIMIT="512M"; PHP_MAX_EXECUTION=300
MYSQL_BUFFER_POOL="1G"; MYSQL_LOG_FILE="512M"
REDIS_MAX_MEMORY="1gb"; NGINX_WORKER_CONNECTIONS=8192
elif [ "$TOTAL_RAM" -le 16384 ]; then
PHP_MAX_CHILDREN=160; PHP_START_SERVERS=20; PHP_MIN_SPARE=10; PHP_MAX_SPARE=30
PHP_MEMORY_LIMIT="1G"; PHP_MAX_EXECUTION=360
MYSQL_BUFFER_POOL="2G"; MYSQL_LOG_FILE="1G"
REDIS_MAX_MEMORY="2gb"; NGINX_WORKER_CONNECTIONS=16384
else
# Final catch-all: anything above our highest named tier (>16GB) OR
# any value that somehow slipped past the validation above. Conservative
# defaults here are intentionally generous-but-safe rather than unbounded,
# so a misdetected/huge value can't blow out memory-sized configs.
log "WARNING" "RAM (${TOTAL_RAM}MB) exceeds the highest defined tier — using capped high-end defaults"
PHP_MAX_CHILDREN=200; PHP_START_SERVERS=24; PHP_MIN_SPARE=12; PHP_MAX_SPARE=36
PHP_MEMORY_LIMIT="1G"; PHP_MAX_EXECUTION=360
MYSQL_BUFFER_POOL="4G"; MYSQL_LOG_FILE="1G"
REDIS_MAX_MEMORY="4gb"; NGINX_WORKER_CONNECTIONS=16384
fi
NGINX_WORKER_PROCESSES=$TOTAL_CORES
# Export so Python config script can read them via env
export TOTAL_RAM TOTAL_CORES
export PHP_MAX_CHILDREN PHP_START_SERVERS PHP_MIN_SPARE PHP_MAX_SPARE
export PHP_MEMORY_LIMIT PHP_MAX_EXECUTION
export MYSQL_BUFFER_POOL MYSQL_LOG_FILE
export REDIS_MAX_MEMORY NGINX_WORKER_CONNECTIONS NGINX_WORKER_PROCESSES
log "SUCCESS" "Auto-tuning complete — PHP children: $PHP_MAX_CHILDREN | MySQL: $MYSQL_BUFFER_POOL | Redis: $REDIS_MAX_MEMORY"
}
# ============================================
# SECTION 8 — OS DETECTION (BASH)
# ============================================
detect_os() {
log "STEP" "Detecting operating system"
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_ID=$ID; OS_NAME=$NAME; OS_VERSION=$VERSION_ID; OS_CODENAME=$VERSION_CODENAME
fi
if [ "$OS_ID" = "debian" ] && [ -f /etc/debian_version ]; then
DEBIAN_VERSION=$(cut -d. -f1 /etc/debian_version)
case $DEBIAN_VERSION in
10) OS_CODENAME="buster" ;;
11) OS_CODENAME="bullseye" ;;
12) OS_CODENAME="bookworm" ;;
*) OS_CODENAME="bullseye" ;;
esac
fi
if [ "$OS_ID" = "ubuntu" ] && [ -f /etc/lsb-release ]; then
. /etc/lsb-release; OS_CODENAME=$DISTRIB_CODENAME
fi
[ -z "$OS_CODENAME" ] && OS_CODENAME=$(lsb_release -sc 2>/dev/null || echo "focal")
export OS_ID OS_NAME OS_VERSION OS_CODENAME
log "SUCCESS" "Detected: $OS_NAME $OS_VERSION ($OS_CODENAME)"
}
# ============================================
# SECTION 9 — PACKAGE MANAGER & BASE DEPS (BASH)
# ============================================
setup_package_manager() {
log "STEP" "Setting up package manager and base dependencies"
run_cmd_retry 3 5 "apt-get update -y"
run_cmd_retry 2 3 "apt --fix-broken install -y"
local packages=(
apt-transport-https ca-certificates curl wget gnupg lsb-release
software-properties-common ufw fail2ban htop git unzip zip tar
jq net-tools dnsutils cron rsync nano vim openssl apache2-utils
systemd dbus python3 python3-pip python3-venv ncdu
)
for pkg in "${packages[@]}"; do
run_cmd_retry 2 3 "apt-get install -y $pkg" || log "WARNING" "Could not install: $pkg"
done
if [ "$OS_ID" = "ubuntu" ]; then
run_cmd_retry 2 3 "apt-get install -y mysql-client-8.0" || \
run_cmd_retry 2 3 "apt-get install -y mysql-client" || true
else
run_cmd_retry 2 3 "apt-get install -y mariadb-client" || \
run_cmd_retry 2 3 "apt-get install -y mysql-client" || true
fi
log "SUCCESS" "Base package setup complete"
}
# ============================================
# SECTION 10 — SWAP SETUP (BASH — filesystem op)
# ============================================
setup_swap() {
log "STEP" "Configuring swap space"
if [ ! -f /swapfile ]; then
if [ "$TOTAL_RAM" -le 512 ]; then SWAPSIZE=1G
elif [ "$TOTAL_RAM" -le 1024 ]; then SWAPSIZE=2G
elif [ "$TOTAL_RAM" -le 2048 ]; then SWAPSIZE=3G
else SWAPSIZE=4G; fi
log "INFO" "Creating ${SWAPSIZE} swap file"
fallocate -l $SWAPSIZE /swapfile 2>/dev/null || \
dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress
chmod 600 /swapfile
if mkswap /swapfile && swapon /swapfile; then
echo '/swapfile none swap sw 0 0' >> /etc/fstab
echo "vm.swappiness=10" >> /etc/sysctl.conf
sysctl -p >/dev/null 2>&1 || true
log "SUCCESS" "Swap created: $SWAPSIZE"
else
log "ERROR" "Failed to create swap"; rm -f /swapfile
fi
else
log "INFO" "Swap file already exists"
fi
}
# ============================================
# SECTION 11 — INSTALL NGINX (BASH: repo + pkg)
# ============================================
install_nginx_packages() {
log "STEP" "Installing Nginx from official repository"
run_cmd_retry 2 3 "apt-get remove -y nginx nginx-common nginx-full nginx-core" 2>/dev/null || true
run_cmd_retry 3 5 "curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor -o /usr/share/keyrings/nginx-archive-keyring.gpg"
if [ "$OS_ID" = "ubuntu" ]; then
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/ubuntu ${OS_CODENAME} nginx" | \
tee /etc/apt/sources.list.d/nginx.list
else
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/debian ${OS_CODENAME} nginx" | \
tee /etc/apt/sources.list.d/nginx.list
fi
run_cmd_retry 3 5 "apt-get update -y"
run_cmd_retry 3 5 "apt-get install -y nginx"
# Optional: Brotli module
run_cmd_retry 2 3 "apt-get install -y libnginx-mod-brotli" 2>/dev/null || \
log "WARNING" "Brotli module not available — gzip remains active"
# Optional: GeoIP2 module
run_cmd_retry 2 3 "apt-get install -y libnginx-mod-http-geoip2 mmdb-bin" 2>/dev/null || true
mkdir -p /etc/nginx/{sites-available,sites-enabled,conf.d,ssl,snippets}
mkdir -p /var/cache/nginx/{fastcgi,proxy,static,edge}
mkdir -p /var/log/nginx
# FIX: Use www-data (matches PHP-FPM socket owner and nginx worker user in config)
chown -R www-data:www-data /var/cache/nginx 2>/dev/null || true
chmod -R 755 /var/cache/nginx 2>/dev/null || true
log "SUCCESS" "Nginx packages installed"
}
# ============================================
# SECTION 12 — INSTALL PHP (BASH: repo + pkgs)
# ============================================
install_php_packages() {
log "STEP" "Installing PHP from Sury/Ondrej repository"
if [ "$OS_ID" = "debian" ]; then
run_cmd_retry 3 5 "apt-get install -y apt-transport-https lsb-release ca-certificates curl wget"
run_cmd_retry 3 5 "wget -qO- https://packages.sury.org/php/apt.gpg | gpg --dearmor > /etc/apt/trusted.gpg.d/sury-php.gpg"
echo "deb https://packages.sury.org/php/ ${OS_CODENAME} main" | tee /etc/apt/sources.list.d/sury-php.list
run_cmd_retry 3 5 "apt-get update -y"
else
run_cmd_retry 3 5 "add-apt-repository -y ppa:ondrej/php"
run_cmd_retry 3 5 "apt-get update -y"
fi
# Try PHP 8.4 → 8.3 → 8.2
PHP_INSTALLED_VERSION=""
for ver in 8.4 8.3 8.2; do
local pkgs="php${ver}-fpm php${ver}-mysql php${ver}-curl php${ver}-gd php${ver}-mbstring"
pkgs="$pkgs php${ver}-xml php${ver}-xmlrpc php${ver}-zip php${ver}-soap php${ver}-intl"
pkgs="$pkgs php${ver}-bcmath php${ver}-imagick php${ver}-redis php${ver}-opcache"
pkgs="$pkgs php${ver}-readline php${ver}-apcu php${ver}-memcached php${ver}-igbinary"
if run_cmd_retry 3 5 "apt-get install -y $pkgs"; then
PHP_INSTALLED_VERSION=$ver
log "SUCCESS" "PHP $ver installed"
# Always also try installing 8.3 and 8.2 as fallback versions
else
log "WARNING" "PHP $ver not available"
fi
done
[ -z "$PHP_INSTALLED_VERSION" ] && { log "ERROR" "No PHP version could be installed"; return 1; }
export PHP_INSTALLED_VERSION
log "SUCCESS" "PHP installation complete (primary: $PHP_INSTALLED_VERSION)"
}
# ============================================
# SECTION 13 — INSTALL MARIADB (BASH: repo + pkg)
# ============================================
install_mysql_packages() {
log "STEP" "Installing MariaDB 11.x"
systemctl stop mysql 2>/dev/null || true
systemctl stop mariadb 2>/dev/null || true
log "INFO" "Adding MariaDB 11.x official repository"
run_cmd_retry 3 5 "curl -fsSL https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash -s -- --mariadb-server-version=mariadb-11.4 --skip-maxscale" 2>/dev/null || \
log "WARNING" "MariaDB 11.x repo failed, falling back to distro default"
run_cmd_retry 3 5 "apt-get update -y"
run_cmd_retry 2 3 "apt --fix-broken install -y"
run_cmd_retry 3 5 "apt-get install -y mariadb-server mariadb-client"
sleep 5
systemctl enable mariadb
systemctl start mariadb
log "SUCCESS" "MariaDB packages installed"
}
# ============================================
# SECTION 14 — INSTALL WP-CLI (BASH: download)
# ============================================
install_wp_cli() {
log "STEP" "Installing WP-CLI"
if run_cmd_retry 3 5 "curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar"; then
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
run_cmd_retry 2 3 "curl -O https://raw.githubusercontent.com/wp-cli/wp-cli/v2.8.0/utils/wp-completion.bash"
mv wp-completion.bash /etc/bash_completion.d/wp-completion.bash 2>/dev/null || true
/usr/local/bin/wp --info &>/dev/null && log "SUCCESS" "WP-CLI installed" || \
log "ERROR" "WP-CLI verification failed"
else
log "ERROR" "Failed to download WP-CLI"
fi
# Weekly self-update cron
cat > /etc/cron.weekly/easyinstall-wpcli-update <<'WPCLIUPDATE'
#!/bin/bash
/usr/local/bin/wp cli update --yes --allow-root 2>/dev/null && \
echo "[$(date)] WP-CLI updated" >> /var/log/easyinstall/install.log || true
WPCLIUPDATE
chmod +x /etc/cron.weekly/easyinstall-wpcli-update
log "SUCCESS" "WP-CLI weekly auto-update cron installed"
}
# ============================================
# SECTION 15 — INSTALL REDIS (BASH: repo + pkg)
# ============================================
install_redis_packages() {
log "STEP" "Installing Redis 7.x"
run_cmd_retry 3 5 "curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg" 2>/dev/null || true
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb ${OS_CODENAME} main" | \
tee /etc/apt/sources.list.d/redis.list 2>/dev/null || true
run_cmd_retry 2 3 "apt-get update -y" 2>/dev/null || true
run_cmd_retry 3 5 "apt-get install -y redis-server redis-tools"
log "SUCCESS" "Redis packages installed"
}
# ============================================
# SECTION 16 — INSTALL CERTBOT (BASH)
# ============================================
install_certbot() {
log "STEP" "Installing Certbot for SSL"
run_cmd_retry 3 5 "apt-get install -y certbot python3-certbot-nginx"
command -v certbot &>/dev/null && log "SUCCESS" "Certbot installed" || \
log "ERROR" "Certbot installation failed"
}
# ============================================
# SECTION 17 — GET/MARK REDIS PORTS (BASH)
# ============================================
get_next_redis_port() {
mkdir -p /var/lib/easyinstall
touch "$USED_REDIS_PORTS_FILE"
local port=6379 attempt=1
while [ $attempt -le 100 ]; do
if ! grep -q "^$port$" "$USED_REDIS_PORTS_FILE" 2>/dev/null; then
if ! ss -tlnp 2>/dev/null | grep -q ":$port "; then
echo "$port"; return 0
fi
fi
port=$((port + 1)); attempt=$((attempt + 1))
done
log "ERROR" "Could not find available Redis port"; echo "6379"; return 1
}
mark_redis_port_used() {
local port=$1
mkdir -p /var/lib/easyinstall
echo "$port" >> "$USED_REDIS_PORTS_FILE"
sort -u "$USED_REDIS_PORTS_FILE" -o "$USED_REDIS_PORTS_FILE"
log "INFO" "Redis port $port marked as used"
}
# ============================================
# SECTION 18 — START/ENABLE SERVICES (BASH)
# ============================================
enable_start_nginx() {
# FIX: Remove default nginx site before starting to avoid port 80 conflict
rm -f /etc/nginx/sites-enabled/default 2>/dev/null || true
systemctl enable nginx
systemctl start nginx
wait_for_service "nginx" 30 || return 1
}
enable_start_php() {
for version in 8.4 8.3 8.2; do
if [ -d "/etc/php/$version" ]; then
systemctl enable php$version-fpm 2>/dev/null || true
systemctl start php$version-fpm 2>/dev/null || true
wait_for_service "php$version-fpm" 20 || log "WARNING" "PHP $version-FPM may not be fully running"
fi
done
}
enable_start_redis() {
systemctl enable redis-server
systemctl start redis-server
wait_for_service "redis-server" 20 && test_redis 6379
mkdir -p /var/lib/easyinstall
echo "6379" > "$USED_REDIS_PORTS_FILE"
}
enable_start_mariadb() {
systemctl enable mariadb
systemctl start mariadb
wait_for_service "mariadb" 30 || return 1
test_mysql_connection || return 1
}
# ============================================
# SECTION 19 — CLEANUP
# ============================================
cleanup_temp_files() {
log "STEP" "Cleaning up temporary files"
find /tmp -name "wordpress*.tar.gz" -type f -mmin +60 -delete 2>/dev/null || true
[ "$1" = "success" ] && apt-get clean && log "INFO" "Package cache cleaned"
find /var/log/easyinstall -name "*.log" -mtime +30 -delete 2>/dev/null || true
log "SUCCESS" "Cleanup completed"
}
# ============================================
# SECTION 19b — DETECT ACTIVE PHP VERSION (BASH)
# Finds the highest PHP-FPM version that is running.
# Called by wordpress_install stage to pass correct version.
# ============================================
detect_active_php_version() {
for ver in 8.4 8.3 8.2; do
if systemctl is-active --quiet "php${ver}-fpm" 2>/dev/null; then
echo "$ver"
return 0
fi
# Also check if the socket exists even if service name differs
if [ -S "/run/php/php${ver}-fpm.sock" ]; then
echo "$ver"
return 0
fi
done
echo "8.3" # safe default
}
# ============================================
# SECTION 19c — PHP SOCKET HEALTH FIX (BASH)
# ============================================
test_php_fpm() {
local version=$1
local sock="/run/php/php${version}-fpm.sock"
if [ ! -S "$sock" ]; then
log "WARNING" "PHP-FPM $version socket not found at $sock"
return 1
fi
# Fix socket permissions so www-data + nginx can both read it
chmod 666 "$sock" 2>/dev/null || true
log "SUCCESS" "PHP-FPM $version socket OK: $sock"
return 0
}
# ============================================
# SECTION 19d — CREATE PER-SITE REDIS INSTANCE (BASH)
# Bash handles: systemd unit creation, daemon-reload, enable/start.
# Config file is written by Python (wordpress_install stage).
# ============================================
create_site_redis_instance() {
local domain=$1
local redis_port=$2
local domain_slug="${domain//./-}"
log "INFO" "Starting dedicated Redis instance for $domain on port $redis_port"
# Config file already written by Python stage; just start the service
if [ ! -f "/etc/redis/redis-${domain_slug}.conf" ]; then
log "WARNING" "Redis config for $domain not found — Python stage may not have run yet"
return 1
fi
systemctl daemon-reload
systemctl enable "redis-${domain_slug}" 2>/dev/null || true
systemctl start "redis-${domain_slug}" 2>/dev/null || true
if wait_for_service "redis-${domain_slug}" 20; then
mark_redis_port_used "$redis_port"
log "SUCCESS" "Redis instance started for $domain (port $redis_port)"
return 0
else
log "ERROR" "Redis instance failed to start for $domain"
return 1
fi
}
# ============================================
# SECTION 20 — INSTALL OLLAMA (BASH: curl install)
# ============================================
install_ollama() {
log "STEP" "Installing Ollama for local AI"
if command -v ollama &>/dev/null; then
local ver=$(ollama --version 2>/dev/null || echo "installed")
log "INFO" "Ollama already installed: $ver"
else
run_cmd_retry 3 5 "curl -fsSL https://ollama.com/install.sh | sh" || {
log "ERROR" "Ollama installation failed"; return 1; }
log "SUCCESS" "Ollama installed"
fi
systemctl enable ollama 2>/dev/null || true
systemctl start ollama 2>/dev/null || true
sleep 3
if systemctl is-active --quiet ollama 2>/dev/null; then
log "SUCCESS" "Ollama service running"
else
ollama serve >/dev/null 2>&1 &
sleep 4
fi
# Pick model based on RAM
local model="llama3"
[ "${TOTAL_RAM:-0}" -ge 8192 ] && model="llama3.1"
[ "${TOTAL_RAM:-0}" -lt 4096 ] && model="phi3"
[ "${TOTAL_RAM:-0}" -lt 2048 ] && model="tinyllama"
log "INFO" "Pulling Ollama model: $model"
ollama pull "$model" 2>/dev/null || log "WARNING" "Model pull failed — will retry on first use"
log "SUCCESS" "Ollama ready with model: $model"
}
# ============================================
# SECTION 21 — PYTHON BRIDGE
# Runs the Python config generator with all
# tuning values exported as environment vars.
# ============================================
run_python_config() {
local stage="$1"
shift
log "STEP" "Running Python config generator: stage=$stage"
# Extra args (e.g. --domain, --php-version) forwarded as-is after shift
python3 "$PYTHON_CONFIG_SCRIPT" \
--stage "$stage" \
--total-ram "$TOTAL_RAM" \
--total-cores "$TOTAL_CORES" \
--php-max-children "$PHP_MAX_CHILDREN" \
--php-start-servers "$PHP_START_SERVERS" \
--php-min-spare "$PHP_MIN_SPARE" \
--php-max-spare "$PHP_MAX_SPARE" \
--php-memory-limit "$PHP_MEMORY_LIMIT" \
--php-max-execution "$PHP_MAX_EXECUTION" \
--mysql-buffer-pool "$MYSQL_BUFFER_POOL" \
--mysql-log-file "$MYSQL_LOG_FILE" \
--redis-max-memory "$REDIS_MAX_MEMORY" \
--nginx-worker-connections "$NGINX_WORKER_CONNECTIONS" \
--nginx-worker-processes "$NGINX_WORKER_PROCESSES" \
--os-id "$OS_ID" \
--os-codename "$OS_CODENAME" \
"$@" && {
log "SUCCESS" "Python config stage '$stage' complete"
} || {
log "ERROR" "Python config stage '$stage' failed"
return 1
}
}
# ============================================
# SECTION 22 — INSTALLATION TESTS
# ============================================
test_installation() {
log "STEP" "Testing installation"
local failed=0
systemctl is-active --quiet nginx && {
log "SUCCESS" "Nginx test passed"
validate_nginx_config || failed=$((failed + 1))
} || { log "ERROR" "Nginx test failed"; failed=$((failed + 1)); }
systemctl is-active --quiet mariadb && {
log "SUCCESS" "MariaDB test passed"
test_mysql_connection || failed=$((failed + 1))
} || { log "ERROR" "MariaDB test failed"; failed=$((failed + 1)); }
wp --info &>/dev/null && log "SUCCESS" "WP-CLI test passed" || {
log "ERROR" "WP-CLI test failed"; failed=$((failed + 1)); }
redis-cli ping &>/dev/null && {
log "SUCCESS" "Redis test passed"
test_redis 6379 || failed=$((failed + 1))
} || { log "ERROR" "Redis test failed"; failed=$((failed + 1)); }
local php_found=0
for version in 8.4 8.3 8.2; do
systemctl is-active --quiet php${version}-fpm 2>/dev/null && {
log "SUCCESS" "PHP ${version}-FPM test passed"; php_found=1; }
done
[ $php_found -eq 0 ] && { log "ERROR" "No PHP-FPM found"; failed=$((failed + 1)); }
systemctl is-active --quiet autoheal && log "SUCCESS" "Autoheal test passed" || \
log "WARNING" "Autoheal test failed (non-critical)"
systemctl is-active --quiet fail2ban && log "SUCCESS" "Fail2ban test passed" || \
log "WARNING" "Fail2ban test failed (non-critical)"
[ $failed -eq 0 ] && log "SUCCESS" "All critical tests passed!" || \
log "WARNING" "$failed critical test(s) failed. Check logs."
return $failed
}
# ============================================
# SECTION 23 — AUTOHEAL SERVICE START
# ============================================
start_autoheal() {
systemctl daemon-reload
systemctl enable autoheal 2>/dev/null || true
systemctl start autoheal 2>/dev/null || true
wait_for_service "autoheal" 10 && log "SUCCESS" "Autoheal service running" || \
log "WARNING" "Autoheal may not be running"
}
start_fail2ban() {
systemctl enable fail2ban
systemctl restart fail2ban
wait_for_service "fail2ban" 20 && log "SUCCESS" "Fail2ban running" || \
log "WARNING" "Fail2ban may not be running"
}
# ============================================
# SECTION 24 — MAIN INSTALLATION FLOW
# ============================================
phase_preflight() {
clear
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}🚀 EasyInstall WordPress Performance v6.4 (HYBRID EDITION)${NC}"
echo -e "${GREEN} Bash = Dependencies | Python = Configuration${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${YELLOW}Architecture:${NC}"
echo -e " • ${CYAN}Bash layer${NC} → apt installs, repos, service start/enable, lock files"
echo -e " • ${CYAN}Python layer${NC} → all config file generation, tuning, WP setup, monitoring"
echo ""
check_root
check_lock
# Create directories first
mkdir -p /var/log/easyinstall /var/lib/easyinstall /var/www/html
mkdir -p /etc/nginx/{sites-available,sites-enabled,ssl,conf.d,snippets}
mkdir -p /backups/{daily,weekly,monthly}
mkdir -p /etc/letsencrypt
touch "$LOG_FILE" "$ERROR_LOG" "$STATUS_FILE"
check_os_compatibility || exit 1
check_network || exit 1
check_disk_space 5120 || exit 1
check_memory
update_status "START" "Installation started"
# ── Backup existing configs ──────────────────────────────────────────
backup_config \
"/etc/nginx/nginx.conf" \
"/etc/mysql/mariadb.conf.d/99-wordpress.cnf" \
"/etc/php/8.3/fpm/php.ini" \
"/etc/php/8.2/fpm/php.ini" \
"/etc/redis/redis.conf" \
"/etc/fail2ban/jail.local"
}
# ── PHASE A: Bash — detect & tune ───────────────────────────────────
phase_detect_and_tune() {
detect_ram_and_tune
update_status "DETECT" "RAM detection complete"
detect_os
update_status "OS" "OS detection complete"
}
# ── PHASE B: Bash — install all packages ───────────────────────────
phase_base_packages() {
setup_package_manager
update_status "PACKAGES" "Package manager setup complete"
setup_swap
update_status "SWAP" "Swap setup complete"
}
# ── PHASE C: Python — kernel tuning (writes sysctl files) ──────────
phase_kernel_tuning() {
# Deploy the Python config script now (it was placed by the installer)
deploy_python_script
run_python_config "kernel_tuning"
sysctl -p /etc/sysctl.d/99-wordpress.conf 2>/dev/null || true
update_status "KERNEL" "Kernel tuning complete"
}
# ── PHASE D/E/F: Bash+Python — Nginx (packages, core config, extras) ─
phase_nginx() {
install_nginx_packages
update_status "NGINX_PKGS" "Nginx packages installed"
run_python_config "nginx_config"
enable_start_nginx
update_status "NGINX" "Nginx configured and running"
# Nginx extras (Brotli, CF, SSL, WS, HTTP3, Edge) — enhancements on top
# of the already-working core nginx_config above; a failure here
# shouldn't sink the entire install.
run_python_config "nginx_extras" || log "WARNING" "nginx_extras stage failed — continuing without it"
run_python_config "websocket_support" || log "WARNING" "websocket_support stage failed — continuing without it"
run_python_config "http3_quic" || log "WARNING" "http3_quic stage failed — continuing without it"
run_python_config "edge_computing" || log "WARNING" "edge_computing stage failed — continuing without it"
reload_or_restart_service "nginx" "nginx -t"
update_status "NGINX_EXTRAS" "Nginx extras complete"
}
# ── PHASE G/H: Bash+Python — PHP ─────────────────────────────────────
phase_php() {
install_php_packages
update_status "PHP_PKGS" "PHP packages installed"
run_python_config "php_config"
enable_start_php
update_status "PHP" "PHP configured and running"
}
# Secure the MariaDB root account — generates a strong random password and
# stores it in /root/.my.cnf instead of leaving root passwordless.
# Split out from phase_mariadb() so the password-generation/verification
# logic reads as a single, self-contained unit.
#
# SECURITY FIX: previously this set the root password to an EMPTY string,
# which (depending on MariaDB version) can downgrade root auth from the
# OS-level `unix_socket`/`auth_socket` plugin to `mysql_native_password`
# with no password at all — meaning ANY local process/user could connect
# as root with zero credentials. Instead we generate a strong random
# password, apply it, and store it in /root/.my.cnf (mode 600) so every
# subsequent `mysql ...` invocation in this script — and in
# easyinstall_config.py, which calls bare `mysql -e` — transparently
# authenticates via that file without any code changes.
secure_mariadb_root() {