-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathphpup.sh
More file actions
3072 lines (2744 loc) · 128 KB
/
Copy pathphpup.sh
File metadata and controls
3072 lines (2744 loc) · 128 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-08-11
# 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"
BLUE="${ESC}[34m"
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_DISTRO="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")
elif [[ -f /etc/os-release ]]; then
# lsb_release not installed on minimal Debian — parse os-release instead
OS_DISTRO=$(grep '^ID=' /etc/os-release | cut -d= -f2 | tr -d '"' | sed 's/.*/\u&/')
OS_VERSION=$(grep '^VERSION_ID=' /etc/os-release | cut -d= -f2 | tr -d '"')
[[ -z "$OS_DISTRO" ]] && OS_DISTRO="Linux"
[[ -z "$OS_VERSION" ]] && OS_VERSION="unknown"
else
OS_VERSION="unknown"
OS_DISTRO="Linux"
fi
SHELL_PROFILE="${HOME}/.bashrc"
HTTPD_USER="www-data"
USE_APT=1
else
OS_NAME="Unknown"
OS_DISTRO="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
# ---- MacPorts Detection -------------------------------------
USE_PORTS=0 # 1 = MacPorts backend active (Intel macOS only)
MACPORTS=0 # 1 = /opt/local/bin/port present on machine
PORT_PREFIX="/opt/local"
MACPORTS_PATHS_FILE="/etc/paths.d/macports" # new-login-shell PATH entry (self-healed by manage_path)
BREW_MIN_OS_MAJOR=14 # Homebrew officially supports the latest 3 macOS releases (14/15/26 as of 2026-08)
MARIADB_PORT="mariadb-12.3" # fallback candidate: mariadb-11.4
PHP_PORT="php85" # active PHP port (changes on fu switch)
PMA_DIR="/opt/local/share/phpmyadmin" # ports backend tarball target
MACPORTS_VERSION="2.12.5" # bump when a new MacPorts release ships (see install.php)
MACPORTS_MIN_OS="10.15" # below this: machine not viable for a modern stack
# ---- MacPorts paths (single source of truth) ----------------
PORTS_APACHE_CONF="${PORT_PREFIX}/etc/apache2/httpd.conf"
PORTS_APACHE_MODDIR="${PORT_PREFIX}/lib/apache2/modules"
PORTS_APACHE_DOCROOT="${PORT_PREFIX}/www/apache2/html"
PORTS_APACHECTL="${PORT_PREFIX}/sbin/apachectl"
PORTS_MARIADB_DATADIR="${PORT_PREFIX}/var/db/${MARIADB_PORT}"
PORTS_MARIADB_SOCKET="${PORT_PREFIX}/var/run/${MARIADB_PORT}/mysqld.sock"
PORTS_MARIADB_CONF="${PORT_PREFIX}/etc/${MARIADB_PORT}/my.cnf"
PORTS_MARIADB_INSTALL_DB="${PORT_PREFIX}/lib/${MARIADB_PORT}/bin/mariadb-install-db"
PORTS_MARIADB_SERVER_PORT="${MARIADB_PORT}-server"
if [[ -x "${PORT_PREFIX}/bin/port" ]]; then
MACPORTS=1
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")
local pkg_mgr="brew"
[[ $USE_APT == 1 ]] && pkg_mgr="apt"
[[ $USE_PORTS == 1 ]] && pkg_mgr="port"
cat > "$CONFIG_FILE" << EOF
{
"install_path": "${install_path}",
"installed_at": "${now}",
"package_manager": "${pkg_mgr}",
"brew_prefix": "${BREW_PREFIX}",
"port_prefix": "${PORT_PREFIX}",
"architecture": "${ARCH}",
"os": "${OS_DISTRO} ${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
# ^ii = installed. rc (removed, config kept) must NOT count as installed —
# otherwise delete leaves rc packages and reinstall skips them.
if dpkg -l apache2 2>/dev/null | grep -q '^ii'; 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 [[ $USE_PORTS == 1 ]] && [[ -f "${PORT_PREFIX}/etc/apache2/httpd.conf" ]]; then
APACHE=1
APACHE_VERSION=$("${PORT_PREFIX}/sbin/httpd" -v 2>/dev/null | sed -n 's/.*Apache\/\([0-9.]*\).*/\1/p')
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 2>/dev/null | grep -q '^ii'; then
MARIADB=1
MARIADB_VERSION=$(dpkg -s mariadb-server 2>/dev/null | grep '^Version:' | awk '{print $2}' | cut -d- -f1 | cut -d: -f2 | sed 's/+maria~.*//')
else
MARIADB=0
MARIADB_VERSION=""
fi
elif [[ $USE_PORTS == 1 ]]; then
# F2: self-healing fallback — the mariadb-11.4 LTS series chosen by a
# previous install must survive across sessions (config.json is
# write-only). If the default series datadir is absent but any other
# mariadb-1[12].* datadir exists, adopt that series BEFORE the check.
if [[ ! -d "${PORT_PREFIX}/var/db/${MARIADB_PORT}" ]]; then
local found_series
found_series=$(find "${PORT_PREFIX}/var/db" -maxdepth 1 -type d -name 'mariadb-1[12].*' 2>/dev/null | sed 's@.*/@@' | sort -V | tail -1)
[[ -n "$found_series" ]] && MARIADB_PORT="$found_series"
fi
if [[ -d "${PORT_PREFIX}/var/db/${MARIADB_PORT}" ]]; then
MARIADB=1
# MacPorts installs the client at the versioned path (no
# /opt/local/bin/mariadb symlink) and prints "from X.Y.Z-MariaDB"
# (not "Distrib X.Y.Z" like brew). Try the versioned client first,
# fall back to the generic path, then extract any dotted version.
local maria_client="${PORT_PREFIX}/lib/${MARIADB_PORT}/bin/mariadb"
MARIADB_VERSION=$("$maria_client" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
[[ -z "$MARIADB_VERSION" ]] && \
MARIADB_VERSION=$("${PORT_PREFIX}/bin/mariadb" --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
elif [[ -d "${BREW_PREFIX}/Cellar/mariadb" ]]; then
# Ports datadir absent (mixed machine or forced PHPPUP_BACKEND=port)
# — fall back to brew detection, mirroring detect_apache's fall-through.
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
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 command -v php &>/dev/null 2>&1; then
PHP=1
# Report the active version from the binary (versioned installs have no php meta package)
PHP_VERSION=$(php -r 'echo PHP_VERSION;' 2>/dev/null || dpkg -s php 2>/dev/null | grep '^Version:' | awk '{print $2}' | cut -d- -f1 | cut -d: -f2)
else
PHP=0
PHP_VERSION=""
fi
elif [[ $USE_PORTS == 1 ]] && command -v php &>/dev/null 2>&1; then
PHP=1
PHP_VERSION=$(php -r 'echo PHP_VERSION;' 2>/dev/null)
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
# Check tarball install first (phpup-version.txt written by install_pma_tarball)
if [[ -f /usr/share/phpmyadmin/phpup-version.txt ]]; then
PHPMYADMIN=1
PHPMYADMIN_VERSION=$(cat /usr/share/phpmyadmin/phpup-version.txt)
elif dpkg -l phpmyadmin 2>/dev/null | grep -q '^ii'; 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 [[ $USE_PORTS == 1 ]] && [[ -f "${PMA_DIR}/phpup-version.txt" ]]; then
PHPMYADMIN=1
PHPMYADMIN_VERSION=$(cat "${PMA_DIR}/phpup-version.txt")
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)
# MacPorts runs the server as "mysqld" (no mariadbd binary name);
# brew/apt use "mariadbd". Match either so detection works on all
# three backends.
{ pgrep -x "mariadbd" &>/dev/null || pgrep -x "mysqld" &>/dev/null; } && return 0 || return 1
;;
php)
if [[ $USE_PORTS == 1 ]]; then
# mod_php inside Apache — no standalone PHP-FPM service
is_service_running apache && return 0 || return 1
fi
pgrep -f "(^|/)php-fpm" &>/dev/null && return 0 || return 1
;;
*) return 1 ;;
esac
fi
}
detect_all() {
if [[ $USE_APT == 0 ]] && [[ $HOMEBREW == 0 ]] && [[ $MACPORTS == 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"; }
# Quiet apt update: suppress the "no stable CLI" warning apt emits when its
# output is piped (non-TTY), plus the "can be upgraded" notices. Real errors pass through.
apt_update_quiet() {
sudo apt update -qq > /dev/null 2>&1 || true
}
# ---- 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)..."
apt_update_quiet && 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..."
# Recheck CLT in a loop — never proceed into source builds without it
# (missing CLT wastes an hour of build time on older Macs).
while ! xcode-select -p &>/dev/null; do
print_warn "Xcode CLT is still not installed. Waiting for installation to finish..."
print_warn "Press Enter once the Xcode CLT installation has completed..."
read -r || return 1
done
print_ok "Xcode CLT detected."
fi
if [[ $USE_PORTS == 1 ]]; then
print_info "MacPorts backend: packages compile from source — Xcode CLT is required."
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
}
# ---- Install MacPorts ---------------------------------------
install_macports() {
[[ $MACPORTS == 1 ]] && return
printf "\n"
print_warn "MacPorts not found. Installing MacPorts ${MACPORTS_VERSION}..."
local osver token
osver=$(sw_vers -productVersion | cut -d. -f1-2)
case "$osver" in
10.6) token="10.6-SnowLeopard";; 10.7) token="10.7-Lion";;
10.8) token="10.8-MountainLion";; 10.9) token="10.9-Mavericks";;
10.10) token="10.10-Yosemite";; 10.11) token="10.11-ElCapitan";;
10.12) token="10.12-Sierra";; 10.13) token="10.13-HighSierra";;
10.14) token="10.14-Mojave";; 10.15) token="10.15-Catalina";;
11) token="11-BigSur";; 12) token="12-Monterey";; 13) token="13-Ventura";;
14) token="14-Sonoma";; 15) token="15-Sequoia";; 26) token="26-Tahoe";;
*) print_err "No MacPorts installer for macOS ${osver} — this machine is not viable for a modern web stack."; return 1 ;;
esac
local ver="${MACPORTS_VERSION:-2.12.5}"
local url="https://github.com/macports/macports-base/releases/download/v${ver}/MacPorts-${ver}-${token}.pkg"
local pkg="/tmp/MacPorts-${ver}-${token}.pkg"
if ! curl -fsSL --connect-timeout 30 "$url" -o "$pkg"; then
print_err "Failed to download MacPorts installer: $url"
return 1
fi
sudo installer -pkg "$pkg" -target / || { print_err "MacPorts installer failed"; return 1; }
rm -f "$pkg"
export PATH="${PORT_PREFIX}/bin:${PORT_PREFIX}/sbin:${PATH}"
MACPORTS=1
print_ok "MacPorts installed"
print_info "Updating ports tree (first selfupdate can take a few minutes)..."
sudo "${PORT_PREFIX}/bin/port" -v selfupdate || print_warn "port selfupdate failed — ports tree may be stale"
}
# ---- 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
# MacPorts backend — /opt/local must come BEFORE /usr/bin so the active
# MacPorts php/mysql win over Apple's bundled binaries (e.g. /usr/bin/php
# 7.3.x on Catalina). Two layers:
# 1. /etc/paths.d/macports — self-heal if the pkg installer missed it
# (adds /opt/local to new login shells, but AFTER /usr/bin)
# 2. SHELL_PROFILE prepend — the layer that actually beats /usr/bin
if [[ $USE_PORTS == 1 ]]; then
local macports_paths_file="$MACPORTS_PATHS_FILE"
if [[ -f "$macports_paths_file" ]] && grep -q '^/opt/local/bin$' "$macports_paths_file" 2>/dev/null; then
print_ok "MacPorts paths.d entry present (${macports_paths_file})"
else
printf '/opt/local/bin\n/opt/local/sbin\n' | sudo tee "$macports_paths_file" >/dev/null 2>&1
print_ok "Created ${macports_paths_file} for new login shells"
fi
local marker="# phpup: keep MacPorts binaries ahead of system (/usr/bin)"
local path_line='export PATH="/opt/local/bin:/opt/local/sbin:$PATH"'
if grep -qF "$path_line" "$SHELL_PROFILE" 2>/dev/null; then
print_ok "MacPorts binaries ahead of /usr/bin in ${SHELL_PROFILE}"
else
printf '\n%s\n%s\n' "$marker" "$path_line" >> "$SHELL_PROFILE"
print_ok "Prepended /opt/local/bin to PATH in ${SHELL_PROFILE}"
fi
if [[ -x "${PORT_PREFIX}/bin/php" ]]; then
print_ok "php available: ${PORT_PREFIX}/bin/php"
fi
if [[ -x "${PORT_PREFIX}/bin/mysql" ]] || [[ -x "${PORT_PREFIX}/bin/mariadb" ]]; then
print_ok "mariadb client available: ${PORT_PREFIX}/bin/mysql"
fi
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 "│ "
printf "${YELLOW}▲${RESET} ${GREEN}▲${RESET} ${CYAN}▲${RESET}"
printf " │\n"
printf "│ "
printf "${BLUE}phpup${RESET}"
printf " │\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_DISTRO" "$OS_VERSION"
elif [[ $USE_PORTS == 1 ]]; then
printf "Architecture: ${CYAN}%s${RESET} | OS: ${CYAN}%s %s${RESET} | Package: ${CYAN}port${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 "${BOLD}Your Web Stack:${RESET}\n"
printf "~~~~~~~~~~~~~~~\n"
printf "%-10s -----> " "Apache"
if [[ $APACHE == 1 ]]; then
printf "${GREEN}%s${RESET}\n" "$APACHE_VERSION"
else
printf "${RED}Not installed${RESET}\n"
fi
printf "%-10s -----> " "MariaDB"
if [[ $MARIADB == 1 ]]; then
printf "${GREEN}%s${RESET}\n" "$MARIADB_VERSION"
else
printf "${RED}Not installed${RESET}\n"
fi
printf "%-10s -----> " "PHP"
if [[ $PHP == 1 ]]; then
printf "${GREEN}%s${RESET}\n" "$PHP_VERSION"
else
printf "${RED}Not installed${RESET}\n"
fi
printf "%-10s -----> " "phpMyAdmin"
if [[ $PHPMYADMIN == 1 ]]; then
printf "${GREEN}%s${RESET}\n" "$PHPMYADMIN_VERSION"
else
printf "${RED}Not installed${RESET}\n"
fi
printf "\n"
# Service Status
printf "${BOLD}Service Status:${RESET}\n"
printf "~~~~~~~~~~~~~~~\n"
printf "%-10s -----> " "Apache"
if [[ $APACHE == 1 ]]; then
is_service_running apache && printf "${GREEN}Running${RESET}\n" || printf "${RED}Stopped${RESET}\n"
else
printf "${RED}Not available${RESET}\n"
fi
printf "%-10s -----> " "MariaDB"
if [[ $MARIADB == 1 ]]; then
is_service_running mariadb && printf "${GREEN}Running${RESET}\n" || printf "${RED}Stopped${RESET}\n"
else
printf "${RED}Not available${RESET}\n"
fi
printf "%-10s -----> " "PHP"
if [[ $PHP == 1 ]]; then
if [[ $USE_APT == 1 ]]; then
# apt uses mod_php (libapache2-mod-php) — check fpm only if installed
if dpkg -l 'php*-fpm' 2>/dev/null | grep -q '^ii'; then
is_service_running php && printf "${GREEN}Running (FPM)${RESET}\n" || printf "${RED}Stopped${RESET}\n"
else
is_service_running apache && printf "${GREEN}Active (mod_php)${RESET}\n" || printf "${RED}Stopped${RESET}\n"
fi
elif [[ $USE_PORTS == 1 ]]; then
# ports: mod_php inside Apache (phpXX-apache2handler)
is_service_running apache && printf "${GREEN}Active (mod_php)${RESET}\n" || printf "${RED}Stopped${RESET}\n"
else
is_service_running php && printf "${GREEN}Running${RESET}\n" || printf "${RED}Stopped${RESET}\n"
fi
else
printf "${RED}Not available${RESET}\n"
fi
printf "\n"
# Quick Info (only when stack is complete)
if [[ $STACK == 1 ]]; then
printf "${BOLD}Quick Info:${RESET}\n"
printf "~~~~~~~~~~~\n"
printf "Where to put website files? ${CYAN}%s${RESET}\n" "$DOC_ROOT"
printf "How to test your PHP setup? ${CYAN}http://localhost/phpinfo.php${RESET}\n"
printf "Where to access phpMyAdmin? ${CYAN}http://localhost/phpmyadmin${RESET}\n"
printf "How to log into phpMyAdmin? ${CYAN}Username: root | Password: [blank]${RESET}\n"
printf "\n"
fi
# Commands
printf "${BOLD}Stack Commands:${RESET}\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
elif [[ $USE_PORTS == 1 ]]; then
if [[ $APACHE == 1 ]]; then
sudo "${PORT_PREFIX}/bin/port" load apache2 >/dev/null 2>&1
for ((_i=0; _i<10; _i++)); do
pgrep -x httpd &>/dev/null && { print_ok "Apache started on port 80"; break; }
sleep 1
done
if ! pgrep -x httpd &>/dev/null; then
print_err "Apache may have failed to start — check ${LOGS_DIR}/apache_error.log"
sudo "${PORT_PREFIX}/sbin/apachectl" configtest 2>&1 | tail -3
fi
fi
if [[ $MARIADB == 1 ]]; then
sudo "${PORT_PREFIX}/bin/port" load "${MARIADB_PORT}-server" >/dev/null 2>&1
for ((_i=0; _i<10; _i++)); do
{ pgrep -x mariadbd &>/dev/null || pgrep -x mysqld &>/dev/null; } && { print_ok "MariaDB started"; break; }
sleep 1
done
if ! { pgrep -x mariadbd &>/dev/null || pgrep -x mysqld &>/dev/null; }; then
print_err "MariaDB failed to start — check ${PORT_PREFIX}/var/log/${MARIADB_PORT}/mariadb_error.log"
tail -50 "${PORT_PREFIX}/var/log/${MARIADB_PORT}/mariadb_error.log" 2>/dev/null || \
tail -50 "${PORT_PREFIX}/var/db/${MARIADB_PORT}/"*.err 2>/dev/null || true
fi
fi
# No PHP service — mod_php inside Apache
else
if [[ $APACHE == 1 ]]; then
sudo "${BREW_PREFIX}/bin/apachectl" restart >/dev/null 2>&1
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
if [[ $MARIADB == 1 ]]; then
brew services start mariadb 2>/dev/null
for ((_i=0; _i<5; _i++)); do
{ pgrep -x mariadbd &>/dev/null || pgrep -x mysqld &>/dev/null; } && { print_ok "MariaDB started"; break; }
sleep 1
done
if ! { pgrep -x mariadbd &>/dev/null || pgrep -x mysqld &>/dev/null; }; then
print_err "MariaDB failed to start — check ${LOGS_DIR}/mariadb_error.log"
tail -50 "${LOGS_DIR}/mariadb_error.log" 2>/dev/null || true
fi
fi
if [[ $PHP == 1 ]]; then
brew services start php 2>/dev/null
for ((_i=0; _i<5; _i++)); do
pgrep -f "(^|/)php-fpm" &>/dev/null && { print_ok "PHP-FPM started"; break; }
sleep 1
done
pgrep -f "(^|/)php-fpm" &>/dev/null || print_err "PHP-FPM failed to start — check ${LOGS_DIR}/php_errors.log"
fi
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
elif [[ $USE_PORTS == 1 ]]; then
[[ $APACHE == 1 ]] && sudo "${PORT_PREFIX}/bin/port" unload apache2 >/dev/null 2>&1 || true
[[ $MARIADB == 1 ]] && sudo "${PORT_PREFIX}/bin/port" unload "${MARIADB_PORT}-server" >/dev/null 2>&1 || true
# Belt-and-braces: launchd will not restart after unload
pkill -x httpd 2>/dev/null || true
pkill -x mariadbd 2>/dev/null || pkill -x mysqld 2>/dev/null || true
else
# Kill any httpd process, then stop the launchd service to prevent restart
if [[ $APACHE == 1 ]] && is_service_running apache; then
sudo "${BREW_PREFIX}/bin/apachectl" stop >/dev/null 2>&1
sleep 1
if ! pgrep -x httpd &>/dev/null; then
print_ok "Apache stopped"
fi
fi
[[ $MARIADB == 1 ]] && brew services stop mariadb
[[ $MARIADB == 1 ]] && pkill -x mariadbd 2>/dev/null || pkill -x mysqld 2>/dev/null || true
[[ $PHP == 1 ]] && brew services stop php
[[ $PHP == 1 ]] && pkill -f "(^|/)php-fpm" 2>/dev/null || true
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
if [[ $USE_PORTS == 1 ]]; then
configure_apache_ports
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 — skip if libphp.so doesn't exist (brew install may have failed)
local php_module_path="${BREW_PREFIX}/opt/php/lib/httpd/modules/libphp.so"
if [[ -f "$php_module_path" ]]; then
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"
else
print_warn "PHP module not found (${php_module_path}) — PHP may not have installed correctly"
fi
# 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 sh -c "chgrp -R _www '$DOC_ROOT' && chmod -R 775 '$DOC_ROOT'" 2>/dev/null || chgrp -R _www "$DOC_ROOT" && 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}"
# Add Directory grant for new doc root (default Apache policy denies non-/var/www paths)
if ! grep -q "<Directory ${DOC_ROOT}>" "$site_conf"; then
sudo sed -i "s|</VirtualHost>|\t<Directory ${DOC_ROOT}>\n\t\tOptions Indexes FollowSymLinks\n\t\tAllowOverride All\n\t\tRequire all granted\n\t</Directory>\n\n</VirtualHost>|" "$site_conf"
print_ok "Added Directory grant for ${DOC_ROOT}"
fi
# 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)"
# Ensure Apache can traverse the home directory (default 700 blocks www-data)
chmod o+x "$HOME" 2>/dev/null || true
# Reload Apache to apply changes
sudo systemctl reload apache2 2>/dev/null || sudo systemctl start apache2 2>/dev/null
print_ok "Apache configured"
}
# ---- Apache Configuration (MacPorts) ------------------------
configure_apache_ports() {
local conf="${PORT_PREFIX}/etc/apache2/httpd.conf"
if [[ ! -f "$conf" ]]; then print_err "Apache config not found: $conf"; return 1; fi
if [[ ! -f "${conf}.phpup.bak" ]]; then sudo cp "$conf" "${conf}.phpup.bak"; fi
print_info "Configuring Apache (MacPorts)..."
# ServerName — stock conf has commented "ServerName www.example.com:80"
sudo sed -i.bak "s/#ServerName www.example.com:80/ServerName localhost:80/g" "$conf"
print_ok "Set ServerName to localhost:80"
# DocumentRoot + its <Directory> block (both reference /opt/local/www/apache2/html)
sudo sed -i.bak "s@${PORT_PREFIX}/www/apache2/html@$DOC_ROOT@g" "$conf"
print_ok "Set DocumentRoot to ${DOC_ROOT}"
# Logs — handle both relative ("logs/error_log", ServerRoot-dependent) and absolute forms
sudo sed -i.bak "s@^ErrorLog.*@ErrorLog ${LOGS_DIR}/apache_error.log@" "$conf"
sudo sed -i.bak "s@^CustomLog.*@CustomLog ${LOGS_DIR}/apache_access.log combined@" "$conf"
print_ok "Routed logs to ${LOGS_DIR}"
# mod_rewrite — uncomment both possible stock spellings
sudo sed -i.bak "s@#LoadModule rewrite_module lib/apache2/modules/mod_rewrite.so@LoadModule rewrite_module lib/apache2/modules/mod_rewrite.so@" "$conf"
sudo sed -i.bak "s@#LoadModule rewrite_module modules/mod_rewrite.so@LoadModule rewrite_module modules/mod_rewrite.so@" "$conf"
sudo sed -i.bak "s/AllowOverride None/AllowOverride All/g" "$conf"
print_ok "Enabled mod_rewrite"
# DirectoryIndex
sudo sed -i.bak "s/DirectoryIndex index.html/DirectoryIndex index.php index.html/" "$conf"
print_ok "Added index.php to DirectoryIndex"
# PHP module — REPLACE, never append-only: strip any existing LoadModule
# php*_module line first (fu→D→I cycles leave dead lines pointing at
# uninstalled .so files) then append exactly one line for the current
# PHP_PORT. Idempotent: every run converges to exactly one php module line.
# NOTE: MacPorts' mod_phpXX.so exports "php_module" (no version suffix) —
# the Apache module name is php_module regardless of the port name.
sudo sed -i.bak "/^LoadModule php[0-9]*_module /d" "$conf"
local php_module="${PORT_PREFIX}/lib/apache2/modules/mod_${PHP_PORT}.so"
if [[ -f "$php_module" ]]; then
printf "\nLoadModule php_module %s\n" "$php_module" | sudo tee -a "$conf" > /dev/null
if ! grep -q '<FilesMatch \\.php$>' "$conf"; then
cat << 'PHPFILESMATCH' | sudo tee -a "$conf" > /dev/null