forked from khs1994-docker/lnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lnmp-docker
executable file
·1928 lines (1498 loc) · 53.4 KB
/
lnmp-docker
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
# Don't Run this shell script on git bash Windows, please use ./lnmp-docker.ps1
# sudo ? only need by install docker-compose
set -e
tty=
tty -s > /dev/null 2>&1 && tty=--tty || true
readonly KUBERNETES_VERSION="1.19.3"
readonly DOCKER_DESKTOP_VERSION="2.5.1.0"
readonly OS=`uname -s`
readonly ARCH=`uname -m`
# only run on macOS and Linux
if [ $OS != 'Darwin' ] && [ $OS != 'Linux' ];then \
echo -e "\n\033[31mError \033[0m Please use ./lnmp-docker.ps1 on PowerShell in Windows"; exit 1; fi
std="stderr"
if [ "$quite" = "true" -o "$QUITE" = "true" -o "$1" = "services" -o "$1" = "env-file" ];then
std="null"
fi
print_info(){
echo -e "\033[32mINFO \033[0m $@\n" > /dev/${std}
}
print_error(){
echo -e "\033[31mERROR\033[0m \a $@\n" > /dev/${std}
export exit_by_error=1
}
print_warning(){
echo -e "\033[33mWARNING\033[0m \a $@\n" > /dev/${std}
}
notsupport(){
print_error "Not Support ${OS} ${ARCH}\n" > /dev/${std}
exit 1
}
_sudo(){
command -v sudo > /dev/null && echo "sudo" || true
}
_cp_only_not_exists(){
if ! [ -f $2 ];then cp $1 $2 ; fi
}
export EXEC_CMD_DIR=`pwd`
ScriptRoot="$( cd "$( dirname "$0" )" && pwd )"
if [ -f cli/khs1994-robot.enc ];then
print_info "Use LNMP CLI in LNMP Root $PWD"
else
print_info "Use LNMP CLI in $PWD"
cd ${ScriptRoot}
fi
if ! [ -f cli/khs1994-robot.enc ];then
exit 1
fi
LNMP_ENV_FILE=".env"
if [ -f ".env.$LNMP_ENV" ];then
LNMP_ENV_FILE=".env.$LNMP_ENV"
fi
print_info "Load env file [ $LNMP_ENV_FILE ]"
_cp_only_not_exists docker-lnmp.include.example.yml docker-lnmp.include.yml
_cp_only_not_exists docker-workspace.example.yml docker-workspace.yml
_cp_only_not_exists lnmp-docker-custom-script.example lnmp-docker-custom-script
print_info "Exec custom scripts"
. ./lnmp-docker-custom-script
_cp_only_not_exists config/php/php.development.ini config/php/php.ini
_cp_only_not_exists config/php/docker-php.example.ini config/php/docker-php.ini
_cp_only_not_exists config/php/zz-docker.example.conf config/php/zz-docker.conf
if ! [ -f config/redis/redis.conf ];then
echo "#" > config/redis/redis.conf
fi
_cp_only_not_exists config/php8/php.development.ini config/php8/php.ini
_cp_only_not_exists config/php8/docker-php.example.ini config/php8/docker-php.ini
_cp_only_not_exists config/php8/zz-docker.example.conf config/php8/zz-docker.conf
_cp_only_not_exists config/composer/config.example.json config/composer/config.json
_cp_only_not_exists config/composer/.env.example config/composer/.env
_cp_only_not_exists config/crontabs/root.example config/crontabs/root
_cp_only_not_exists config/npm/.npmrc.example config/npm/.npmrc
_cp_only_not_exists config/npm/.env.example config/npm/.env
_cp_only_not_exists config/yarn/.yarnrc.example config/yarn/.yarnrc
_cp_only_not_exists config/yarn/.env.example config/yarn/.env
_cp_only_not_exists config/registry/config.example.yml config/registry/config.yml
help(){
echo -e "
Docker-LNMP CLI ${LNMP_DOCKER_VERSION}
Official WebSite https://lnmp.khs1994.com
Usage: ./docker-lnmp COMMAND
PCIT EE:
pcit-up Up(Run) PCIT EE https://github.com/pcit-ce/pcit
pcit-cp Copy PCIT files to app/.pcit
Commands:
up Up LNMP (Support x86_64 arm64v8)
down Stop and remove LNMP Docker containers, networks
acme.sh Run original acme.sh command to issue SSL certificate
backup Backup MySQL databases
build Build or rebuild your LNMP images (Only Support x86_64)
build-config Validate and view the LNMP with your images Compose file
build-up Create and start LNMP containers With your Build images
build-push Pushes your images to Docker Registory
build-pull Pull LNMP Docker Images Build By your self
build-up Create and start LNMP containers With Self Build images
cleanup Cleanup log files
compose Install docker-compose [ --official | -f ]
composer Exec composer command on Docker Container
completion Move fish shell completion code (Only Support fish)
config Validate and view the LNMP Compose file
bug Generate Debug information, then copy it to GitHub Issues
daemon-socket Expose Docker daemon on tcp://0.0.0.0:2375 without TLS on macOS
help Display this help message
nfs Up NFS server [ down | ]
pull Pull LNMP Docker Images
restore Restore MySQL databases
restart Restart LNMP services
services List services
update Upgrades LNMP [ -f | ]
upgrade Upgrades LNMP [ -f | ]
code-init Init vsCode remote development env
code-run Run command on vsCode remote workspace
code-exec Exec command on vsCode remote workspace
lrew(package):
lrew-init Init a new lrew package
lrew-add Add new lrew package
lrew-outdated Shows a list of installed lrew packages that have updates available
lrew-backup Upload composer.json to GitHub Gist
lrew-update Update lrew package
PHP Tools:
new New PHP Project and generate nginx conf and issue SSL certificate
httpd-config Generate Apache2 vhost conf
nginx-config Generate nginx vhost conf
ssl Issue SSL certificate powered by acme.sh, Thanks Let's Encrypt
ssl-self Issue Self-signed SSL certificate
Composer:
satis Build Satis
Kubernets:
gcr.io Up local gcr.io registry server to start Docker Desktop Kubernetes [ --no-pull | ]
Swarm mode:
swarm-build Build Swarm mode LNMP images (nginx php7)
swarm-config Validate and view the Swarm mode Compose file
swarm-deploy Deploy LNMP stack IN Swarm mode
swarm-down Remove LNMP stack IN Swarm mode
swarm-ps List the LNMP tasks
swarm-pull Pull LNMP Docker Images IN Swarm mode
swarm-push Push Swarm mode LNMP images (nginx php7)
swarm-update Print update LNMP service example
Container Tools:
SERVICE-cli Execute a command in a running LNMP container
SERVICE-container-id Display SERVICE container id
SERVICE-logs Print LNMP containers logs (journald) [-f | ]
ClusterKit:
clusterkit-help Print ClusterKit help info
Developer Tools:
test Test LNMP
Read './docs/*.md' for more information about CLI commands.
You can open issue in [ https://github.com/khs1994-docker/lnmp/issues ] when you meet problems.
You must Update $LNMP_ENV_FILE file when update this project.
Donate https://zan.khs1994.com
AD Tencent Kubernetes Engine https://cloud.tencent.com/act/cps/redirect?redirect=10058&cps_key=3a5255852d5db99dcd5da4c72f05df61
"
}
_tongji(){
if ! [ -f log/tongji.txt ];then
print_info "Send OS type and IP data to khs1994-docker/lnmp, Thanks you help us improve"
if [ -f /etc/os-release ];then . /etc/os-release ; fi
curl -s -X GET --user-agent "${OS:-unkown os}-${ID:-unknow id}-${VERSION:-unkown version}" https://developer.khs1994.com/lnmp/tongji --connect-timeout 5 > /dev/null || echo 'ERROR=true' > log/tongji.txt
TIMESTAMP=`date +%s`
echo "TIMESTAMP=${TIMESTAMP}" > log/tongji.txt
else
. ./log/tongji.txt
if [ `date +%s` -gt $((TIMESTAMP + 86400)) ];then
rm -rf log/tongji.txt
_tongji
fi
fi
}
test -f cluster/help.sh && source cluster/help.sh || true
# docker for mac local gcr.io server
_gcr_io_down(){
docker container rm -f $(docker container ls -a -f label=com.khs1994.lnmp.gcr.io -q) > /dev/null 2>&1 || true
}
_test_gcr_image(){
result=$(docker image ls -q k8s.gcr.io/$1)
if [ -n "$result" ];then
return 0
fi
return 1
}
_get_gcr_image(){
docker pull $2/$1
docker tag $2/$1 k8s.gcr.io/$1
# docker push k8s.gcr.io/$image
docker rmi $2/$1
}
_gcr_io(){
if [ "$1" = down ];then
_gcr_io_down
print_info "Stop gcr.io local server success"
exit
fi
if [ "$1" = logs ];then
exec docker container logs $(docker container ls -f label=com.khs1994.lnmp.gcr.io -q) -f
fi
_gcr_io_down
print_info "This local server support Docker Desktop v${DOCKER_DESKTOP_VERSION} with Kubernetes v${KUBERNETES_VERSION}"
mkdir -p $HOME/.khs1994-docker-lnmp/registry
docker run -i ${tty} -d \
-p 443:443 \
-p 80:80 \
--mount type=bind,src=$PWD/config/registry/config.gcr.io.yml,target=/etc/docker/registry/config.yml \
--mount type=bind,src=$PWD/config/registry,target=/etc/docker/registry/ssl \
--mount type=bind,src=$HOME/.khs1994-docker-lnmp/registry,target=/var/lib/registry:cached \
--label com.khs1994.lnmp.gcr.io \
registry || quit=1
# -v $PWD/config/registry/nginx.htpasswd:/etc/docker/registry/auth/nginx.htpasswd \
if [ "$quit" = 1 ];then
print_error "Please stop soft who bind 443 port first"
exit 1
fi
if [ "$1" = '--no-pull' ];then
print_info "Up gcr.io Server Success"
return
fi
images="kube-controller-manager:v${KUBERNETES_VERSION} \
kube-apiserver:v${KUBERNETES_VERSION} \
kube-scheduler:v${KUBERNETES_VERSION} \
kube-proxy:v${KUBERNETES_VERSION} \
etcd:3.4.13-0 \
etcd:3.3.15-0 \
coredns:1.7.0 \
pause:3.2 \
pause:3.1"
sleep 5
set +e
for image in $images
do
print_info "Handle ${image} ..."
_test_gcr_image $image
if [ $? = 0 ];then
print_info "k8s.gcr.io/${image} exists"
continue
fi
_get_gcr_image $image gcr.io/google_containers
# 一些镜像 aliyun 可能不存在,从第二个镜像下载
_test_gcr_image $image
if [ $? = 0 ];then
continue
fi
print_error "Download from mirror error, try other mirror"
_get_gcr_image $image ccr.ccs.tencentyun.com/gcr-mirror
done
_gcr_io_down
}
# composer satis
_satis(){
if ! [ -d ${APP_ROOT}/satis ];then
cp -r app/satis-demo ${APP_ROOT}/satis
print_warning "Please modify app/satis/satis.json"
fi
docker run --rm -i ${tty} --mount type=bind,src=${APP_ROOT}/satis,target=/build \
--mount type=volume,src=lnmp_composer-cache-data,target=/composer composer/satis
}
_get_compose_options(){
local isBuild=0
for compose_file in "$@"
do
if [ $compose_file = "--build" ];then
isBuild=1
continue
fi
options+=" -f $compose_file "
done
for compose_file in $LREW_INCLUDE
do
# COMPOSE_FILE=$(echo $compose_file | tr '[a-z]' '[A-Z]')
# local PACKAGE=$(echo ${compose_file^^} | tr '-' '_')
local PACKAGE=$(echo $compose_file | tr '[a-z]' '[A-Z]' | tr '-' '_')
if [ -d vendor/lrew-dev/$compose_file ];then
LREW_INCLUDE_ROOT="vendor/lrew-dev/$compose_file"
# set env
cat $LNMP_ENV_FILE | grep -q "LREW_${PACKAGE}_VENDOR=lrew-dev" && true || echo "LREW_${PACKAGE}_VENDOR=lrew-dev" >> $LNMP_ENV_FILE
elif [ -d vendor/lrew/$compose_file ];then
LREW_INCLUDE_ROOT="vendor/lrew/$compose_file"
# unset env
sed -i "s#^LREW_${PACKAGE}_VENDOR.*##g" $LNMP_ENV_FILE
elif [ -d lrew/$compose_file ];then
LREW_INCLUDE_ROOT="lrew/$compose_file"
else
continue
fi
if [ $isBuild -eq 1 ];then
if ! [ -f "$LREW_INCLUDE_ROOT/docker-compose.build.yml" ];then
options+=" -f $LREW_INCLUDE_ROOT/docker-compose.yml "
continue
fi
options+=" -f $LREW_INCLUDE_ROOT/docker-compose.yml -f $LREW_INCLUDE_ROOT/docker-compose.build.yml "
continue
fi
if ! [ -f "$LREW_INCLUDE_ROOT/docker-compose.override.yml" ];then
options+=" -f $LREW_INCLUDE_ROOT/docker-compose.yml "
continue
fi
options+=" -f $LREW_INCLUDE_ROOT/docker-compose.yml -f $LREW_INCLUDE_ROOT/docker-compose.override.yml "
done
options+=" --env-file $LNMP_ENV_FILE "
options+=" -f docker-lnmp.include.yml "
}
env_status(){
# cp .env.example to .env
if [ -f .env ];then \
print_info ".env file existing"; \
else print_warning ".env file NOT existing, Maybe First Run"; cp .env.example .env ; fi
_cp_only_not_exists kubernetes/nfs-server/.env.example kubernetes/nfs-server/.env
_cp_only_not_exists secrets/minio/key.example.txt secrets/minio/key.txt
_cp_only_not_exists secrets/minio/secret.example.txt secrets/minio/secret.txt
_cp_only_not_exists config/supervisord/supervisord.ini.example config/supervisord/supervisord.ini
}
# 自动升级软件版本
update_version(){
. ./.env.example
local softs='PHP \
NGINX \
HTTPD \
MYSQL \
MARIADB \
REDIS \
MEMCACHED \
RABBITMQ \
POSTGRESQL \
MONGODB \
'
for soft in $softs; do
version="LNMP_${soft}_VERSION"
eval version=$(echo \$$version)
if [ $OS = "Darwin" ];then
sed -i '' 's/^LNMP_'"${soft}"'_VERSION.*/LNMP_'"${soft}"'_VERSION='"${version}"'/g' $LNMP_ENV_FILE
else
sed -i 's/^LNMP_'"${soft}"'_VERSION.*/LNMP_'"${soft}"'_VERSION='"${version}"'/g' $LNMP_ENV_FILE
fi
done
}
# Docker 是否运行
_docker_is_run(){
docker info > /dev/null 2>&1 || (clear ; \
echo "==========================" ;\
echo "=== Please Run Docker ====" ;\
echo "==========================" ;\
exit 1)
}
# 创建日志文件
logs(){
if ! [ -f log/supervisord.log ];then touch log/supervisord.log; fi
if ! [ -d log/supervisord ];then mkdir -p log/supervisord; fi
if ! [ -d log/httpd ];then mkdir -p log/httpd; fi
if ! [ -d log/mongodb ];then mkdir -p log/mongodb && touch log/mongodb/mongo.log; fi
if ! [ -d log/mysql ];then mkdir -p log/mysql && touch log/mysql/error.log; fi
if ! [ -d log/mariadb ];then mkdir -p log/mariadb && touch log/mariadb/error.log; fi
if ! [ -d log/nginx ];then mkdir -p log/nginx && touch log/nginx/{error.log,access.log}; fi
if ! [ -d log/nginx-unit ];then mkdir -p log/nginx-unit; fi
if ! [ -d log/php ];then
mkdir -p log/php \
&& touch log/php/{error.log,php-fpm-slow.log,php-fpm-access.log,php-fpm-error.log,xdebug-remote.log}
fi
if ! [ -d log/redis ];then mkdir -p log/redis && touch log/redis/redis.log ; fi
chmod -R 777 log/{mongodb,mysql,nginx,php,redis} \
|| $(_sudo) chmod -R 777 log/{mongodb,mysql,nginx,php,redis}
}
# 清理日志文件
cleanup(){
logs \
&& echo > log/mongodb/mongo.log \
&& echo > log/mysql/error.log \
&& echo > log/mariadb/error.log \
&& echo > log/nginx/error.log \
&& echo > log/nginx/access.log \
&& echo > log/nginx-unit/nginx-unit.log \
&& echo > log/nginx-unit/access.log \
&& echo > log/php/php-fpm-access.log \
&& echo > log/php/php-fpm-error.log \
&& echo > log/php/error.log \
&& echo > log/php/php-fpm-slow.log \
&& echo > log/php/xdebug-remote.log \
&& echo > log/redis/redis.log \
&& rm -rf log/httpd/* \
&& rm -rf log/supervisord/* \
&& rm -rf log/supervisord.log \
print_info "Clean log files SUCCESS"
}
# 将 compose 移入 PATH
move_docker_compose_to_path(){
if [ -f /etc/os-release ];then
. /etc/os-release
case "$ID" in
coreos )
if ! [ -d /opt/bin ];then $(_sudo) mkdir -p /opt/bin; fi
$(_sudo) install -m755 /tmp/docker-compose /opt/bin/docker-compose
;;
* )
$(_sudo) install -m755 /tmp/docker-compose /usr/local/bin/docker-compose
;;
esac
else
notsupport
fi
}
# 从 GitHub 安装 compose
install_docker_compose_official(){
if [ "$ARCH" != 'x86_64' ];then
install_docker_compose "$@"
return
fi
if [ "$OS" != 'Linux' ];then print_error "Only support Linux" ; exit 1; fi
curl -sL ${COMPOSE_LINK_OFFICIAL}/$LNMP_DOCKER_COMPOSE_VERSION/docker-compose-`uname -s`-`uname -m` \
> /tmp/docker-compose || print_error "Download docker-compose error, code is $?"
move_docker_compose_to_path
}
# 安装 compose arm 版本
install_docker_compose_arm(){
print_info "$OS $ARCH docker-compose v$LNMP_DOCKER_COMPOSE_VERSION is installing by pip3 ..."
command -v pip3 >/dev/null 2>&1 || $(_sudo) apt install -y python3-pip
$(_sudo) apt install -y libffi-dev || true
local pip_config="[global]\nindex-url = https://pypi.douban.com/simple\n[list]\nformat=columns"
if ! [ -d ~/.pip ];then
if ! [ "${LNMP_CN_ENV}" = "false" ];then
mkdir -p ~/.pip
echo -e $pip_config > ~/.pip/pip.conf
fi
fi
if ! [ -d /root/.pip ];then
if ! [ "${LNMP_CN_ENV}" = "false" ];then
$(_sudo) mkdir -p /root/.pip || true
echo -e $pip_config | $(_sudo) tee /root/.pip/pip.conf || true
fi
fi
$(_sudo) pip3 install --upgrade docker-compose
}
install_docker_compose(){
if ! [ "$OS" = 'Linux' ];then echo; print_error "Docker Desktop not Support install docker-compose"; exit 1; fi
if [ "${ARCH}" = 'armv7l' ] || [ ${ARCH} = 'aarch64' ];then
install_docker_compose_arm "$@"
elif [ "$ARCH" = 'x86_64' ];then
curl -sL ${COMPOSE_LINK}/${LNMP_DOCKER_COMPOSE_VERSION}/docker-compose-`uname -s`-`uname -m` \
> /tmp/docker-compose || print_error "Download docker-compose error, code is $?"
move_docker_compose_to_path
fi
}
docker_compose(){
if [ "$1" = 'compose' ];then shift ; fi
command -v docker-compose >/dev/null 2>&1 || local not_install=1
if ! [ "$not_install" = 1 ];then
# compose 已经安装,判断是否安装了正确的版本
# 取得版本号
DOCKER_COMPOSE_VERSION=`docker-compose --version | cut -d ' ' -f 3 | cut -d , -f 1`
local x=`echo $DOCKER_COMPOSE_VERSION | cut -d . -f 1`
local y=`echo $DOCKER_COMPOSE_VERSION | cut -d . -f 2`
#local z=`echo $DOCKER_COMPOSE_VERSION | cut -d . -f 3`
local current_x=`echo $LNMP_DOCKER_COMPOSE_VERSION | cut -d . -f 1`
local current_y=`echo $LNMP_DOCKER_COMPOSE_VERSION | cut -d . -f 2`
#local true_z=`echo $LNMP_DOCKER_COMPOSE_VERSION | cut -d . -f 3`
if [ "$x" != "$current_x" ];then
print_error "Check docker-compose error"
exit 1
fi
# 判断是否安装正确
# -lt 小于
if [ "$y" -lt "$current_y" ];then
# 安装不正确
if [ "$1" = '--official' ];then shift; print_info "Install compose from GitHub"; \
install_docker_compose_official "$@"; return 0; fi
if [ "$1" = '-f' ];then install_docker_compose -f; return 0; fi
# 判断 OS
if [ "$OS" = 'Darwin' ] ;then
print_error \
"docker-compose v$DOCKER_COMPOSE_VERSION NOT installed Correct version, You MUST update Docker Desktop"
exit 1
elif [ "$OS" = 'Linux' ];then
docker_compose_location=`command -v docker-compose`
print_error \
"docker-compose [$docker_compose_location] v$DOCKER_COMPOSE_VERSION NOT installed Correct version, You MUST EXEC $ ./lnmp-docker compose -f"
exit 1
else
notsupport
fi
# 安装正确
else
print_info "docker-compose v$DOCKER_COMPOSE_VERSION already installed Correct version"; return 0
fi
else
# compose 未安装
print_error "docker-compose NOT install, install..."
if [[ "$1" = '--official' || "$2" = '--official' ]];then \
shift; print_info "Install compose from GitHub"; install_docker_compose_official "$@" && return 0; fi
install_docker_compose
fi
}
network(){
command -v ping > /dev/null 2>&1 || return
ping -c 3 -W 3 baidu.com > /dev/null 2>&1 || ( print_error "Network connection error" ;exit 1)
}
# 初始化
init() {
case $APP_ENV in
# 开发环境 拉取示例项目 [cn github]
development )
print_info "APP_ENV is $APP_ENV"
;;
# 生产环境 转移项目文件、配置文件、安装依赖包
production )
print_info "APP_ENV is $APP_ENV"
# 请在 ./scripts/production-init 定义要执行的操作
scripts/production-init
;;
esac
#@custom
__lnmp_custom_init 2>/dev/null || true
# 初始化完成提示
print_info "Init success"
}
# 更新项目
set_git_remote_origin_url(){
network
git remote get-url origin > /dev/null 2>&1 || local no_origin=1
if [ "$no_origin" = 1 ];then
# 不存在
print_error "This git remote origin NOT set, seting..."
git remote add origin [email protected]:khs1994-docker/lnmp.git
# 不能使用 SSH
git fetch --depth=1 origin > /dev/null 2>&1 || git remote set-url origin https://github.com/khs1994-docker/lnmp
print_info `git remote get-url origin`
echo
elif [ `git remote get-url origin` != '[email protected]:khs1994-docker/lnmp.git' ] \
&& [ `git remote get-url origin` != 'https://gitee.com/khs1994-docker/lnmp' ] \
&& [ `git remote get-url origin` != '[email protected]:khs1994-docker/lnmp' ] \
&& [ `git remote get-url origin` != 'https://github.com/khs1994-docker/lnmp' ];then
# 存在但是设置错误
GIT_REMOTE_ORIGIN=`git remote get-url origin`
print_error "This git remote origin ${GIT_REMOTE_ORIGIN} NOT set Correct, reseting..."
git remote set-url origin [email protected]:khs1994-docker/lnmp.git
# 不能使用 SSH
git fetch --depth=1 origin > /dev/null 2>&1 || git remote set-url origin https://github.com/khs1994-docker/lnmp
print_info `git remote get-url origin`
fi
}
update(){
if ! [ -d .git ];then git init > /dev/null 2>&1; BRANCH=master; fi
set_git_remote_origin_url
for item in "$@"
do
if [ "$item" = '-f' ];then force='true'; fi
done
GIT_STATUS=`git status -s --ignore-submodules`
if [ ! -z "${GIT_STATUS}" ] && [ ! "$force" = 'true' ];then
git status -s --ignore-submodules
print_error "Somefile changed, please commit or stash first"
exit 1
fi
print_info "Branch is ${BRANCH}"
git pull origin ${BRANCH} || true
git reset --hard origin/${BRANCH}
# print_info "Update Git Submodule"
# git submodule update --init --recursive
command -v bash > /dev/null 2>&1 || sed -i 's!^#\!/bin/bash.*!#\!/bin/sh!g' lnmp-docker
}
_lnmp_bug(){
docker_version=$(docker --version)
compose_version=$(docker-compose --version)
dockerd_version=$(docker version -f "{{.Server.Version}}")
dockerd_os=$(docker version -f "{{.Server.Os}}")
dockerd_arch=$(docker version -f "{{.Server.Arch}}")
docker_version_info=$(docker version -f "CLIENT: {{.Client}} || DAEMON: {{.Server}}")
git_commit=$(git log -1 --pretty=%H)
echo "
<details>
<summary>OS Environment Info</summary>
<pre>
" > bug.md
cat /etc/os-release >> bug.md 2>&1 /dev/null || uname -s >> bug.md
echo "
</pre>
<code>$docker_version</code>
<code>$compose_version</code>
<code>docker daemon $dockerd_version $dockerd_os $dockerd_arch</code>
<code>$docker_version_info</code>
* https://github.com/khs1994-docker/lnmp/commit/$git_commit
</details>
<details>
<summary>Console output</summary>
<!--Don't Edit it-->
<!--不要手动编辑以上内容,将终端输出内容贴到下面-->
<pre>
</pre>
</details>
## My Issue is
<!--在这里描述你的问题-->
XXX
XXX
<!--提交问题之前务必点击(Preview)标签进行预览-->
" >> bug.md
print_warning "Please Edit bug.md, then new issue in [ https://github.com/khs1994-docker/lnmp/issues/new/choose ] with copy bug.md"
}
nginx_http(){
echo "#
# Generate nginx config By khs1994-docker/lnmp
#
server {
listen 80;
server_name $1;
root ${LNMP_PHP_PATH:-/app}/$2;
index index.html index.htm index.php;
# include conf.d/demo-include-php.config
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ .*\.php(\/.*)*$ {
fastcgi_pass php7:9000;
include fastcgi.conf;
}
}" > config/${LNMP_NGINX_CONF_D:-nginx}/$1.conf
}
apache_http(){
echo "#
# Generate Apache2 connfig By khs1994-docker/lnmp
#
<VirtualHost *:80>
DocumentRoot \"${LNMP_PHP_PATH:-/app}/$2\"
ServerName $1
ServerAlias $1
ErrorLog \"logs/$1.error.log\"
CustomLog \"logs/$1.access.log\" common
<FilesMatch \.php$>
SetHandler \"proxy:fcgi://php7:9000\"
</FilesMatch>
<Directory \"/app/$2\" >
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>" >> config/${LNMP_HTTPD_CONF_D:-httpd}/$1.conf
}
apache_https(){
echo "#
# Generate Apache2 HTTPS config By khs1994-docker/lnmp
#
<VirtualHost *:443>
DocumentRoot \"${LNMP_PHP_PATH:-/app}/$2\"
ServerName $1
ServerAlias $1
ErrorLog \"logs/$1.error.log\"
CustomLog \"logs/$1.access.log\" common
SSLEngine on
SSLCertificateFile conf/ssl/$1.crt
SSLCertificateKeyFile conf/ssl/$1.key
# HSTS (mod_headers is required) (15768000 seconds = 6 months)
Header always set Strict-Transport-Security \"max-age=15768000\"
<FilesMatch \.php$>
SetHandler \"proxy:fcgi://php7:9000\"
</FilesMatch>
<Directory \"/app/$2\" >
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>" >> config/${LNMP_HTTPD_CONF_D:-httpd}/$1.conf
}
nginx_https(){
echo "#
# Generate nginx HTTPS config By khs1994-docker/lnmp
#
server {
listen 80;
server_name $1;
return 301 https://\$host\$request_uri;
}
server{
listen 443 ssl http2;
server_name $1;
root ${LNMP_PHP_PATH:-/app}/$2;
index index.html index.htm index.php;
# include conf.d/demo-include-ssl.config
ssl_certificate conf.d/ssl/$1.crt;
ssl_certificate_key conf.d/ssl/$1.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'TLS13+AESGCM+AES128:TLS13+AESGCM+AES256:TLS13+CHACHA20:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
ssl_prefer_server_ciphers on;
ssl_stapling on;
ssl_stapling_verify on;
# include conf.d/demo-include-php.config
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ .*\.php(\/.*)*$ {
fastcgi_pass php7:9000;
include fastcgi.conf;
}
}" > config/${LNMP_NGINX_CONF_D:-nginx}/$1.conf
}
# 申请 ssl 证书
acme(){
exec docker run --init -i ${tty} --rm \
-v $PWD/config/${LNMP_NGINX_CONF_D:-nginx}/ssl:/ssl \
--mount source=lnmp_ssl-data,target=/acme.sh \
--env-file $LNMP_ENV_FILE \
khs1994/acme:${ACME_VERSION:-latest} acme.sh "$@"
}
ssl(){
if [ -z "$1" ];then
exec echo "command example
$ ./lnmp-docker ssl khs1994.com [--rsa] [--httpd] [--debug] [acme other parameters]
通配符证书
$ ./lnmp-docker ssl khs1994.com -d *.khs1994.com -d *.t.khs1994.com
RSA 证书(默认 ECC 证书)
$ ./lnmp-docker ssl khs1994.com -d *.khs1994.com --rsa [--httpd] [--debug] [acme other parameters]
HTTPD server
$ ./lnmp-docker ssl khs1994.com -d *.khs1994.com --httpd [--rsa] [--debug] [acme other parameters]
ACME.sh help
$ ./lnmp-docker acme.sh
"
fi
for httpd in "$@"
do
if [ "${httpd}" = '--httpd' ];then
HTTPD=1
fi
if [ "${httpd}" = '--rsa' ];then
RSA=1
fi
done
command="$@"
command=${command[@]//'--rsa'/}
command=${command[@]//'--httpd'/}
exec docker run --init -i ${tty} --rm \
-v $PWD/config/$(if [ "$HTTPD" = 1 ];then \
echo ${LNMP_HTTPD_CONF_D:-httpd}; else echo ${LNMP_NGINX_CONF_D:-nginx}; fi)/ssl:/ssl \
--mount source=lnmp_ssl-data,target=/acme.sh \
--env-file $LNMP_ENV_FILE \
-e HTTPD=${HTTPD:-0} \
-e RSA=${RSA:-0} \
khs1994/acme:${ACME_VERSION:-latest} "$command"
}
ssl_self(){
docker run --init -i ${tty} --rm -v $PWD/config/${LNMP_NGINX_CONF_D:-nginx}/ssl:/ssl khs1994/tls "$@"
}
# 快捷开始 PHP 项目开发
new(){
for arg in "$@"; do if [ $arg = '-f' ];then rm -rf ${APP_ROOT}/$1; fi; done
if [ -z "$1" ];then read -p "Please input project name: /app/" name; else name=$1; fi
if [ -z "$name" ];then print_error 'Please input content'; exit 1; fi
if [ -d ${APP_ROOT}/$name ];then print_error "This folder existing"; exit 1; fi
if [ -z "$2" -o "$2" = '-f' ];then read -p \
"Please input domain:{ example http[s]://$name.domain.com }" input_url; else input_url=$2; fi
if [ -z "$input_url" ]; then print_error 'Please input content'; exit 1; fi
protocol=$(echo $input_url | awk -F':' '{print $1}')
url=$(echo $input_url | awk -F'[/:]' '{print $4}')
if [ -z $url ];then url=$input_url; protocol=http; fi
print_info "PROTOCOL IS $protocol"
print_info "URL IS $protocol://$url"
if [ $protocol = 'https' ];then
# 申请 ssl 证书
read -n 1 -p "Self-Signed SSL certificate? [y/N]:" self_signed
echo ""
if [ "$self_signed" = 'y' ];then
ssl_self $url
else
ssl $url
fi
# 生成 nginx 配置文件
nginx_https $url $name
else
nginx_http $url $name
fi
mkdir -p ${APP_ROOT}/$name
print_info "Now you can start PHP project in /${APP_ROOT}/$name"
print_info "Please set hosts in /etc/hosts in development"
}
#
# $1 compose name
# $2 Swarm mode name
# $3 bash or sh
#
get_service_container_id(){
local SERVICE_NAME=$1
container_id=`docker container ls \
--format "{{.ID}}" \
-f label=${LNMP_DOMAIN:-com.khs1994.lnmp} \
-f label=com.docker.compose.service=$SERVICE_NAME -n 1`
if [ -z "$container_id" ];then
container_id=`docker container ls \
--format "{{.ID}}" \
-f label=${LNMP_DOMAIN:-com.khs1994.lnmp} \