-
Notifications
You must be signed in to change notification settings - Fork 3
/
caddy
928 lines (829 loc) · 26.7 KB
/
caddy
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
#!/bin/bash
#
# Caddy Web Server Installer
# https://github.com/sayem314/Caddy-Web-Server-Installer
#
# shellcheck disable=SC1091,SC1117,SC2015,SC2009,SC2004,SC2116,SC2046,SC2005,SC2034
# Do not change any value after installation
caddypath="/opt/caddyserver" # folder where caddyserver will be kept
caddywww="/var/caddywww" # folder where public files should be
caddyfile="/etc/Caddyfile" # caddy web server configuration file
caddylog="/var/log/caddy" # folder where logs will be written
caddyuser="caddy" # user under caddy is gonna be running
# Load and replace above variable if Caddy.conf exists
[[ -e /etc/Caddy.conf ]] && source /etc/Caddy.conf
# Global config
caddyname="Caddy Web Server"
cwsiv="v6.5"
bashname=$(basename "$0")
pids=$(pgrep -nf $caddypath/caddy)
updateCheck="$caddypath/update.check"
# working directory
cd /opt || echo "Oops! /opt doesn't exist."
about () {
echo ""
echo " ========================================================= "
echo " \ Caddy Web Server Installer / "
echo " \ $cwsiv / "
echo " \ Created by Sayem Chowdhury / "
echo " \ Caddy Web: Caddyserver.com / "
echo " ========================================================= "
echo ""
}
howto () {
echo " Welcome!"
echo " run $(tput setaf 3)$bashname help $(tput sgr0) for help"
echo ""
}
prms () {
echo ""
echo " $bashname start - Start $caddyname."
echo " $bashname stop - Stop/Shutdown $caddyname."
echo " $bashname reload - Reload $caddyname."
echo " $bashname restart - Restart $caddyname."
echo " $bashname status - Check $caddyname is running or not"; echo " or just run '$bashname'."
echo " $bashname install - Install $caddyname on fresh OS."
echo " $bashname install-naked - Install only $caddyname and no prompt."
echo " $bashname edit - Edit Caddyfile using nano or default editor."
echo " $bashname service - Make $caddyname start on boot (upstart/systemd)."; echo " Not required because we install service on install."
echo " $bashname update - Update $caddyname."
echo " $bashname delete - Uninstall $caddyname and data."; echo " We would prompt you if you want to keep data."
echo " $bashname version - Print version information."
echo " $bashname about - About $caddyname Installer."
echo ""
}
isinstalled () {
# check if caddy is installed or return error code 1
if [[ ! -x $caddypath/caddy ]]; then
echo; echo " Caddy is not installed.";
echo " run $(tput setaf 3)$bashname install$(tput sgr0) to install $caddyname";
echo; return 1
fi
}
startcaddy () {
# exit if caddy is not installed
isinstalled || exit 1
# create process id folder if necessary
caddypid="/var/run/caddy"
if [[ ! -f $caddypid ]]; then
mkdir -p $caddypid || (
echo " error creating folder $caddypid"
echo " try running with: sudo $bashname start"
exit 1
) || exit 1
chown $caddyuser $caddypid
fi
# Detect if running or start
pids=$(pgrep -nf $caddypath/caddy)
if [ -z "$pids" ] ; then
echo -n " Starting $caddyname..."
if [[ -e /etc/init/caddy.conf || -e /etc/systemd/system/caddy.service ]]; then
# If service exist use service
hash service 2>/dev/null && service caddy start || systemctl start caddy
else
sudo -H -u $caddyuser $caddypath/caddy -conf="$caddyfile" -quiet=true -pidfile=$caddypid/caddy.pid -agree &
fi
sleep 1
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
sleep 1
# restart or reload php for smooth environment
hash php 2>/dev/null && restartphpfpm;
pids=$(pgrep -nf $caddypath/caddy); caddystatus;
else
echo ""
echo " $caddyname is already $(tput setaf 2)running$(tput sgr0) on: $pids (PID)"
echo ""
fi
}
stopcaddy () {
pids=$(pgrep -nf $caddypath/caddy)
if [ -z "$pids" ] ; then
echo ""
echo " $caddyname is $(tput setaf 1)NOT running$(tput sgr0)"
echo ""
else
secs=$(ps -p "$pids" -oetime= | tr '-' ':' | awk -F: '{ total=0; m=1; } { for (i=0; i < NF; i++) {total += $(NF-i)*m; m *= i >= 2 ? 24 : 60 }} {print total}')
echo -n " Shutting down $caddyname..."
# Wait if Caddy uptime is less than 5 seconds
if [[ $secs -lt 5 ]]; then
sleep 4
fi
if [[ -e /etc/init/caddy.conf || -e /etc/systemd/system/caddy.service ]]; then
# Most arch based linux don't have service
hash service 2>/dev/null && service caddy stop || systemctl stop caddy
else
kill "$pids"
fi
sleep 1
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
fi
}
reloadcaddy () {
if [ -z "$pids" ] ; then
echo ""
echo " $caddyname is $(tput setaf 1)NOT running$(tput sgr0)"
echo " run $(tput setaf 3)$bashname start$(tput sgr0) to start Caddy"
echo ""
else
echo ""
echo -n " Reloading $caddyname..."
if [[ -e /etc/init/caddy.conf || -e /etc/systemd/system/caddy.service ]]; then
# Headless server should have service
hash service 2>/dev/null && service caddy reload || systemctl reload caddy
else
kill -USR1 "$pids"
fi
sleep 2
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
echo ""
echo " To restart, use $(tput setaf 6)$bashname restart$(tput sgr0)"
echo " Refresh your browser at least 3 times to take affect"
echo ""
fi
}
restartphpfpm () {
phpvalue;
# Ubuntu
if [[ -e "$ubuntuphp5fpm" ]]; then
service php5-fpm restart &>/dev/null
# CentOS
elif [[ -e "$centosphpfpm" || -e "$centosphpfpm2" ]]; then
service php-fpm restart &>/dev/null
# PHP 7
elif [[ -e "$phpfpm7" ]]; then
/etc/init.d/php7.*-fpm reload &>/dev/null
fi
}
caddystatus () {
# Check if Caddy is installed or not
isinstalled || exit 1
# Detect Caddy process
if [ -z "$pids" ] ; then
echo ""
echo " $caddyname is $(tput setaf 1)NOT running$(tput sgr0)"
echo ""
else
caddymemusage=$(ps axo rss,comm,pid | grep "$pids" | awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}' | awk '{print $1}')
secs=$(ps -p "$pids" -oetime= | tr '-' ':' | awk -F: '{ total=0; m=1; } { for (i=0; i < NF; i++) {total += $(NF-i)*m; m *= i >= 2 ? 24 : 60 }} {print total}')
if [[ $secs -lt 120 ]]; then
caddyuptime="$secs seconds"
elif [[ $secs -lt 3600 ]]; then
caddyuptime=$(printf '%d minutes %d seconds\n' $(($secs%3600/60)) $(($secs%60)))
elif [[ $secs -lt 86400 ]]; then
caddyuptime=$(printf '%d hrs %d min %d sec\n' $(($secs/3600)) $(($secs%3600/60)) $(($secs%60)))
else
caddyuptime=$(echo $((secs/86400))" days, "$(date -d "1970-01-01 + $secs seconds" "+%H hrs %M min %S sec"))
fi
echo ""
echo " $caddyname is $(tput setaf 2)running$(tput sgr0)"
echo " Caddy (PID: $pids) is using $caddymemusage RAM"
echo " System available RAM $(free -h | grep Mem: | awk '{print $7}')B"
echo " Uptime - $caddyuptime"
echo ""
fi
}
amiroot () {
if [[ "$EUID" -ne 0 ]]; then
echo " Sorry, you need to run this as root"
echo " Try running with command 'sudo'"
exit
fi
}
getcaddy () {
# Detect architecture. We only support linux.
if uname -m | grep -q x86_64; then
cpubits="amd64"
cpubitsname="64bit..."
elif uname -m | grep -q aarch64; then
cpubits="arm64"
cpubitsname="AArch64..."
elif uname -m | grep -q 86; then
cpubits="i386"
cpubitsname="32bit..."
# elif uname -m | grep -q armv5; then
# cpubits="arm5"
# cpubitsname="ARM 5..."
# elif uname -m | grep -q armv6l; then
# cpubits="arm6"
# cpubitsname="ARM 6..."
# elif uname -m | grep -q armv7l; then
# cpubits="arm7"
# cpubitsname="ARM 7..."
else
echo; echo " unsupported or unknown architecture";
echo; exit;
fi
# Downloading Caddy
isupdate="$1"
nocert="--no-check-certificate"
if [[ $isupdate == "update" ]]; then
caddyversion=$($caddypath/caddy -version | awk '{print}')
echo ""; echo " Found $caddyversion";
fi
# Check if plugin should be installed
#if [[ ! $plugin == "" ]]; then
# cpubits="$cpubits?plugins=$plugin&license=personal&telemetry=off"
#echo " Downloading $caddyname with plugin ($plugin) for $cpubitsname" # Caddy linux
#else
# cpubits="$cpubits?license=personal&telemetry=off"
# echo " Downloading $caddyname for $cpubitsname" # Caddy linux
#fi
# Star downloading caddy now https://caddyserver.com/download/linux/$cpubits replace to https://github.com/sztuxp/caddy1.0/raw/master/caddy_linux_$cpubits
if ! wget -q $nocert "https://github.com/sztuxp/caddy1.0/raw/master/caddy_linux_$cpubits" -O "caddy" --show-progress; then
echo " [$(tput setaf 1)FAILED$(tput sgr0)]"
echo ""
echo " Error: Cannot download Caddy binary!"
echo " https://github.com/sztuxp/caddy1.0/raw/master/caddy_linux_$cpubits"
exit 1
fi
echo ""
}
installcaddy () {
# Detetcting if Caddy is installed or not
echo ""
if [[ -e "$caddypath/caddy" ]]; then
echo " $caddyname is already installed on"
echo " $caddypath/caddy"
echo; exit;
fi
# Download caddy
getcaddy;
# Creating folders
mkdir -p $caddypath
mkdir -p $caddylog
# Extract Caddy to appropriate folder
echo -n " Extracting $caddyname to $caddypath..."
chmod 755 caddy
mv caddy $caddypath
#if ! tar xzf caddy_linux_custom.tar.gz -C $caddypath; then
# echo " [$(tput setaf 1)FAILED$(tput sgr0)]"
# echo ""
# echo " Error: Cannot extract 'caddy_linux_custom.tar.gz'"
# echo " Abort."
# exit 1
#fi
echo " $(tput setaf 2)[DONE]$(tput sgr0)"
#rm -f caddy_linux_custom.tar.gz # Deleting Caddy archive
echo ""
# Skip php installation on arch-linux and naked install
# Php installation support for arch-linux will be implemented later
if [[ ! $1 == naked ]]; then
hash pacman 2>/dev/null || installphp
fi
# Creating non-root user
if id -u $caddyuser >/dev/null 2>&1; then
echo " User '$caddyuser' already exists. Skipped!"
else
echo -n " Creating user '$caddyuser'..."
useradd -r -d $caddypath -s /bin/false $caddyuser &> /dev/null
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
fi
chown -R $caddyuser $caddypath
chown -R $caddyuser $caddylog
# Port setup
echo -n " Binding port using setcap..."
if hash setcap 2>/dev/null; then
setcap cap_net_bind_service=+ep $caddypath/caddy &>/dev/null && echo " [$(tput setaf 2)DONE$(tput sgr0)]" || echo " [$(tput setaf 1)FAILED$(tput sgr0)]"
elif hash apt-get 2>/dev/null; then
apt-get install libcap2-bin -y &>/dev/null
setcap cap_net_bind_service=+ep $caddypath/caddy &>/dev/null && echo " [$(tput setaf 2)DONE$(tput sgr0)]" || echo " [$(tput setaf 1)FAILED$(tput sgr0)]"
else
echo " [$(tput setaf 1)FAILED$(tput sgr0)]"
fi
# Show succes message on naked install
if [[ $1 == naked ]]; then
echo ""
echo " Installed $caddyname to $caddypath"
echo ""
fi
}
compareVersion () {
IFS='.' read -ra ver1 <<< "$1"
IFS='.' read -ra ver2 <<< "$2"
[[ ${#ver1[@]} -gt ${#ver2[@]} ]] && till=${#ver1[@]} || till=${#ver2[@]}
for ((i=0; i<${till}; i++)); do
local num1; local num2;
[[ -z ${ver1[i]} ]] && num1=0 || num1=${ver1[i]}
[[ -z ${ver2[i]} ]] && num2=0 || num2=${ver2[i]}
if [[ $num1 -gt $num2 ]]; then
echo ">"
return 0
elif
[[ $num1 -lt $num2 ]]; then
echo "<"
return 0
fi
done
echo "="
return 0
}
updatecaddy () {
# Detetcting Caddy installed or not
isinstalled || exit 1
echo;
# Check if there is newer version available
caddyversion=$($caddypath/caddy -version | sed -e 's/-/ /g' -e 's/[^0-9\. ]*//g' | awk '{print $1}')
CADDYV=$(wget -qO- https://api.github.com/repos/mholt/caddy/releases/latest | grep tag_name | sed 's/[^0-9\.]*//g')
isGreater=$(compareVersion $CADDYV $caddyversion)
if [[ $isGreater != ">" ]]; then
echo " Looks like you already have latest version installed."
sleep 0.1
echo " Abort.";
sleep 0.1
exit;
fi
# Download caddy
getcaddy update;
sleep 0.1
# Stop current process if found
if [ ! -z "$pids" ] ; then
echo ""
echo " Found Caddy is running!"
stopcaddy;
fi
# Delete Caddy folder
echo -n " Removing current $caddyname from $caddypath"
sleep 0.2
rm -rf "${caddypath:?}/"*
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
sleep 0.1
# Extract Caddy to appropriate folder
echo -n " Extracting $caddyname to $caddypath..."
if ! tar xzf caddy_linux_custom.tar.gz -C $caddypath; then
echo " [$(tput setaf 1)FAILED$(tput sgr0)]"
echo ""
echo " Error: Cannot extract 'caddy_linux_custom.tar.gz'"
echo " Abort."
exit 1
fi
echo " $(tput setaf 2)[DONE]$(tput sgr0)"
# Granting owner access and required permission
if id -u $caddyuser >/dev/null 2>&1; then
chown -R $caddyuser $caddypath
fi
# Port setup
hash setcap 2>/dev/null && setcap cap_net_bind_service=+ep $caddypath/caddy &>/dev/null
# Removing caddy archive and printing info
echo $(date +%s) > $updateCheck
rm -f caddy_linux_custom.tar.gz # Deleting Caddy archive
caddyversion=$($caddypath/caddy -version | awk '{print}')
echo; echo " Updated to $caddyversion"; echo;
}
installphp () {
if [[ -e "$caddypath" ]] ; then
read -r -p " Are you gonna run php site? [y/N]: " -e -i y option
case $option in
[yY][eE][sS]|[yY])
echo -n " Installing required php files..."
# Detecting apt-get/yum
if hash apt-get 2>/dev/null; then
CODENAME=$(cat /etc/*-release | grep "VERSION_ID=" | cut -f1 -d'.'| cut -f2 -d'"')
DEBIAN9=$(lsb_release -i | awk 'NF{ print $NF }')
# If debian 9 or above select php7
if [[ $DEBIAN9 == Debian ]]; then
DEBIAN9V=$(lsb_release -r | awk 'NF{ print $NF }' | cut -f1 -d'.')
CODENAME="0"
fi
fi
# Setup PHP
if [[ $CODENAME -gt 15 ]] || [[ $DEBIAN9V -eq 9 ]]; then
PHP="php7.0"
elif [[ $DEBIAN9V -ge 10 ]]; then
PHP="php"
else
PHP="php5"
fi
# Install PHP
if hash yum 2>/dev/null; then
yum update -y &>/dev/null && yum install sed php-fpm php-cli php-curl curl sudo -y &>/dev/null
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
echo " Installed 'php-fpm php-cli php-curl curl sudo'"
fixphpfpm;
elif hash apt-get 2>/dev/null; then
apt-get update -y &>/dev/null && apt-get install sed $PHP-fpm $PHP-cli $PHP-curl curl sudo -y &>/dev/null
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
echo " Installed '$PHP-fpm $PHP-cli $PHP-curl curl sudo'"
fixphpfpm;
else
echo " [$(tput setaf 1)FAILED$(tput sgr0)]"
fi
;;
*)
echo " PHP installation skipped!"
;;
esac
else
echo " Looks like Caddy was not installed!"
fi
}
phpvalue () {
ubuntuphp5fpm="/etc/php5/fpm/pool.d/www.conf"
centosphpfpm="/etc/php/fpm/pool.d/www.conf"
centosphpfpm2="/etc/php-fpm.d/www.conf"
phpfpm7=$(echo /etc/php/7.*/fpm/pool.d/www.conf)
}
fixphpfpm () {
phpvalue;
phpuser=$1
if [[ $phpuser == "" ]]; then
phpuser=$caddyuser
fi
# Detect php5-fpm or php-fpm
echo -n " Fixing php sock issue..."
#Ubuntu
if [[ -e "$ubuntuphp5fpm" ]] ; then
sed -i "s@user = .*@user = $phpuser@g" $ubuntuphp5fpm
sed -i "s@group = .*@group = $phpuser@g" $ubuntuphp5fpm
sed -i 's/listen = .*/listen = 127.0.0.1:9000/g' $ubuntuphp5fpm
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
# CentOS
elif [[ -e "$centosphpfpm" ]] ; then
sed -i "s@user = .*@user = $phpuser@g" $centosphpfpm
sed -i "s@group = .*@group = $phpuser@g" $centosphpfpm
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
# CentOS 2
elif [[ -e "$centosphpfpm2" ]] ; then
sed -i "s@user = .*@user = $phpuser@g" $centosphpfpm2
sed -i "s@group = .*@group = $phpuser@g" $centosphpfpm2
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
# PHP 7
elif [[ -e "$phpfpm7" ]] ; then
sed -i "s@user = .*@user = $phpuser@g" $phpfpm7
sed -i "s@group = .*@group = $phpuser@g" $phpfpm7
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
else
echo " [$(tput setaf 1)FAILED$(tput sgr0)]"
echo " php not found!"
echo ""
fi
}
caddyfile () {
phpvalue;
# Ubuntu
if [[ -e "$ubuntuphp5fpm" ]] ; then
phpsock=$(grep "listen = *" < $ubuntuphp5fpm | cut -c 10-)
# CentOS
elif [[ -e "$centosphpfpm" ]] ; then
phpsock=$(grep "listen = *" < $centosphpfpm | cut -c 10-)
# CentOS 2
elif [[ -e "$centosphpfpm2" ]] ; then
phpsock=$(grep "listen = *" < $centosphpfpm2 | cut -c 10-)
# PHP 7
elif [[ -e "$phpfpm7" ]] ; then
sed -i 's/listen = .*/listen = 127.0.0.1:9000/g' $phpfpm7
phpsock=$(grep "listen = *" < $phpfpm7 | cut -c 10-)
else
phpsock="127.0.0.1:9000"
fi
# Check if Caddyfile exist
# Generate Caddyfile
if [[ ! -e $caddyfile ]]; then
echo ""
echo " Enter your domain name"
echo " Seperated by comma"
read -r -p " Domain name: " domainname
echo ""
# Check if gonna run locally
if [[ $domainname == "localhost" || $domainname == "localhost:*" || $domainname == "127.0.0*" || $domainname == "192.168*" ]]; then
echo " SSL is disabled on non-production server."
else
echo " Enter your email for automated ssl"
read -r -p " Email: " domainmail
until [[ "$domainmail" == *@*.* || "$domainmail" == off ]]; do
echo ""
echo " Invalid email"
echo " Type $(tput setaf 3)off$(tput sgr0) if you don't want https"
read -r -p " Email: " domainmail
done
fi
echo "$domainname {
root $caddywww
gzip
tls $domainmail
log $caddylog/access.log
errors $caddylog/error.log
fastcgi / $phpsock php
}
" >> $caddyfile
echo ""
echo " $caddyfile created"
else
echo " $caddyfile found. Skipped! "
fi
# Check if caddywww exist
if [[ ! -e $caddywww ]]; then
mkdir -p $caddywww
fi
# Generate example html
# shellcheck disable=SC1078,SC1079,SC2140
if [[ ! -e $caddywww/index.html ]]; then
cat <<EOF > $caddywww/index.html
<TITLE>Caddy Default Page</TITLE>
<h1><font color="green">It works!</font></h1>
<h3><font color="grey">This is the default welcome page used to test the correct operation of the Caddy web server after installation via Caddy Web Server Installer.</font></h3>
<h3><font color="grey">If you can read this page, it means that the Caddy web server installed at this site is working properly.<br>
You should <font color="#D03C3C">replace this file</font>
(located at <tt>$caddywww/index.html</tt>) before continuing to operate your server.</font></h3>
</hr>
<h3><font color="grey">If you are a normal user of this web site and don't know what this page is about, this probably means that the site is currently unavailable due to maintenance.</font></h3>
EOF
echo " index file created"
echo " $caddywww/index.html"
else
echo " index.html found. Skipped!"
fi
# phpinfo
if [[ ! -e $caddywww/info.php ]]; then
echo "<?php phpinfo(); ?>" >> $caddywww/info.php
echo ""
echo " PHP-info file created"
echo " $caddywww/info.php"
fi
chown -R $caddyuser $caddywww
echo ""
echo " Done. Caddy has been installed :)"
echo " Run $(tput setaf 3)$bashname start$(tput sgr0) to start $caddyname"
echo ""
}
makeservice () {
if [[ -e /etc/systemd/system/caddy.service || -e /etc/init.d/caddy.sh ]]; then
echo " Service already exists. Skipped!"
else
nocert="--no-check-certificate"
init=$(cat /proc/1/comm)
echo -n " Creating startup service..."
if [ "$init" == 'systemd' ]; then
MAIN="$"
MAINPID="MAINPID"
rm -f /etc/systemd/system/caddy.service
cat <<EOF > /etc/systemd/system/caddy.service
[Unit]
Description=Caddy HTTP/2 web server
Documentation=https://caddyserver.com/docs
After=network-online.target
Requires=network-online.target
[Service]
User=$caddyuser
StartLimitInterval=86400
StartLimitBurst=5
LimitNOFILE=16535
ExecStart=$caddypath/caddy -conf=$caddyfile -quiet=true -pidfile=/var/run/caddy/caddy.pid -agree
ExecReload=/bin/kill -USR1 $MAIN$MAINPID
ExecStop=/bin/kill $MAIN$MAINPID
PIDFile=/var/run/caddy/caddy.pid
[Install]
WantedBy=multi-user.target
EOF
chmod 0644 /etc/systemd/system/caddy.service
echo " $(tput setaf 2)DONE$(tput sgr0)"
echo ""
systemctl daemon-reload
systemctl enable caddy
else
# Download
wget -q $nocert "https://raw.githubusercontent.com/sayem314/Caddy-Web-Server-Installer/master/php-fpm/runcaddy.sh" -O "/etc/init.d/caddy.sh"
chmod +x /etc/init.d/caddy.sh
# Enable
if hash yum 2>/dev/null; then
chkconfig caddy.sh on
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
elif hash apt-get 2>/dev/null; then
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
update-rc.d caddy.sh defaults
else
echo " [$(tput setaf 1)FAILED$(tput sgr0)]"
fi
fi
fi
}
deletecaddy () {
if [[ -e "$caddypath" ]] ; then
echo ""
echo " Are you sure you want to uninstall Caddy web server? [y/N]"
read -r -p " Select an option: " option
case $option in
[yY][eE][sS]|[yY])
# Stop caddy if running
if [ ! -z "$pids" ] ; then
echo " Found Caddy is running!"
stopcaddy;
sleep 2
fi
sleep 1
# Disable and remove startup script if exist
removeservice;
# Remove Caddy web server folder, usually on /opt/caddyserver.
rm -rf $caddypath &>/dev/null
# Backup Caddyfile if exists, usually on /etc/Caddyfile.
if [[ -e $caddyfile ]]; then
cp $caddyfile $caddyfile.bak
echo " Backup of current Caddyfile saved to $caddyfile.bak"
fi
# Now remove Caddyfile
rm -f $caddyfile &>/dev/null
# Prompt if data should be removed
if [[ -d "$caddywww" ]]; then
echo " $caddyname has been deleted."
deletecaddydata;
else
# remove caddy user
if [[ ! $caddyuser == $(whoami) ]]; then
if id -u $caddyuser >/dev/null 2>&1; then
hash php 2>/dev/null && fixphpfpm www-data;
restartphpfpm;
userdel $caddyuser # delete caddy user
echo " $caddyname and Caddy user '$caddyuser' has been deleted."
fi
fi
fi
echo ""
exit
;;
[nN][oO]|[nN])
echo " Abort."
exit;
;;
*) echo ""; echo " Incorrect input, exiting! "; exit;;
esac
else
echo ""
echo " Looks like Caddy is not installed!"
fi
echo ""
}
removeservice () {
# Disable and remove startup script if exist
if [[ -e /etc/init/caddy.conf || -e /etc/systemd/system/caddy.service ]]; then
init=$(cat /proc/1/comm)
echo -n " Removing startup service..."
if [ "$init" == 'systemd' ]; then
systemctl disable caddy &>/dev/null
rm -f /etc/systemd/system/caddy.service &>/dev/null
elif [ "$init" == 'init' ]; then
update-rc.d -f caddy.sh remove
rm -f /etc/init/caddy.conf &>/dev/null
fi
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
fi
}
deletecaddydata () {
totalsize=$(du -sh $caddywww)
totalfiles=$(find $caddywww -type f | wc -l)
echo ""
echo " Would you also like to delete data?"
echo " Size: $totalsize (Files: $totalfiles)"
read -r -p " Select an option [y/N]: " option
echo ""
case $option in
[yY][eE][sS]|[yY])
echo -n " Deleting $caddywww (data)..."
sleep 2
rm -rf $caddywww # deleting caddy html folder
echo " [$(tput setaf 2)DONE$(tput sgr0)]"
# remove caddy user too if exists
if [[ ! $caddyuser == $(whoami) ]]; then
if id -u $caddyuser >/dev/null 2>&1; then
hash php 2>/dev/null && fixphpfpm www-data;
hash php 2>/dev/null && restartphpfpm;
userdel $caddyuser # delete caddy user
echo " Caddy user '$caddyuser' has been deleted too."
fi
fi
;;
*)
echo " Removing $caddywww (data)... [$(tput setaf 5)Skipped$(tput sgr0)]"
;;
esac
}
versioninfo () {
echo ""
if [[ -e $caddypath/caddy ]]; then
echo " $($caddypath/caddy -version)"
else
echo " $caddyname is not installed."
fi
echo " $caddyname Installer - $cwsiv"
if hash php 2>/dev/null; then
echo " $(php -v | grep cli)"
fi
echo ""
}
# Edit Caddyfile using nano or default editor
editcaddyfile (){
if hash nano 2>/dev/null; then
echo " opening $caddyfile using nano"
nano $caddyfile
elif hash editor 2>/dev/null; then
editor $caddyfile
else
echo " error: please manually edit files"
echo " $caddyfile"
fi
}
checkforupdate () {
# Check if there is newer version available
# Currenly only checks for caddy web server and not this script
caddyversion=$($caddypath/caddy -version | sed -e 's/-/ /g' -e 's/[^0-9\. ]*//g' | awk '{print $1}')
CADDYV=$(wget -qO- https://api.github.com/repos/mholt/caddy/releases/latest | grep tag_name | sed 's/[^0-9\.]*//g')
isGreater=$(compareVersion $CADDYV $caddyversion)
if [[ $CADDYV != "" && $isGreater == ">" ]]; then
echo ""
echo " $(tput setaf 1)NOTICE:$(tput sgr0)"
echo " There is an update available of $caddyname"
echo " run $(tput setaf 3)$bashname update$(tput sgr0) to update"
echo ""
fi
}
backupcaddy () {
backupname="caddybackup-$(date +"%Y%m%d%H%M").tar.gz"
echo " Please wait! Creating backup $backupname"
echo " $(du -sh $caddypath)"
echo " $(du -sh $caddyfile)"
tar -czf "$backupname" $caddypath $caddyfile --absolute-names || ( echo " error creating backup $backupname"
echo " Abort."
rm -f "$backupname"; exit )
echo " Backup finished."
echo " $(du -sh "$backupname")"
exit
}
restorecaddy () {
if [[ ! -x $caddypath/caddy ]]; then
echo " $(tput setaf 1)WARN:$(tput sgr0)"
echo " Caddy is not installed."
echo " It is recommended to install Caddy before restoring."
read -r -p " Press enter to continue anyway!"
fi
echo -n " Please provide backup name: "
read -r -t 15 backupname || exit
echo " Please wait! restoring backup $backupname"
if [[ -e $backupname ]]; then
echo " $(du -sh "$backupname")"
tar -xzf "$backupname" --absolute-names || ( echo " error restoring backup $backupname"
echo " Abort."; exit )
echo " Restore finished."
else
echo " $backupname not found on $(pwd)"
echo " Abort."
fi
exit
}
# See how we were called.
case $1 in
'-start'|'--start'|'start'|'startcaddy' )
startcaddy;;
'-stop'|'--stop'|'stop'|'stopcaddy' )
stopcaddy;;
'-reload'|'--reload'|'reload'|'reloadcaddy' )
reloadcaddy;;
'-restart'|'--restart'|'restart' )
stopcaddy; sleep 2; startcaddy;;
'-status'|'--status'|'status'|'caddystatus' )
caddystatus;;
'-install'|'--install'|'install' )
amiroot; plugin="$2"; installcaddy; makeservice; caddyfile;;
'-install-naked'|'--install-naked'|'install-naked' )
amiroot; plugin="$2"; installcaddy naked;;
'-update'|'--update'|'update'|'updatecaddy' )
amiroot; plugin="$2"; updatecaddy; caddyfile;;
'-fixphp'|'--fixphp'|'fixphp'|'fixphpfpm' )
amiroot; fixphpfpm;;
'-installphp'|'--installphp'|'installphp' )
amiroot; installphp;;
'-edit'|'edit'|'editcaddyfile' )
amiroot; editcaddyfile;;
'-service'|'--service'|'service'|'makeservice' )
amiroot; makeservice;;
'-del'|'delete'|'--delete'|'-rm'|'deletecaddy'|'-uninstall'|'uninstall' )
amiroot; deletecaddy;;
'delete-service'|'--delete-service'|'remove-startup'|'--remove-startup' )
amiroot; removeservice;;
'-backup'|'--backup'|'backup' )
backupcaddy;;
'-restore'|'--restore'|'restore' )
restorecaddy;;
'-about'|'about'|'--about' )
about;;
'-h'|'help'|'-help'|'--help' )
prms;;
'-v'|'version'|'-version'|'--version'|'versioninfo'|'-V' )
versioninfo;;
*)
caddystatus; howto;;
esac
# check updates every 48hrs
if [[ -e "$caddypath/caddy" ]]; then
currentDays=$(date +%s)
# Create update.check file if not exist
if [[ ! -e $updateCheck ]]; then
echo $currentDays > $updateCheck
chmod 777 $updateCheck
fi
lastCheckDays=$(cat $updateCheck)
updateDays="$(($currentDays - $lastCheckDays))"
# if more than 48hrs execute update check
if [[ $updateDays -gt 172800 ]]; then
checkforupdate $currentDays $updateCheck;
fi
fi
# exit now!
exit 0