-
Notifications
You must be signed in to change notification settings - Fork 1
/
initialization.sh
1027 lines (873 loc) · 38.8 KB
/
initialization.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# coding:utf-8
# **********************************************************
# * Author : buyfakett
# * Email : [email protected]
# * Create time : 2023-1-28
# * Last modified : 2024-6-17
# * Filename : initialization.sh
# * Description : shell
# **********************************************************
pwd=$(pwd)
# docker位置
docker_data_site=${docker_data_site:-"/data/docker"}
# docker版nginx快捷位置
docker_nginx_site=${docker_nginx_site:-"/data/app/nginx"}
# 本地版nginx快捷位置
local_nginx_site=${local_nginx_site:-"/data/app/nginx"}
# node版本
install_node_version=${install_node_version:-"16"}
# 是否是中国大陆(1:是,2:不是)
is_mainland=${is_mainland:-"1"}
# wget重试次数
wget_retry_number=${wget_retry_number:-"3"}
# 是否是aws(在自动挂载硬盘的时候,aws和别的厂商不一样)
is_aws=${is_aws:-"f"}
# 需要自动挂载的
mount_dir=${mount_dir:-"/data"}
# fstab的位置
fstab_file=${fstab_file:-"/etc/fstab"}
# 如果$1传一个y就全自动
is_auto_key=$1
# 下载的地方
download_type=${download_type:-"gitee"}
# 颜色参数,让脚本更好看
Green="\033[32m"
Font="\033[0m"
Red="\033[31m"
# 本地脚本版本号
shell_version=v1.9.1
# 远程仓库作者
git_project_author_name=buyfakett
# 远程仓库项目名
git_project_project_name=centos7_initialization
# 远程仓库名
git_project_name=${git_project_author_name}/${git_project_project_name}
# 打印初始化
function echo_initialization(){
echo -e "${Green}
_ __ _ _ _____ _ ___ _ _ ______ ___ _____ _ _____ __ _
| | | \ | | | | |_ _| | | / | | | | | |___ / / | |_ _| | | / _ \ | \ | |
| | | \| | | | | | | | / /| | | | | | / / / /| | | | | | | | | | | \| |
| | | |\ | | | | | | | / / | | | | | | / / / / | | | | | | | | | | | |\ |
| | | | \ | | | | | | | / / | | | |___ | | / /__ / / | | | | | | | |_| | | | \ |
|_| |_| \_| |_| |_| |_| /_/ |_| |_____| |_| /_____| /_/ |_| |_| |_| \_____/ |_| \_|
Email:[email protected] Author:buyfakett Filename:initialization.sh
${Font}"
}
# 判断系统
function Inspection_system(){
# 获取发行版信息
distro=$(cat /etc/*-release | grep -w "ID" | awk -F '=' '{print $2}' | tr -d '"')
# 获取操作系统版本
os_version=$(cat /etc/centos-release | grep -oE '[0-9]+\.[0-9]+' | cut -d'.' -f1)
case "$distro" in
"centos")
if [[ $os_version -eq 7 ]]; then
main
else
if (whiptail --title "当前系统不是CentOS 7.*版本,是否继续安装" --yesno "当前系统不是CentOS 7.*版本,是否继续安装" --fb 15 70); then
main
else
echo -e "${Red}已跳过安装${Font}"
exit 0
fi
fi
;;
"alinux")
if [[ $os_version -eq 2 ]]; then
if (whiptail --title "当前系统是alinux 2.*版本,但是脚本是为CentOS 7.*设计,是否继续安装" --yesno "当前系统是alinux 2.*版本,但是脚本是为CentOS 7.*设计,是否继续安装" --fb 15 70); then
main
else
echo -e "${Red}已跳过安装${Font}"
exit 0
fi
else
if (whiptail --title "当前系统不是alinux 2.*版本,是否继续安装" --yesno "当前系统不是alinux 2.*版本,是否继续安装" --fb 15 70); then
main
else
echo -e "${Red}已跳过安装${Font}"
exit 0
fi
fi
;;
"ubuntu")
echo "本脚本是为centos7设计的,当前系统是:Ubuntu,当前版本为${os_version}"
exit 1
;;
"fedora")
echo "本脚本是为centos7设计的,当前系统是:Fedora,当前版本为${os_version}"
exit 1
;;
*)
echo "本脚本是为centos7设计的,当前系统是:${distro},当前版本为${os_version}"
exit 1
;;
esac
}
# root权限
function root_need(){
if [[ $EUID -ne 0 ]]; then
echo -e "${Red}你现在不是root权限,请使用sudo命令或者联系网站管理员${Font}"
exit 1
fi
}
# 检查版本
function is_inspect_script(){
yum install -y wget jq
echo -e "${Green}您已开始检查版本${Font}"
if [ $inspect_script == 1 ];then
remote_version=$(wget -qO- -t1 -T2 "https://gitee.com/api/v5/repos/${git_project_name}/releases/latest" | jq -r '.tag_name')
elif [ $inspect_script == 2 ];then
remote_version=$(wget -qO- -t1 -T2 "https://api.github.com/repos/${git_project_name}/releases/latest" | jq -r '.tag_name')
fi
if [ ! "${remote_version}"x = "${shell_version}"x ];then
bash <( curl -s -S -L "https://${download_type}.com/${git_project_name}/raw/master/$(basename $0)" )
else
echo -e "${Green}您现在的版本是最新版${Font}"
fi
}
# 设置时区
function set_sys_timezone() {
rm -rf /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo "Asia/Shanghai" > /etc/timezone
systemctl restart crond
}
# 关闭防火墙
function close_firewall(){
systemctl disable firewalld.service --now
}
# 更新yum包
function update_packages(){
yum install -y wget whiptail
cd /etc/yum.repos.d
if [ "${is_mainland}"x == "1"x ] && [ ${is_auto_key,,}x != 'y'x ];then
inspect_script_yum=$(whiptail --title "# 是否yum换源 #" --menu "# 是否yum换源 #" --ok-button 确认 --cancel-button 退出 20 65 13 \
"0" "不换源" \
"1" "阿里" \
"2" "清华大学" \
"3" "中科大" \
"4" "退出" 3>&1 1>&2 2>&3)
EXITSTATUS_YUM=$?
if [ $EXITSTATUS_YUM = 0 ]; then
case $inspect_script_yum in
0)
echo -e "${Green}您已选择不换源${Font}"
;;
1)
mv Centos-Base.repo Centos-Base.repo.bak
wget -t ${wget_retry_number} -O Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache
;;
2)
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo
yum clean all && yum makecache
;;
3)
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
-i.bak \
/etc/yum.repos.d/CentOS-*.repo
yum clean all && yum makecache
;;
*)
echo -e "${Red}操作错误${Font}"
;;
esac
else
exit 0
fi
wget -t ${wget_retry_number} -O Centos-ali.repo http://mirrors.aliyun.com/repo/Centos-7.repo
fi
yum install -y yum-utils device-mapper-persistent-data lvm2 tree git bash-completion.noarch \
chrony lrzsz tar zip unzip gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl--devel \
screen tree
systemctl enable chronyd --now
yum update -y
cd ${pwd}
}
# 安装工具
function install_tools(){
# https://gitee.com/buyfakett/centos7_initialization/raw/master/download_file/add2swap.sh
[ ! -f add2swap.sh ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/raw/master/download_file/add2swap.sh
}
# 下载docker
function install_docker(){
mkdir -p /data/app
mkdir -p /data/logs/docker
if [ "${enable_docker_rsyslog}"x == "1"x ];then
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/sysconfig/selinux
[ $(egrep "^SELINUX=" /etc/selinux/config |wc -l) == 0 ] && echo "SELINUX=disabled" >> /etc/selinux/config
sed -i 's/#$ModLoad imtcp/$ModLoad imtcp/g' /etc/rsyslog.conf
sed -i 's/#$InputTCPServerRun 514/$InputTCPServerRun 514/g' /etc/rsyslog.conf
systemctl restart rsyslog
fi
# cat << EOF > /etc/yum.repos.d/docker-ce.repo
# [dockerCe]
# name=dockerCe
# baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7.9/x86_64/stable
# gpgcheck=0
# enabled=1
# EOF
cd /etc/yum.repos.d
[ ! -f Centos-7.repo ] && wget -t ${wget_retry_number} http://mirrors.aliyun.com/repo/Centos-7.repo
[ ! -f epel-testing.repo ] && wget -t ${wget_retry_number} http://mirrors.aliyun.com/repo/epel-testing.repo
[ ! -f epel-7.repo ] && wget -t ${wget_retry_number} http://mirrors.aliyun.com/repo/epel-7.repo
[ ! -f epel.repo ] && wget -t ${wget_retry_number} http://mirrors.aliyun.com/repo/epel.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
cd ${pwd}
yum makecache
yum -y install docker-ce docker-ce-cli containerd.io && systemctl enable docker --now
# 为最小安装不能自动补全做的兼容
source /usr/share/bash-completion/completions/docker
source /usr/share/bash-completion/bash_completion
mkdir -p /etc/docker/
# 是否开启api
if [ "${enable_docker_api}"x == "1"x ];then
sed -i '/^ExecStart=/cExecStart=/usr/bin/dockerd' /usr/lib/systemd/system/docker.service
systemctl daemon-reload
if [ "${enable_docker_rsyslog}"x == "1"x ];then
# 是否换源
if [ "${is_mainland}"x == "1"x ];then
cat << EOF > /etc/docker/daemon.json
{
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"],
"registry-mirrors": [
"https://pee6w651.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
],
"data-root": "${docker_data_site}",
"log-driver": "syslog",
"log-opts": {
"syslog-address": "tcp://127.0.0.1:514",
"tag": "docker/{{.Name}},"
}
}
EOF
else
cat << EOF > /etc/docker/daemon.json
{
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"],
"data-root": "${docker_data_site}",
"log-driver": "syslog",
"log-opts": {
"syslog-address": "tcp://127.0.0.1:514",
"tag": "docker/{{.Name}},"
}
}
EOF
fi
systemctl restart docker
cat << EOF > /etc/rsyslog.d/rule.conf
\$EscapeControlCharactersOnReceive off
\$template CleanMsgFormat,"%msg:2:$%\n"
\$template docker,"data/logs/docker/%syslogtag:F,44:1%/%\$YEAR%-%\$MONTH%-%\$DAY%.log"
if \$syslogtag contains 'docker' then ?docker;CleanMsgFormat
& ~
EOF
systemctl restart rsyslog
cat << EOF > /data/logs/docker/gzip_log.sh
#!/bin/bash
for day in 1;
do
find /data/logs/ -name \`date -d "\${day} days ago" +%Y-%m-%d\`*.log -type f -exec gzip {} \;
done
EOF
cat << EOF > /data/logs/docker/del_gz.sh
#!/bin/bash
find /data/logs/ -mtime +30 -name "*.gz" -exec rm -rf {} \;
EOF
chmod +x /data/logs/docker/del_gz.sh /data/logs/docker/gzip_log.sh
cat << EOF >> /var/spool/cron/root
0 12 * * * /bin/sh -x /data/logs/docker/gzip_log.sh
30 12 * * * /bin/sh -x /data/logs/docker/del_gz.sh
EOF
else
if [ "${is_mainland}"x == "1"x ];then
cat << EOF > /etc/docker/daemon.json
{
"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"],
"registry-mirrors": [
"https://pee6w651.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
]
}
EOF
fi
fi
else
# 是否开启docker日志发送到本地rsyslog
if [ "${enable_docker_rsyslog}"x == "1"x ];then
# 是否换源
if [ "${is_mainland}"x == "1"x ];then
cat << EOF > /etc/docker/daemon.json
{
"registry-mirrors": [
"https://pee6w651.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
],
"data-root": "${docker_data_site}",
"log-driver": "syslog",
"log-opts": {
"syslog-address": "tcp://127.0.0.1:514",
"tag": "docker/{{.Name}},"
}
}
EOF
else
cat << EOF > /etc/docker/daemon.json
{
"data-root": "${docker_data_site}",
"log-driver": "syslog",
"log-opts": {
"syslog-address": "tcp://127.0.0.1:514",
"tag": "docker/{{.Name}},"
}
}
EOF
fi
systemctl restart docker
cat << EOF > /etc/rsyslog.d/rule.conf
\$EscapeControlCharactersOnReceive off
\$template CleanMsgFormat,"%msg:2:$%\n"
\$template docker,"data/logs/docker/%syslogtag:F,44:1%/%\$YEAR%-%\$MONTH%-%\$DAY%.log"
if \$syslogtag contains 'docker' then ?docker;CleanMsgFormat
& ~
EOF
systemctl restart rsyslog
cat << EOF > /data/logs/docker/gzip_log.sh
#!/bin/bash
for day in 1;
do
find /data/logs/ -name \`date -d "\${day} days ago" +%Y-%m-%d\`*.log -type f -exec gzip {} \;
done
EOF
cat << EOF > /data/logs/docker/del_gz.sh
#!/bin/bash
find /data/logs/ -mtime +30 -name "*.gz" -exec rm -rf {} \;
EOF
chmod +x /data/logs/docker/del_gz.sh /data/logs/docker/gzip_log.sh
cat << EOF >> /var/spool/cron/root
0 12 * * * /bin/sh -x /data/logs/docker/gzip_log.sh
30 12 * * * /bin/sh -x /data/logs/docker/del_gz.sh
EOF
else
if [ "${is_mainland}"x == "1"x ];then
cat << EOF > /etc/docker/daemon.json
{
"registry-mirrors": [
"https://pee6w651.mirror.aliyuncs.com",
"https://docker.mirrors.ustc.edu.cn",
"https://registry.docker-cn.com"
]
}
EOF
fi
fi
fi
systemctl restart docker
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/docker-compose
[ ! -f /usr/local/bin/docker-compose ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/docker-compose -O /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
cd ${pwd}
}
# 安装本地版的nginx
function install_local_nginx(){
mkdir -p ${local_nginx_site}
yum install -y yum-utils logrotate
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty -y
ln -s /usr/local/openresty/nginx/conf /root/nginx
mkdir -p ${local_nginx_site}/conf/conf.d
mkdir -p ${local_nginx_site}/web
mkdir -p ${local_nginx_site}/ssl
mkdir -p ${local_nginx_site}/res
mkdir -p ${local_nginx_site}/lua
mkdir -p ${local_nginx_site}/logs
chmod -R 755 ${local_nginx_site}/logs
# https://gitee.com/buyfakett/centos7_initialization/raw/master/download_file/nginx_local.conf
[ ! -f /usr/local/openresty/nginx/conf/nginx.conf ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/raw/master/download_file/nginx_local.conf -O /usr/local/openresty/nginx/conf/nginx.conf
# https://gitee.com/buyfakett/centos7_initialization/raw/master/download_file/api.conf.bak
[ ! -f ${local_nginx_site}/conf/conf.d/api.conf.bak ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/raw/master/download_file/api.conf.bak -O ${local_nginx_site}/conf/conf.d/api.conf.bak
# https://gitee.com/buyfakett/centos7_initialization/raw/master/download_file/reload_local.sh
[ ! -f ${local_nginx_site}/conf/conf.d/reload.sh ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/raw/master/download_file/reload_local.sh -O ${local_nginx_site}/conf/conf.d/reload.sh
# https://gitee.com/buyfakett/centos7_initialization/raw/master/download_file/local_nginx_index.conf
[ ! -f ${local_nginx_site}/conf/conf.d/index.conf ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/raw/master/download_file/local_nginx_index.conf -O ${local_nginx_site}/conf/conf.d/index.conf
cat << EOF > ${local_nginx_site}/nginx_log.sh
#!/bin/bash
now_date=\`date -d '-1 day' +%Y-%m-%d\`
cat ${local_nginx_site}/logs/nginx.log > ${local_nginx_site}/logs/nginx-\${now_date}.log && > ${local_nginx_site}/logs/nginx.log
EOF
cat << EOF > ${local_nginx_site}/gzip_log.sh
#!/bin/bash
for day in 1;
do
find ${local_nginx_site}/logs/ -name nginx-\`date -d "\${day} days ago" +%Y-%m-%d\`*.log -type f -exec gzip {} \;
done
EOF
cat << EOF > ${local_nginx_site}/del_gz.sh
#!/bin/bash
find ${local_nginx_site}/logs/ -mtime +30 -name "*.gz" -exec rm -rf {} \;
EOF
chmod +x ${local_nginx_site}/del_gz.sh ${local_nginx_site}/gzip_log.sh ${local_nginx_site}/nginx_log.sh ${local_nginx_site}/conf/conf.d/reload.sh
cat << EOF >> /var/spool/cron/root
0 0 * * * /bin/sh -x /root/nginx/nginx_log.sh
0 12 * * * /bin/sh -x /root/nginx/gzip_log.sh
30 12 * * * /bin/sh -x /root/nginx/del_gz.sh
EOF
systemctl enable openresty --now
cd ${pwd}
}
# 安装docker版本的nginx
function install_docker_nginx(){
mkdir -p ${docker_nginx_site}/config/conf.d/
# https://gitee.com/buyfakett/centos7_initialization/raw/master/download_file/nginx.conf
[ ! -f ${docker_nginx_site}/config/nginx.conf ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/raw/master/download_file/nginx.conf -O ${docker_nginx_site}/config/nginx.conf
cat << EOF > ${docker_nginx_site}/setup.sh
docker stop nginx
docker rm nginx
docker run -id \\
--name nginx \\
--restart=always \\
-e LC_ALL="C.UTF-8" \\
-e LANG="C.UTF-8" \\
--network=host \\
-v ./config/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \\
-v ./config/conf.d/:/etc/nginx/conf.d/ \\
-v ./ssl/:/data/ssl/ \\
-v ./lua/:/data/lua/ \\
-v ./web/:/data/web/ \\
-v ./res/:/data/res/ \\
-v /data/logs/nginx/:/data/logs/nginx/ \\
-v /etc/localtime:/etc/localtime:ro \\
openresty/openresty
EOF
# https://gitee.com/buyfakett/centos7_initialization/raw/master/download_file/api.conf.bak
[ ! -f ${docker_nginx_site}/config/conf.d/api.conf.bak ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/raw/master/download_file/api.conf.bak -O ${docker_nginx_site}/config/conf.d/api.conf.bak
# https://gitee.com/buyfakett/centos7_initialization/raw/master/download_file/reload.sh
[ ! -f ${docker_nginx_site}/config/conf.d/reload.sh ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/raw/master/download_file/reload.sh -O ${docker_nginx_site}/config/conf.d/reload.sh
cd ${docker_nginx_site}/
chmod +x ${docker_nginx_site}/setup.sh ${docker_nginx_site}/config/conf.d/reload.sh
/bin/bash -x ${docker_nginx_site}/setup.sh
cd ${pwd}
}
# 宿主机安装maven和java17
function install_local_maven_java17(){
cd /usr/local/
# 安装maven
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/apache-maven-3.6.3-bin.zip
[ ! -f /usr/local/apache-maven-3.6.3-bin.zip ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/apache-maven-3.6.3-bin.zip -O /usr/local/apache-maven-3.6.3-bin.zip
unzip apache-maven-3.6.3-bin.zip
rm -f apache-maven-3.6.3-bin.zip
mv apache-maven-3.6.3 maven
export PATH=/usr/local/maven/bin:$PATH
mv /usr/local/maven/conf/settings.xml /usr/local/maven/conf/settings.xml.bak
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/settings.xml
[ ! -f /usr/local/maven/conf/settings.xml ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/settings.xml -O /usr/local/maven/conf/settings.xml
# 安装java17
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/jdk-17_linux-x64_bin.tar.gz
[ ! -f /usr/local/jdk-17_linux-x64_bin.tar.gz ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/jdk-17_linux-x64_bin.tar.gz -O /usr/local/jdk-17_linux-x64_bin.tar.gz
tar -zxvf jdk-17_linux-x64_bin.tar.gz
mv jdk-*/ java/
rm -f jdk-17_linux-x64_bin.tar.gz
cat << EOF >> /etc/profile
# maven
export PATH=/usr/local/maven/bin:\$PATH
# java
export JAVA_HOME=/usr/local/java
export JRE_HOME=\${JAVA_HOME}/jre
export CLASSPATH=.:\${JAVA_HOME}/lib:\${JRE_HOME}/lib
export PATH=\${JAVA_HOME}/bin:\$PATH
EOF
source /etc/profile
java -version
mvn -version
cd ${pwd}
}
# 安装node.js
function install_nodejs(){
mkdir /usr/local/nodejs
cd /usr/local/nodejs
if [ $install_node_version == 10 ];then
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/node-v10.23.0-linux-x64.tar.xz
[ ! -f /usr/local/nodejs/node-v10.23.0-linux-x64.tar.xz ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/node-v10.23.0-linux-x64.tar.xz -O /usr/local/nodejs/node-v10.23.0-linux-x64.tar.xz
xz -d node-v10.23.0-linux-x64.tar.xz
tar xvf node-v10.23.0-linux-x64.tar
mv node-v10.23.0-linux-x64/ node-10/
fi
if [ $install_node_version == 12 ];then
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/node-v12.4.0-linux-x64.tar.xz
[ ! -f /usr/local/nodejs/node-v12.4.0-linux-x64.tar.xz ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/node-v12.4.0-linux-x64.tar.xz -O /usr/local/nodejs/node-v12.4.0-linux-x64.tar.xz
xz -d node-v12.4.0-linux-x64.tar.xz
tar xvf node-v12.4.0-linux-x64.tar
mv node-v12.4.0-linux-x64/ node-12/
fi
if [ $install_node_version == 14 ];then
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/node-v14.17.1-linux-x64.tar.xz
[ ! -f /usr/local/nodejs/node-v14.17.1-linux-x64.tar.xz ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/node-v14.17.1-linux-x64.tar.xz -O /usr/local/nodejs/node-v14.17.1-linux-x64.tar.xz
xz -d node-v14.17.1-linux-x64.tar.xz
tar xvf node-v14.17.1-linux-x64.tar
mv node-v14.17.1-linux-x64/ node-14/
fi
if [ $install_node_version == 16 ];then
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/node-v16.20.2-linux-x64.tar.xz
[ ! -f /usr/local/nodejs/node-v16.20.2-linux-x64.tar.xz ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/node-v16.20.2-linux-x64.tar.xz -O /usr/local/nodejs/node-v16.20.2-linux-x64.tar.xz
xz -d node-v16.20.2-linux-x64.tar.xz
tar xvf node-v16.20.2-linux-x64.tar
mv node-v16.20.2-linux-x64/ node-16/
fi
rm -f node-v*.tar.xz && rm -f node-v*.tar
cat << EOF >> /etc/profile
# nodejs
export NODEJS_HOME=/usr/local/nodejs/node-${install_node_version}
export PATH=\${NODEJS_HOME}/bin:\$PATH
EOF
source /etc/profile
node -v
npm -v
if [ "${is_mainland}"x == "1"x ];then
npm config set registry https://registry.npmmirror.com
fi
cd ${pwd}
}
# 安装全部node.js
function install_all_nodejs(){
mkdir /usr/local/nodejs
cd /usr/local/nodejs
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/node-v10.23.0-linux-x64.tar.xz
[ ! -f /usr/local/nodejs/node-v10.23.0-linux-x64.tar.xz ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/node-v10.23.0-linux-x64.tar.xz -O /usr/local/nodejs/node-v10.23.0-linux-x64.tar.xz
xz -d node-v10.23.0-linux-x64.tar.xz
tar xvf node-v10.23.0-linux-x64.tar
mv node-v10.23.0-linux-x64/ node-10/
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/node-v12.4.0-linux-x64.tar.xz
[ ! -f /usr/local/nodejs/node-v12.4.0-linux-x64.tar.xz ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/node-v12.4.0-linux-x64.tar.xz -O /usr/local/nodejs/node-v12.4.0-linux-x64.tar.xz
xz -d node-v12.4.0-linux-x64.tar.xz
tar xvf node-v12.4.0-linux-x64.tar
mv node-v12.4.0-linux-x64/ node-12/
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/node-v14.17.1-linux-x64.tar.xz
[ ! -f /usr/local/nodejs/node-v14.17.1-linux-x64.tar.xz ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/node-v14.17.1-linux-x64.tar.xz -O /usr/local/nodejs/node-v14.17.1-linux-x64.tar.xz
xz -d node-v14.17.1-linux-x64.tar.xz
tar xvf node-v14.17.1-linux-x64.tar
mv node-v14.17.1-linux-x64/ node-14/
# https://gitee.com/buyfakett/centos7_initialization/releases/download/v1.2.3/node-v16.20.2-linux-x64.tar.xz
[ ! -f /usr/local/nodejs/node-v16.20.2-linux-x64.tar.xz ] && wget -t ${wget_retry_number} https://${download_type}.com/${git_project_name}/releases/download/v1.2.3/node-v16.20.2-linux-x64.tar.xz -O /usr/local/nodejs/node-v16.20.2-linux-x64.tar.xz
xz -d node-v16.20.2-linux-x64.tar.xz
tar xvf node-v16.20.2-linux-x64.tar
mv node-v16.20.2-linux-x64/ node-16/
rm -f node-v*.tar.xz && rm -f node-v*.tar
cat << EOF >> /etc/profile
# nodejs
export NODEJS_HOME=/usr/local/nodejs/node-16
export PATH=\${NODEJS_HOME}/bin:\$PATH
EOF
source /etc/profile
node -v
npm -v
if [ "${is_mainland}"x == "1"x ];then
npm config set registry https://registry.npmmirror.com
fi
cd ${pwd}
}
# 安装python3
function install_python3(){
yum install -y epel-release python3 python3-devel
python3 --version
pip3 --version
if [ "${is_mainland}"x == "1"x ];then
pip3 install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
fi
cd ${pwd}
}
# 检查磁盘分区
auto_mount_disk() {
count=0
TMP1=/tmp/.tmp1
TMP2=/tmp/.tmp2
touch $TMP1
touch $TMP2
LOCKfile=/tmp/.$(basename $0)
# 检查系统和依赖
if [ `uname -a |grep -c el7` -eq 1 ];then
modprobe xfs
echo "${Green}开始 yum 安装 xfsprogs${Font}"
yum -y install xfsprogs
fi
# 检查硬盘
touch $LOCKfile
for i in `fdisk -l | egrep "Disk|磁盘" | grep "/dev" | awk '{print $2}' | awk -F: '{print $1}'`
do
if [ -z "$(blkid | grep -v 'PTTYPE="dos"' | grep -w "$i")" ];then
DEVICE_COUNT=$(fdisk -l $i | grep "$i" | awk '{print $2}' | awk -F: '{print $1}' | wc -l)
NEW_MOUNT=$(df -h)
if [ $DEVICE_COUNT -lt 2 ];then
if [ -n "$(echo $NEW_MOUNT | grep -w "$i")" -o "$(grep -v '^#' $fstab_file | grep -v ^$ | awk '{print $1,$2,$3}' | grep -w "$i" | awk '{print $2}')" == '/' -o "$(grep -v '^#' $fstab_file | grep -v ^$ | awk '{print $1,$2,$3}' | grep -w "$i" | awk '{print $3}')" == 'swap' ];then
echo "${Green}$i 已经被挂载${Font}"
else
echo $i >> $LOCKfile
echo "${Green}您有一个空闲磁盘,现在将fdisk并挂载它${Font}"
fi
fi
fi
done
DISK_LIST=$(cat $LOCKfile)
if [ "X$DISK_LIST" == "X" ];then
echo "${Red}没有空余硬盘需要挂载${Font}"
# rm -rf $LOCKfile
# exit 0
else
echo "${Green}系统存在空余硬盘:${Font}"
for i in `echo $DISK_LIST`
do
echo "$i"
count=$((count+1))
done
[ $count -gt 1 ] && { echo "${Red}此系统至少有两个可用磁盘,必须手动挂载${Font}";}
# 格式化硬盘
[ -n "`df -h | grep ${i}`" ] && { echo "${Green}${i} 已挂载${Font}"; echo; ALREADY_MOUNT="true"; } || ALREADY_MOUNT="false"
if [ "${ALREADY_MOUNT}"x != "true"x ];then
fdisk_fun $i > /dev/null 2>&1
if [ "${is_aws}"x == "t"x ]; then
echo "${i}p1" >> $TMP2
else
echo "${i}1" >> $TMP2
fi
# 创建挂载目录
[ -d "$mount_dir" ] && mv ${mount_dir}{,_bk}
mkdir -p $mount_dir
echo "$mount_dir" >> $TMP1
# 挂载硬盘
touch $LOCKfile
paste $TMP2 $TMP1 > $LOCKfile
while read a b
do
dev_uuid=$(blkid|grep ${a}|awk '{print $2}')
[ -z "`grep ^${a} $fstab_file`" -a -z "`grep ${b} $fstab_file`" ] && echo "${dev_uuid} $b xfs defaults 0 0" >> $fstab_file
done < $LOCKfile
mount -a
df -h
fi
fi
rm -rf $LOCKfile $TMP1 $TMP2
}
# fdisk,格式化和创建文件系统
fdisk_fun() {
fdisk -S 56 $1 << EOF
n
p
1
wq
EOF
sleep 3
if [ "${is_aws}"x == "t"x ]; then
mkfs.xfs ${1}p1
else
mkfs.xfs ${1}1
fi
}
function main(){
if [ ${is_auto_key,,}x != 'y'x ]; then
root_need
inspect_script=$(whiptail --title "是否检查脚本" --menu "Choose your option" --ok-button 确认 --cancel-button 退出 20 65 13 \
"0" "gitee" \
"1" "github" \
"2" "不检查更新"\
"3" "退出" 3>&1 1>&2 2>&3)
EXITSTATUS=$?
if [ $EXITSTATUS = 0 ]; then
case $inspect_script in
0|1)
is_inspect_script
;;
2)
echo -e "${Green}已跳过检查更新${Font}"
;;
3)
exit 0
;;
*)
echo -e "${Red}操作错误${Font}"
;;
esac
else
exit 0
fi
echo_initialization
sleep 3
if (whiptail --title "#是否关闭防火墙#" --yesno "是否关闭防火墙" --fb 15 70); then
close_firewall_evn=1
else
echo -e "${Red}已跳过安装${Font}"
fi
if (whiptail --title "#是否所有程序换源#" --yesno "#是否所有程序换源#" --fb 15 70); then
is_mainland=1
download_type=gitee
else
is_mainland=2
download_type=github
echo -e "${Red}已选择不换源${Font}"
fi
OPTION=$(whiptail --title "centos7.* 初始化脚本, made in 2023 by buyfakett" --menu "Choose your option" --ok-button 确认 --cancel-button 退出 20 65 13 \
"1" "手动选择安装" \
"2" "一键全部安装(安装docker版本nginx)" \
"3" "退出" 3>&1 1>&2 2>&3)
EXITSTATUS=$?
if [ $EXITSTATUS = 0 ]; then
case $OPTION in
1)
if (whiptail --title "#是否自动挂载硬盘#" --yesno "是否自动挂载硬盘" --fb 15 70); then
mount_dir=$(whiptail --title "#请输入挂载硬盘#" --inputbox "挂载硬盘默认位置为:/dara\n不推荐修改!!!!" 10 60 "${mount_dir}" --ok-button 确认 --cancel-button 取消 3>&1 1>&2 2>&3)
auto_mount_disk_evn=1
else
auto_mount_disk_evn=2
fi
if (whiptail --title "#是否安装docker#" --yesno "是否安装docker" --fb 15 70); then
docker_data_site=$(whiptail --title "#请输入docker位置#" --inputbox "docker默认位置为:/var/lib/docker\n推荐修改!!!!" 10 60 "${docker_data_site}" --ok-button 确认 --cancel-button 取消 3>&1 1>&2 2>&3)
install_docker_evn=1
if (whiptail --title "#是否开启docker日志发送到本地rsyslog#" --yesno "是否开启rsyslog" --fb 15 70); then
enable_docker_rsyslog=1
else
enable_docker_rsyslog=2
echo -e "${Red}不开启docker日志发送到本地rsyslog${Font}"
fi
if (whiptail --title "#是否开启docker api#" --yesno "是否开启docker api" --fb 15 70); then
enable_docker_api=1
else
enable_docker_api=2
echo -e "${Red}不开启docker api${Font}"
fi
else
echo -e "${Red}已跳过安装${Font}"
fi
NGINX_OPTION=$(whiptail --title "#安装什么版本的nginx/不安装#" --menu "Choose your option" --ok-button 确认 --cancel-button 退出 20 65 13 \
"1" "安装docker版本的nginx" \
"2" "安装local版本的nginx" \
"3" "不安装nginx" \
"4" "退出" 3>&1 1>&2 2>&3)
NGINX_EXITSTATUS=$?
if [ $NGINX_EXITSTATUS = 0 ]; then
case $NGINX_OPTION in
1)
docker_nginx_site=$(whiptail --title "#请输入docker版nginx位置#" --inputbox "自定义nginx的安装位置" 10 60 "${docker_nginx_site}" --ok-button 确认 --cancel-button 取消 3>&1 1>&2 2>&3)
install_docker_nginx_evn=1
;;
2)
local_nginx_site=$(whiptail --title "#请输入本地版nginx位置#" --inputbox "本地版nginx默认位置有点深,推荐创建快捷方式到自己熟系的位置" 10 60 "${local_nginx_site}" --ok-button 确认 --cancel-button 取消 3>&1 1>&2 2>&3)
install_local_nginx_evn=1
;;
3)
echo -e "${Red}已跳过安装${Font}"
;;
*)
echo -e "${Red}操作错误${Font}"
;;
esac
else
exit 0
fi
if (whiptail --title "#是否安装宿主机版本的maven和java17#" --yesno "是否安装宿主机版本的maven和java17" --fb 15 70); then
install_local_maven_java17_evn=1
else
echo -e "${Red}已跳过安装${Font}"
fi
if (whiptail --title "#是否安装node.js#" --yesno "是否安装node.js" --fb 15 70); then
NODE_OPTION=$(whiptail --title "#是否安装全版本的node.js#" --menu "是否安装全版本的node.js" --ok-button 确认 --cancel-button 退出 20 65 13 \
"1" "安装指定版本" \
"2" "安装全部版本(默认使用16)" \
"3" "退出" 3>&1 1>&2 2>&3)
NODE_EXITSTATUS=$?
if [ $NODE_EXITSTATUS = 0 ]; then
case $NODE_OPTION in
1)
install_node_version=$(whiptail --title "#请输入需要安装的node.js的版本#" --inputbox "支持10,12,14,16,18" 10 60 "${install_node_version}" --ok-button 确认 --cancel-button 取消 3>&1 1>&2 2>&3)
install_nodejs_evn=1
;;
2)
install_all_nodejs_evn=1
;;
*)
echo -e "${Red}操作错误${Font}"
;;
esac
else
exit 0
fi
else
echo -e "${Red}已跳过安装${Font}"
fi
if (whiptail --title "#是否安装python3#" --yesno "是否安装python3" --fb 15 70); then
install_python3_evn=1
else
echo -e "${Red}已跳过安装${Font}"
fi
if (whiptail --title "#是否生成2倍虚拟缓存#" --yesno "是否生成2倍虚拟缓存" --fb 15 70); then
add2swap_evn=1
else
echo -e "${Red}已跳过安装${Font}"
fi
;;
2)
close_firewall_evn=1
auto_mount_disk_evn=1
install_docker_evn=1
enable_docker_rsyslog=1
install_docker_nginx_evn=1
install_local_maven_java17_evn=1
install_all_nodejs_evn=1
install_python3_evn=1
add2swap_evn=1
;;
3)
exit 0
;;
*)
echo -e "${Red}操作错误${Font}"
;;
esac
setenforce 0
update_packages && echo -e "\n${Green}更新包成功${Font}\n"
install_tools && echo -e "\n${Green}下载工具成功${Font}\n"
if [ "${is_mainland}"x == "1"x ];then
set_sys_timezone
fi
[ "${close_firewall_evn}"x == "1"x ] && close_firewall && echo -e "\n${Green}关闭防火墙成功${Font}\n"
[ "${auto_mount_disk_evn}"x == "1"x ] && auto_mount_disk
[ "${install_docker_evn}"x == "1"x ] && install_docker && echo -e "\n${Green}安装docker成功${Font}\n"
[ "${install_docker_nginx_evn}"x == "1"x ] && install_docker_nginx && echo -e "\n${Green}安装docker版nginx成功${Font}\n"
[ "${install_local_nginx_evn}"x == "1"x ] && install_local_nginx && echo -e "\n${Green}安装本地版nginx成功${Font}\n"
[ "${install_local_maven_java17_evn}"x == "1"x ] && install_local_maven_java17 && echo -e "\n${Green}安装java和maven成功${Font}\n"
[ "${install_nodejs_evn}"x == "1"x ] && install_nodejss && echo -e "\n${Green}安装nodejs成功${Font}\n"
[ "${install_all_nodejs_evn}"x == "1"x ] && install_all_nodejs && echo -e "\n${Green}安装全部nodejs成功${Font}\n"
[ "${install_python3_evn}"x == "1"x ] && install_python3 && echo -e "\n${Green}安装python3成功${Font}\n"
[ "${add2swap_evn}"x == "1"x ] && /bin/bash add2swap.sh && echo -e "\n${Green}添加2倍虚拟内存成功${Font}\n"