forked from getphporg/getphp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathphpup.sh
More file actions
1480 lines (1274 loc) · 49.5 KB
/
Copy pathphpup.sh
File metadata and controls
1480 lines (1274 loc) · 49.5 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
#!/usr/bin/env bash
# ============================================================
# phpup — Mac & Linux Web Stack Installer & Dashboard
# Inspired by getphp.org (Mac, Linux)
# GitHub: https://github.com/DaFa66/phpup
# Author: Simon Field (aka - DaFa)
# License: MIT
# Date: 2026-07-19
# Version: 1.0.0
# ============================================================
# ---- Config -------------------------------------------------
REMOTE_URL='https://raw.githubusercontent.com/DaFa66/phpup/HEAD/phpup.sh'
BASE_DIR="${HOME}/phpup"
DOC_ROOT="${BASE_DIR}/www"
LOGS_DIR="${BASE_DIR}/logs"
CONFIG_DIR="${HOME}/.config/phpup"
CONFIG_FILE="${CONFIG_DIR}/config.json"
DATA_BACKUP_DIR="${BASE_DIR}/data_backup"
# ---- Colour Constants ---------------------------------------
ESC='\033'
RED="${ESC}[31m"
GREEN="${ESC}[32m"
YELLOW="${ESC}[33m"
CYAN="${ESC}[36m"
BOLD="${ESC}[1m"
UNDERLINE="${ESC}[4m"
RESET="${ESC}[0m"
# ---- Platform Detection -------------------------------------
ARCH=$(uname -m)
OS_TYPE="${OSTYPE}"
if [[ "${OS_TYPE}" == "darwin"* ]]; then
OS_NAME="macOS"
OS_VERSION=$(sw_vers -productVersion 2>/dev/null || echo "unknown")
OS_MAJOR=$(echo "$OS_VERSION" | cut -d. -f1)
SHELL_PROFILE="${HOME}/.zshrc"
HTTPD_USER="_www"
USE_APT=0
elif [[ "${OS_TYPE}" == "linux-gnu"* ]]; then
OS_NAME="Linux"
if command -v lsb_release &>/dev/null; then
OS_VERSION=$(lsb_release -rs 2>/dev/null || echo "unknown")
OS_DISTRO=$(lsb_release -is 2>/dev/null || echo "Linux")
else
OS_VERSION="unknown"
OS_DISTRO="Linux"
fi
SHELL_PROFILE="${HOME}/.bashrc"
HTTPD_USER="www-data"
USE_APT=1
else
OS_NAME="Unknown"
OS_VERSION="unknown"
SHELL_PROFILE="${HOME}/.bashrc"
HTTPD_USER="www-data"
USE_APT=0
fi
# ---- Homebrew Detection -------------------------------------
HOMEBREW=0
BREW_PREFIX=""
if brew --version &>/dev/null; then
HOMEBREW=1
BREW_PREFIX=$(brew --prefix)
fi
APACHE=0
MARIADB=0
PHP=0
PHPMYADMIN=0
STACK=0
# ---- JSON Helpers (no jq dependency) ------------------------
json_get() {
# Usage: json_get "$json_string" "key"
# Extremely naive JSON parser — sufficient for our flat config
local json="$1" key="$2"
echo "$json" | grep -o "\"${key}\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" | head -1 | sed 's/.*: *"\([^"]*\)".*/\1/'
}
json_get_versions() {
local json="$1" component="$2"
# Extract the versions block and find the component version
echo "$json" | grep -o "\"${component}\"[[:space:]]*:[[:space:]]*\"[^\"]*\"" | head -1 | sed 's/.*: *"\([^"]*\)".*/\1/'
}
# ---- Config Persistence -------------------------------------
load_config() {
if [[ -f "$CONFIG_FILE" ]]; then
cat "$CONFIG_FILE"
else
echo ""
fi
}
save_config() {
local install_path="$1"
local apache_ver="$2"
local mariadb_ver="$3"
local php_ver="$4"
local phpmyadmin_ver="$5"
mkdir -p "$CONFIG_DIR"
local now
now=$(date "+%Y-%m-%dT%H:%M:%S")
cat > "$CONFIG_FILE" << EOF
{
"install_path": "${install_path}",
"installed_at": "${now}",
"brew_prefix": "${BREW_PREFIX}",
"architecture": "${ARCH}",
"os": "${OS_NAME} ${OS_VERSION}",
"versions": {
"apache": "${apache_ver}",
"mariadb": "${mariadb_ver}",
"php": "${php_ver}",
"phpmyadmin": "${phpmyadmin_ver}"
}
}
EOF
}
clear_config() {
if [[ -f "$CONFIG_FILE" ]]; then
rm -f "$CONFIG_FILE"
fi
if [[ -d "$CONFIG_DIR" ]]; then
rmdir "$CONFIG_DIR" 2>/dev/null || true
fi
}
# ---- Component Detection ------------------------------------
detect_apache() {
if [[ $USE_APT == 1 ]]; then
if dpkg -l apache2 &>/dev/null 2>&1 && dpkg -s apache2 &>/dev/null 2>&1; then
APACHE=1
APACHE_VERSION=$(dpkg -s apache2 2>/dev/null | grep '^Version:' | awk '{print $2}' | cut -d- -f1)
else
APACHE=0
APACHE_VERSION=""
fi
elif [[ -d "${BREW_PREFIX}/Cellar/httpd" ]]; then
APACHE=1
APACHE_VERSION=$(find "${BREW_PREFIX}/Cellar/httpd" -maxdepth 1 -mindepth 1 -exec basename {} \; 2>/dev/null | sort -V | tail -1)
else
APACHE=0
APACHE_VERSION=""
fi
}
detect_mariadb() {
if [[ $USE_APT == 1 ]]; then
if dpkg -l mariadb-server &>/dev/null 2>&1 && dpkg -s mariadb-server &>/dev/null 2>&1; then
MARIADB=1
MARIADB_VERSION=$(dpkg -s mariadb-server 2>/dev/null | grep '^Version:' | awk '{print $2}' | cut -d- -f1 | cut -d: -f2)
else
MARIADB=0
MARIADB_VERSION=""
fi
elif [[ -d "${BREW_PREFIX}/Cellar/mariadb" ]]; then
MARIADB=1
MARIADB_VERSION=$(find "${BREW_PREFIX}/Cellar/mariadb" -maxdepth 1 -mindepth 1 -exec basename {} \; 2>/dev/null | sort -V | tail -1)
else
MARIADB=0
MARIADB_VERSION=""
fi
}
detect_php() {
if [[ $USE_APT == 1 ]]; then
if dpkg -l php &>/dev/null 2>&1 && dpkg -s php &>/dev/null 2>&1; then
PHP=1
PHP_VERSION=$(dpkg -s php 2>/dev/null | grep '^Version:' | awk '{print $2}' | cut -d- -f1 | cut -d: -f2)
else
PHP=0
PHP_VERSION=""
fi
elif [[ -d "${BREW_PREFIX}/Cellar/php" ]]; then
PHP=1
PHP_VERSION=$(find "${BREW_PREFIX}/Cellar/php" -maxdepth 1 -mindepth 1 -exec basename {} \; 2>/dev/null | sort -V | tail -1)
else
PHP=0
PHP_VERSION=""
fi
}
detect_phpmyadmin() {
if [[ $USE_APT == 1 ]]; then
if dpkg -l phpmyadmin &>/dev/null 2>&1 && dpkg -s phpmyadmin &>/dev/null 2>&1; then
PHPMYADMIN=1
PHPMYADMIN_VERSION=$(dpkg -s phpmyadmin 2>/dev/null | grep '^Version:' | awk '{print $2}' | cut -d- -f1 | cut -d: -f2)
else
PHPMYADMIN=0
PHPMYADMIN_VERSION=""
fi
elif [[ -d "${BREW_PREFIX}/Cellar/phpmyadmin" ]]; then
PHPMYADMIN=1
PHPMYADMIN_VERSION=$(find "${BREW_PREFIX}/Cellar/phpmyadmin" -maxdepth 1 -mindepth 1 -exec basename {} \; 2>/dev/null | sort -V | tail -1)
else
PHPMYADMIN=0
PHPMYADMIN_VERSION=""
fi
}
is_service_running() {
local svc="$1"
if [[ $USE_APT == 1 ]]; then
case "$svc" in
apache|httpd)
systemctl is-active --quiet apache2 2>/dev/null && return 0 || return 1
;;
mariadb)
systemctl is-active --quiet mariadb 2>/dev/null && return 0 || return 1
;;
php)
systemctl is-active --quiet php*-fpm 2>/dev/null && return 0 || return 1
;;
*) return 1 ;;
esac
else
case "$svc" in
apache|httpd)
pgrep -x "httpd" &>/dev/null && return 0 || return 1
;;
mariadb)
pgrep -x "mariadbd" &>/dev/null && return 0 || return 1
;;
php)
pgrep -f "(^|/)php-fpm" &>/dev/null && return 0 || return 1
;;
*) return 1 ;;
esac
fi
}
detect_all() {
if [[ $USE_APT == 0 ]] && [[ $HOMEBREW == 0 ]]; then
return
fi
detect_apache
detect_mariadb
detect_php
detect_phpmyadmin
if [[ $APACHE == 1 && $MARIADB == 1 && $PHP == 1 && $PHPMYADMIN == 1 ]]; then
STACK=1
fi
}
# ---- Utility Functions --------------------------------------
print_ok() { printf "[${GREEN} OK ${RESET}] %s\n" "$1"; }
print_err() { printf "[${RED} ERROR ${RESET}] %s\n" "$1"; }
print_warn() { printf "[${YELLOW} WAIT ${RESET}] %s\n" "$1"; }
print_info() { printf "${CYAN}%s${RESET}\n" "$1"; }
# ---- Prerequisites Check ------------------------------------
check_prerequisites() {
# Linux: apt prerequisites
if [[ "${OS_TYPE}" == "linux-gnu"* ]]; then
local need_apt=0
for pkg in build-essential procps file git curl; do
if ! dpkg -s "$pkg" &>/dev/null; then
need_apt=1
break
fi
done
if [[ $need_apt == 1 ]]; then
printf "\n"
print_warn "Installing Linux prerequisites (build-essential, procps, file, git, curl)..."
sudo apt update -qq && sudo apt install -y build-essential procps file git curl
print_ok "Linux prerequisites installed"
fi
fi
# macOS: Xcode CLT
if [[ "${OS_TYPE}" == "darwin"* ]]; then
if ! xcode-select -p &>/dev/null; then
printf "\n"
print_warn "Xcode Command Line Tools required. Starting installation..."
xcode-select --install 2>/dev/null || true
printf "\n"
print_warn "Press Enter after the Xcode CLT installation completes..."
read -r
fi
fi
}
check_brew_path() {
# Ensure brew is in PATH (critical on Linux where it may not auto-configure)
if [[ $HOMEBREW == 1 ]] && ! command -v brew &>/dev/null; then
if [[ -f /home/linuxbrew/.linuxbrew/bin/brew ]]; then
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
elif [[ -f /usr/local/bin/brew ]]; then
eval "$(/usr/local/bin/brew shellenv)"
elif [[ -f /opt/homebrew/bin/brew ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
fi
}
# ---- Install Homebrew ---------------------------------------
install_homebrew() {
if [[ $HOMEBREW == 1 ]]; then
return
fi
printf "\n"
print_warn "Homebrew not found. Installing..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Post-install PATH setup
if [[ "${OS_TYPE}" == "linux-gnu"* ]]; then
if [[ -f /home/linuxbrew/.linuxbrew/bin/brew ]]; then
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
if ! grep -q 'linuxbrew/bin/brew shellenv' "$SHELL_PROFILE" 2>/dev/null; then
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> "$SHELL_PROFILE"
fi
print_ok "Added Homebrew to PATH in ${SHELL_PROFILE}"
fi
fi
if command -v brew &>/dev/null; then
HOMEBREW=1
BREW_PREFIX=$(brew --prefix)
print_ok "Homebrew installed successfully"
else
print_err "Homebrew installation failed"
exit 1
fi
}
# ---- PATH Management ----------------------------------------
manage_path() {
# On Linux/apt, binaries are already in standard system paths (/usr/bin)
if [[ $USE_APT == 1 ]]; then
print_ok "php and mysql available via system PATH"
return
fi
# On macOS, brew is already in PATH. On Linux, ensure shellenv is in profile.
if [[ "${OS_TYPE}" == "linux-gnu"* ]]; then
if ! grep -q 'linuxbrew/bin/brew shellenv' "$SHELL_PROFILE" 2>/dev/null; then
if [[ -f /home/linuxbrew/.linuxbrew/bin/brew ]]; then
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> "$SHELL_PROFILE"
print_ok "Added Homebrew to PATH in ${SHELL_PROFILE}"
fi
fi
fi
# Verify php and mysql are reachable
local brew_bin="${BREW_PREFIX}/bin"
if [[ -x "${brew_bin}/php" ]]; then
print_ok "php available: ${brew_bin}/php"
fi
if [[ -x "${brew_bin}/mariadb" ]] || [[ -x "${brew_bin}/mysql" ]]; then
print_ok "mariadb client available: ${brew_bin}/mariadb"
fi
}
# ---- Dashboard Display --------------------------------------
show_banner() {
printf "\n"
printf "┌─────────────────────────────┐\n"
printf "│ ____ _ _ ____ │\n"
printf "│ | _ \\| | | | _ \\ /\\ │\n"
printf "│ | |_) | |_| | |_) | || | │\n"
printf "│ | __/| _ | __/| || | │\n"
printf "│ |_| |_| |_|_| ||_| │\n"
printf "│ ▲ ▲ ▲ │\n"
printf "│ phpup │\n"
printf "└─────────────────────────────┘\n"
printf "\n"
}
show_dashboard() {
show_banner
# Architecture line
if [[ $USE_APT == 1 ]]; then
printf "Architecture: ${CYAN}%s${RESET} | OS: ${CYAN}%s %s${RESET} | Package: ${CYAN}apt${RESET}\n" \
"$ARCH" "$OS_NAME" "$OS_VERSION"
else
printf "Architecture: ${CYAN}%s${RESET} | OS: ${CYAN}%s %s${RESET} | Homebrew: ${CYAN}%s${RESET}\n" \
"$ARCH" "$OS_NAME" "$OS_VERSION" "$BREW_PREFIX"
fi
printf "\n"
# Stack Status
printf "Your Web Stack:\n"
printf "~~~~~~~~~~~~~~~\n"
printf "Apache ${CYAN}------->${RESET} "
if [[ $APACHE == 1 ]]; then
printf "%s\n" "$APACHE_VERSION"
else
printf "${RED}Not installed${RESET}\n"
fi
printf "MariaDB ${CYAN}------>${RESET} "
if [[ $MARIADB == 1 ]]; then
printf "%s\n" "$MARIADB_VERSION"
else
printf "${RED}Not installed${RESET}\n"
fi
printf "PHP ${CYAN}---------->${RESET} "
if [[ $PHP == 1 ]]; then
printf "%s\n" "$PHP_VERSION"
else
printf "${RED}Not installed${RESET}\n"
fi
printf "phpMyAdmin ${CYAN}--->${RESET} "
if [[ $PHPMYADMIN == 1 ]]; then
printf "%s\n" "$PHPMYADMIN_VERSION"
else
printf "${RED}Not installed${RESET}\n"
fi
printf "\n"
# Service Status
printf "Service Status:\n"
printf "~~~~~~~~~~~~~~~\n"
printf "Apache ${CYAN}------->${RESET} "
if [[ $APACHE == 1 ]]; then
is_service_running apache && printf "Running\n" || printf "Stopped\n"
else
printf "${RED}Not available${RESET}\n"
fi
printf "MariaDB ${CYAN}------>${RESET} "
if [[ $MARIADB == 1 ]]; then
is_service_running mariadb && printf "Running\n" || printf "Stopped\n"
else
printf "${RED}Not available${RESET}\n"
fi
printf "PHP-FPM ${CYAN}------>${RESET} "
if [[ $PHP == 1 ]]; then
is_service_running php && printf "Running\n" || printf "Stopped\n"
else
printf "${RED}Not available${RESET}\n"
fi
printf "\n"
# Quick Info (only when stack is complete)
if [[ $STACK == 1 ]]; then
printf "Quick Info:\n"
printf "~~~~~~~~~~~\n"
printf "${CYAN}Where to put website files?${RESET} %s\n" "$DOC_ROOT"
printf "${CYAN}How to test your PHP setup?${RESET} http://localhost/phpinfo.php\n"
printf "${CYAN}Where to access phpMyAdmin?${RESET} http://localhost/phpmyadmin\n"
printf "${CYAN}How to log into phpMyAdmin?${RESET} Username: root | Password: [blank]\n"
printf "\n"
fi
# Commands
printf "Stack Commands:\n"
printf "~~~~~~~~~~~~~~~\n"
if [[ $STACK == 0 ]]; then
printf "${CYAN}${UNDERLINE}I${RESET}${CYAN}nstall${RESET} Install the PHP stack.\n"
else
printf "${CYAN}${UNDERLINE}U${RESET}${CYAN}pdate${RESET} Update components to latest versions.\n"
printf "${CYAN}${UNDERLINE}R${RESET}${CYAN}estart${RESET} Restart all services.\n"
printf "${CYAN}${UNDERLINE}S${RESET}${CYAN}tart${RESET} Start / Stop services.\n"
printf "${CYAN}${UNDERLINE}D${RESET}${CYAN}elete${RESET} Delete the web stack.\n"
fi
printf "${CYAN}${UNDERLINE}Q${RESET}${CYAN}uit${RESET} Quit this application.\n"
printf "\n"
}
# ---- Service Management -------------------------------------
start_services() {
print_info "Starting services..."
if [[ $USE_APT == 1 ]]; then
[[ $APACHE == 1 ]] && sudo systemctl start apache2 2>/dev/null
[[ $MARIADB == 1 ]] && sudo systemctl start mariadb 2>/dev/null
[[ $PHP == 1 ]] && sudo systemctl start php*-fpm 2>/dev/null
else
if [[ $APACHE == 1 ]]; then
sudo "${BREW_PREFIX}/bin/apachectl" restart 2>/dev/null
sleep 1
if pgrep -x httpd &>/dev/null; then
print_ok "Apache started on port 80"
else
print_err "Apache may have failed to start — check ${LOGS_DIR}/apache_error.log"
sudo "${BREW_PREFIX}/bin/apachectl" configtest 2>&1 | tail -3
fi
fi
[[ $MARIADB == 1 ]] && brew services start mariadb 2>/dev/null
[[ $PHP == 1 ]] && brew services start php 2>/dev/null
fi
sleep 2
print_ok "Services started"
}
stop_services() {
print_info "Stopping services..."
if [[ $USE_APT == 1 ]]; then
[[ $APACHE == 1 ]] && sudo systemctl stop apache2 2>/dev/null
[[ $MARIADB == 1 ]] && sudo systemctl stop mariadb 2>/dev/null
[[ $PHP == 1 ]] && sudo systemctl stop php*-fpm 2>/dev/null
else
# Kill any httpd process, then stop the launchd service to prevent restart
[[ $APACHE == 1 ]] && sudo "${BREW_PREFIX}/bin/apachectl" stop 2>/dev/null
[[ $APACHE == 1 ]] && brew services stop httpd 2>/dev/null
[[ $MARIADB == 1 ]] && brew services stop mariadb 2>/dev/null
[[ $PHP == 1 ]] && brew services stop php 2>/dev/null
fi
sleep 3
print_ok "Services stopped"
}
restart_services() {
stop_services
start_services
}
toggle_services() {
local any_running=0
is_service_running apache && any_running=1
is_service_running mariadb && any_running=1
is_service_running php && any_running=1
if [[ $any_running == 1 ]]; then
stop_services
printf "\n${CYAN}Services stopped. Press S again to start them.${RESET}\n"
else
start_services
fi
}
# ---- Apache Configuration -----------------------------------
configure_apache() {
if [[ $USE_APT == 1 ]]; then
configure_apache_apt
return
fi
local conf="${BREW_PREFIX}/etc/httpd/httpd.conf"
if [[ ! -f "$conf" ]]; then
print_err "Apache config not found: $conf"
return 1
fi
# Backup original
if [[ ! -f "${conf}.phpup.bak" ]]; then
cp "$conf" "${conf}.phpup.bak"
fi
print_info "Configuring Apache..."
# Port 80
sed -i.bak "s/Listen 8080/Listen 80/" "$conf"
print_ok "Enabled port 80"
# ServerName
sed -i.bak "s/#ServerName www.example.com:8080/ServerName localhost:80/g" "$conf"
print_ok "Set ServerName to localhost:80"
# DocumentRoot
sed -i.bak "s@${BREW_PREFIX}/var/www@$DOC_ROOT@g" "$conf"
print_ok "Set DocumentRoot to ${DOC_ROOT}"
# Log files
sed -i.bak "s@${BREW_PREFIX}/var/log/httpd/error_log@${LOGS_DIR}/apache_error.log@g" "$conf"
sed -i.bak "s@${BREW_PREFIX}/var/log/httpd/access_log@${LOGS_DIR}/apache_access.log@g" "$conf"
print_ok "Routed logs to ${LOGS_DIR}"
# mod_rewrite
sed -i.bak "s@#LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so@LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so@g" "$conf"
sed -i.bak "s/AllowOverride None/AllowOverride All/g" "$conf"
print_ok "Enabled mod_rewrite"
# DirectoryIndex
sed -i.bak "s/DirectoryIndex index.html/DirectoryIndex index.php index.html/" "$conf"
print_ok "Added index.php to DirectoryIndex"
# Linux-specific: change user/group to www-data
if [[ "${OS_TYPE}" == "linux-gnu"* ]]; then
sed -i.bak "s/User _www/User www-data/" "$conf"
sed -i.bak "s/Group _www/Group www-data/" "$conf"
print_ok "Set Apache user/group to www-data (Linux)"
fi
# PHP module
local php_module_path="${BREW_PREFIX}/opt/php/lib/httpd/modules/libphp.so"
if ! grep -q "LoadModule php_module" "$conf"; then
printf "\nLoadModule php_module %s\n" "$php_module_path" >> "$conf"
fi
if ! grep -q "<FilesMatch \\\.php\$>" "$conf"; then
cat >> "$conf" << 'PHPFILESMATCH'
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
PHPFILESMATCH
fi
print_ok "Enabled php_module"
# phpMyAdmin alias
local pma_path="${BREW_PREFIX}/share/phpmyadmin"
if ! grep -q "Alias /phpmyadmin" "$conf"; then
cat >> "$conf" << PMAALIAS
Alias /phpmyadmin ${pma_path}
<Directory ${pma_path}/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
PMAALIAS
fi
print_ok "Created phpMyAdmin alias"
# Ensure www directory is writable by both user and Apache
sudo chgrp -R _www "$DOC_ROOT" 2>/dev/null || chgrp -R _www "$DOC_ROOT" 2>/dev/null || true
sudo chmod -R 775 "$DOC_ROOT" 2>/dev/null || chmod -R 775 "$DOC_ROOT" 2>/dev/null || true
print_ok "Set ${DOC_ROOT} group to _www (775)"
# Clean up sed backup files
rm -f "${conf}.bak"
}
# ---- Apache Configuration (apt) ------------------------------
configure_apache_apt() {
local site_conf="/etc/apache2/sites-available/000-default.conf"
local main_conf="/etc/apache2/apache2.conf"
print_info "Configuring Apache (apt)..."
# Enable mod_rewrite
sudo a2enmod rewrite 2>/dev/null
print_ok "Enabled mod_rewrite"
# Configure 000-default.conf — the default site
if [[ -f "$site_conf" ]]; then
if [[ ! -f "${site_conf}.phpup.bak" ]]; then
sudo cp "$site_conf" "${site_conf}.phpup.bak"
fi
# DocumentRoot
sudo sed -i "s@DocumentRoot /var/www/html@DocumentRoot ${DOC_ROOT}@" "$site_conf"
print_ok "Set DocumentRoot to ${DOC_ROOT}"
# AllowOverride All for .htaccess
sudo sed -i "s/AllowOverride None/AllowOverride All/g" "$site_conf"
print_ok "Set AllowOverride All"
# DirectoryIndex
sudo sed -i "s/DirectoryIndex index.html/DirectoryIndex index.php index.html/" "$site_conf"
print_ok "Added index.php to DirectoryIndex"
# Log files — redirect to phpup logs
sudo sed -i "s@\${APACHE_LOG_DIR}/error.log@${LOGS_DIR}/apache_error.log@" "$site_conf"
sudo sed -i "s@\${APACHE_LOG_DIR}/access.log@${LOGS_DIR}/apache_access.log@" "$site_conf"
print_ok "Routed logs to ${LOGS_DIR}"
fi
# ServerName in apache2.conf
if [[ -f "$main_conf" ]]; then
if ! grep -q "ServerName localhost" "$main_conf"; then
echo "ServerName localhost:80" | sudo tee -a "$main_conf" > /dev/null
print_ok "Set ServerName to localhost:80"
fi
fi
# Ensure www directory is writable by both user and Apache
sudo chgrp -R www-data "$DOC_ROOT" 2>/dev/null || true
sudo chmod -R 775 "$DOC_ROOT" 2>/dev/null || true
print_ok "Set ${DOC_ROOT} group to www-data (775)"
# Reload Apache to apply changes
sudo systemctl reload apache2 2>/dev/null || sudo systemctl start apache2 2>/dev/null
print_ok "Apache configured"
}
# ---- PHP Configuration --------------------------------------
configure_php() {
if [[ $USE_APT == 1 ]]; then
configure_php_apt
return
fi
local php_ini="${BREW_PREFIX}/etc/php/$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;' 2>/dev/null)/php.ini"
# Fallback: search for php.ini
if [[ ! -f "$php_ini" ]]; then
php_ini=$(php -r 'echo php_ini_loaded_file();' 2>/dev/null)
fi
if [[ ! -f "$php_ini" ]]; then
php_ini=$(php -i 2>/dev/null | grep "Loaded Configuration File" | awk -F' => ' '{print $2}')
fi
if [[ ! -f "$php_ini" ]]; then
print_warn "Could not locate php.ini — skipping PHP configuration"
return
fi
# Backup
if [[ ! -f "${php_ini}.phpup.bak" ]]; then
cp "$php_ini" "${php_ini}.phpup.bak"
fi
print_info "Configuring PHP..."
# Enable extensions only if the .so file exists (Homebrew PHP 8.x compiles most statically)
local ext_dir
ext_dir=$(php -r 'echo PHP_EXTENSION_DIR;' 2>/dev/null)
local extensions=("curl" "fileinfo" "gd" "intl" "mbstring" "mysqli" "openssl" "pdo_mysql" "pdo_sqlite" "sodium" "sqlite3")
for ext in "${extensions[@]}"; do
if [[ -f "${ext_dir}/${ext}.so" ]]; then
sed -i.bak "s/^; *extension=${ext}/extension=${ext}/" "$php_ini" 2>/dev/null || true
fi
done
print_ok "Enabled PHP extensions"
# Display errors
sed -i.bak "s/^display_errors = Off/display_errors = On/" "$php_ini" 2>/dev/null || true
sed -i.bak "s/^display_errors = Off/display_errors = On/" "$php_ini" 2>/dev/null || true
print_ok "Enabled display_errors"
# Error log
local error_log_line="error_log = ${LOGS_DIR}/php_errors.log"
if ! grep -q "^error_log" "$php_ini" 2>/dev/null; then
echo "$error_log_line" >> "$php_ini"
else
sed -i.bak "s@^error_log.*@${error_log_line}@" "$php_ini"
fi
print_ok "Set PHP error log to ${LOGS_DIR}/php_errors.log"
# OPCache
if grep -q "^;*opcache.enable=" "$php_ini" 2>/dev/null; then
sed -i.bak "s/^;*opcache.enable=.*/opcache.enable=1/" "$php_ini"
sed -i.bak "s/^;*opcache.memory_consumption=.*/opcache.memory_consumption=256/" "$php_ini"
sed -i.bak "s/^;*opcache.interned_strings_buffer=.*/opcache.interned_strings_buffer=16/" "$php_ini"
sed -i.bak "s/^;*opcache.max_accelerated_files=.*/opcache.max_accelerated_files=20000/" "$php_ini"
print_ok "Configured OPCache (256MB, JIT-ready)"
fi
rm -f "${php_ini}.bak"
}
# ---- PHP Configuration (apt) ---------------------------------
configure_php_apt() {
print_info "Configuring PHP (apt)..."
# Find the Apache PHP ini
local php_ver
php_ver=$(php -r 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;' 2>/dev/null)
local php_ini="/etc/php/${php_ver}/apache2/php.ini"
if [[ ! -f "$php_ini" ]]; then
# Fallback: try CLI ini
php_ini=$(php -r 'echo php_ini_loaded_file();' 2>/dev/null)
fi
if [[ ! -f "$php_ini" ]]; then
print_warn "Could not locate php.ini — skipping PHP configuration"
return
fi
if [[ ! -f "${php_ini}.phpup.bak" ]]; then
sudo cp "$php_ini" "${php_ini}.phpup.bak"
fi
# Display errors
sudo sed -i "s/^display_errors = Off/display_errors = On/" "$php_ini" 2>/dev/null || true
print_ok "Enabled display_errors"
# Error log
if ! grep -q "^error_log" "$php_ini" 2>/dev/null; then
echo "error_log = ${LOGS_DIR}/php_errors.log" | sudo tee -a "$php_ini" > /dev/null
else
sudo sed -i "s@^error_log.*@error_log = ${LOGS_DIR}/php_errors.log@" "$php_ini"
fi
print_ok "Set PHP error log to ${LOGS_DIR}/php_errors.log"
# OPCache
if grep -q "^;*opcache.enable=" "$php_ini" 2>/dev/null; then
sudo sed -i "s/^;*opcache.enable=.*/opcache.enable=1/" "$php_ini"
sudo sed -i "s/^;*opcache.memory_consumption=.*/opcache.memory_consumption=256/" "$php_ini"
sudo sed -i "s/^;*opcache.interned_strings_buffer=.*/opcache.interned_strings_buffer=16/" "$php_ini"
sudo sed -i "s/^;*opcache.max_accelerated_files=.*/opcache.max_accelerated_files=20000/" "$php_ini"
print_ok "Configured OPCache (256MB, JIT-ready)"
fi
# Extensions should already be enabled via apt package dependencies
print_ok "PHP configured"
}
# ---- MariaDB Configuration ----------------------------------
configure_mariadb() {
if [[ $USE_APT == 1 ]]; then
configure_mariadb_apt
return
fi
print_info "Configuring MariaDB..."
# Start MariaDB to initialise data directory
brew services start mariadb 2>/dev/null
sleep 3
# Set blank root password (Homebrew MariaDB often has no password by default)
if mysql -u root -e "SELECT 1" &>/dev/null 2>&1; then
print_ok "MariaDB root access confirmed (no password)"
else
# Try to set blank password via safe mode
brew services stop mariadb 2>/dev/null
sleep 1
mysqld_safe --skip-grant-tables &
sleep 3
mysql -u root -e "FLUSH PRIVILEGES; ALTER USER 'root'@'localhost' IDENTIFIED BY ''; FLUSH PRIVILEGES;" 2>/dev/null || true
killall mysqld_safe 2>/dev/null || true
sleep 1
brew services start mariadb 2>/dev/null
sleep 2
print_ok "MariaDB root password set to blank"
fi
# Configure my.cnf with error log
local my_cnf="${BREW_PREFIX}/etc/my.cnf"
if [[ ! -f "$my_cnf" ]]; then
my_cnf="${BREW_PREFIX}/etc/my.cnf.d/server.cnf"
fi
if [[ -f "$my_cnf" ]] || [[ -d "$(dirname "$my_cnf")" ]]; then
if ! grep -q "log-error" "$my_cnf" 2>/dev/null; then
mkdir -p "$(dirname "$my_cnf")" 2>/dev/null || true
echo "[mysqld]" >> "$my_cnf"
echo "log-error = ${LOGS_DIR}/mariadb_error.log" >> "$my_cnf"
print_ok "Set MariaDB error log to ${LOGS_DIR}/mariadb_error.log"
fi
else
print_warn "Could not configure MariaDB my.cnf — log routing skipped"
fi
}
# ---- MariaDB Configuration (apt) -----------------------------
configure_mariadb_apt() {
print_info "Configuring MariaDB (apt)..."
# Ensure MariaDB is running
sudo systemctl start mariadb 2>/dev/null || true
sleep 2
# Set blank root password via Unix socket (default on Debian/Ubuntu)
if sudo mysql -u root -e "SELECT 1" &>/dev/null 2>&1; then
# Set blank password for TCP connections
sudo mysql -u root -e "ALTER USER 'root'@'localhost' IDENTIFIED BY ''; FLUSH PRIVILEGES;" 2>/dev/null || true
print_ok "MariaDB root access confirmed (no password via socket)"
else
print_warn "Could not connect to MariaDB as root — you may need to set a password manually"
fi
# Configure error log
local mariadb_conf="/etc/mysql/mariadb.conf.d/50-server.cnf"
if [[ -f "$mariadb_conf" ]]; then
if ! grep -q "log_error" "$mariadb_conf" 2>/dev/null; then
echo "log_error = ${LOGS_DIR}/mariadb_error.log" | sudo tee -a "$mariadb_conf" > /dev/null
print_ok "Set MariaDB error log to ${LOGS_DIR}/mariadb_error.log"
fi
else
print_warn "Could not configure MariaDB error log — config file not found"
fi
print_ok "MariaDB configured"
}
# ---- phpMyAdmin Configuration -------------------------------
configure_phpmyadmin() {
if [[ $USE_APT == 1 ]]; then
configure_phpmyadmin_apt
return
fi
local pma_conf="${BREW_PREFIX}/etc/phpmyadmin.config.inc.php"
if [[ ! -f "$pma_conf" ]]; then
print_warn "phpMyAdmin config not found — skipping"
return
fi
# Backup
if [[ ! -f "${pma_conf}.phpup.bak" ]]; then
cp "$pma_conf" "${pma_conf}.phpup.bak"
fi
print_info "Configuring phpMyAdmin..."
# Blowfish secret
sed -i.bak "s/\$cfg\['blowfish_secret'\] = '';/\$cfg\['blowfish_secret'\] = '12345678901234567890123456789012';/" "$pma_conf"
print_ok "Set blowfish secret"
# Allow passwordless root login
sed -i.bak "s/\$cfg\['Servers'\]\[\$i\]\['AllowNoPassword'\] = false;/\$cfg\['Servers'\]\[\$i\]\['AllowNoPassword'\] = true;/" "$pma_conf"
print_ok "Enabled passwordless root login"
# Create phpMyAdmin tmp directory (required for template cache)
local pma_tmp="${BREW_PREFIX}/share/phpmyadmin/tmp"
sudo mkdir -p "$pma_tmp" 2>/dev/null || mkdir -p "$pma_tmp" 2>/dev/null || true
sudo chgrp _www "$pma_tmp" 2>/dev/null || true
sudo chmod 775 "$pma_tmp" 2>/dev/null || chmod 775 "$pma_tmp" 2>/dev/null || true
print_ok "Created phpMyAdmin tmp directory"
rm -f "${pma_conf}.bak"
}
# ---- phpMyAdmin Configuration (apt) --------------------------
configure_phpmyadmin_apt() {
local pma_conf="/etc/phpmyadmin/config.inc.php"
if [[ ! -f "$pma_conf" ]]; then
print_warn "phpMyAdmin config not found — skipping"
return
fi
if [[ ! -f "${pma_conf}.phpup.bak" ]]; then
sudo cp "$pma_conf" "${pma_conf}.phpup.bak"
fi
print_info "Configuring phpMyAdmin (apt)..."
# Blowfish secret
sudo sed -i "s/\\$cfg\\['blowfish_secret'\\] = '';/\\$cfg\\['blowfish_secret'\\] = '12345678901234567890123456789012';/" "$pma_conf"
print_ok "Set blowfish secret"
# Allow passwordless root login
sudo sed -i "s/\\$cfg\\['Servers'\\]\\[\\$i\\]\\['AllowNoPassword'\\] = false;/\\$cfg\\['Servers'\\]\\[\\$i\\]\\['AllowNoPassword'\\] = true;/" "$pma_conf"
print_ok "Enabled passwordless root login"
# Create phpMyAdmin tmp directory (required for template cache)
local pma_tmp="/usr/share/phpmyadmin/tmp"
sudo mkdir -p "$pma_tmp" 2>/dev/null || true
sudo chgrp www-data "$pma_tmp" 2>/dev/null || true
sudo chmod 775 "$pma_tmp" 2>/dev/null || true
print_ok "Created phpMyAdmin tmp directory"
print_ok "phpMyAdmin configured"
}
# ---- Install Command ----------------------------------------
cmd_install() {
if [[ $STACK == 1 ]]; then
print_err "Stack is already installed. Use U to update or D to delete first."
printf "\n"
read -r -p "Press Enter to continue..."
return
fi
printf "\n${BOLD}phpup — Install Web Stack${RESET}\n\n"
# Prerequisites
check_prerequisites
# Create directories
mkdir -p "$DOC_ROOT"
print_ok "Created directory: ${DOC_ROOT}"
mkdir -p "$LOGS_DIR"
print_ok "Created directory: ${LOGS_DIR}"
# Install packages (apt or brew)
if [[ $USE_APT == 1 ]]; then
printf "\n"
print_info "Installing packages via apt..."
printf "\n"
sudo apt update -qq
[[ $APACHE == 0 ]] && sudo apt install -y apache2 && APACHE=1
[[ $MARIADB == 0 ]] && sudo apt install -y mariadb-server && MARIADB=1
[[ $PHP == 0 ]] && sudo apt install -y php php-curl php-fileinfo php-gd php-intl php-mbstring php-mysql php-sqlite3 php-sodium libapache2-mod-php && PHP=1
[[ $PHPMYADMIN == 0 ]] && sudo apt install -y phpmyadmin && PHPMYADMIN=1
detect_all
else
# Install Homebrew
install_homebrew
printf "\n"
print_info "Installing packages via Homebrew..."
printf "\n"
[[ $APACHE == 0 ]] && printf 'y\n' | HOMEBREW_NO_AUTO_UPDATE=1 brew install httpd && APACHE=1
[[ $MARIADB == 0 ]] && { printf 'y\n' | HOMEBREW_NO_AUTO_UPDATE=1 brew install mariadb || true; }
[[ $PHP == 0 ]] && printf 'y\n' | HOMEBREW_NO_AUTO_UPDATE=1 brew install php && PHP=1
[[ $PHPMYADMIN == 0 ]] && printf 'y\n' | HOMEBREW_NO_AUTO_UPDATE=1 brew install phpmyadmin && PHPMYADMIN=1