-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathhostvn
3635 lines (3303 loc) · 118 KB
/
hostvn
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
######################################################################
# Auto Install & Optimize LEMP Stack on CentOS 7, 8 #
# #
# Author: Sanvv - HOSTVN Technical #
# Website: https://hostvn.vn #
# #
# Please do not remove copyright. Thank! #
# Please do not copy under any circumstance for commercial reason! #
######################################################################
# shellcheck disable=SC2207
# Set Color
RED='\033[0;31m'
NC='\033[0m'
SCRIPTS_VERSION="1.0.5.3"
# Set variables
OS_VER=$(rpm -E %centos)
OS_ARCH=$(uname -m)
IPADDRESS=$(curl -s http://myip.directadmin.com)
DIR=$(pwd)
BASH_DIR="/var/hostvn"
PHP_MODULES_DIR="/usr/lib64/php/modules"
GITHUB_RAW_LINK="https://raw.githubusercontent.com"
EXT_LINK="https://scripts.hostvn.net"
UPDATE_LINK="https://scripts.hostvn.net/update"
GITHUB_URL="https://github.com"
PECL_PHP_LINK="https://pecl.php.net/get"
PMA_LINK="https://files.phpmyadmin.net/phpMyAdmin"
FILE_INFO="${BASH_DIR}/hostvn.conf"
HOSTNAME=$(hostname)
PHP2_RELEASE="no"
ADMIN_TOOL_PWD=$(< /dev/urandom tr -dc A-Za-z0-9 | head -c8)
# Copyright
AUTHOR="HOSTVN.VN"
AUTHOR_CONTACT="https://www.facebook.com/groups/hostvn.vn"
AUTHOR_WEBSITE="https://hostvn.vn"
# Service Version
PHPMYADMIN_VERSION=$(curl -s ${UPDATE_LINK}/version | grep "phpmyadmin_version=" | cut -f2 -d'=')
PHP_SYS_INFO_VERSION=$(curl -s ${UPDATE_LINK}/version | grep "phpsysinfo_version=" | cut -f2 -d'=')
IGBINARY_VERSION=$(curl -s ${UPDATE_LINK}/version | grep "igbinary_version=" | cut -f2 -d'=')
PHP_MEMCACHED_VERSION=$(curl -s ${UPDATE_LINK}/version | grep "php_memcached_version=" | cut -f2 -d'=')
PHP_REDIS_VERSION=$(curl -s ${UPDATE_LINK}/version | grep "php_redis_version=" | cut -f2 -d'=')
MARIADB_VERSION=$(curl -s ${UPDATE_LINK}/version | grep "mariadb_version=" | cut -f2 -d'=')
#PHP_VERSION_1=$(curl -s ${UPDATE_LINK}/version | grep "php_version=" | cut -f2 -d'=')
#PHP_VERSION_2=$(curl -s ${UPDATE_LINK}/version | grep "php2_version=" | cut -f2 -d'=')
PHP_LIST=( $(curl -s "${UPDATE_LINK}"/version | grep "php_list=" | cut -f2 -d'=') )
if [[ "${OS_VER}" -eq 8 ]]; then
index=0
for keyword in "${PHP_LIST[@]}"; do
if [[ "$keyword" = "php7.1" || "$keyword" = "php7.0" || "$keyword" = "php5.6" ]]; then
unset "PHP_LIST[$index]"
fi
(( index++ ))
done
fi
# Set Lang
OPTION_CHANGE_SSH="Ban co muon thay doi port SSH khong ? "
OPTION_INST_PUREFTP="Ban co muon cai dat PURE-FTPD (Quan ly FTP) khong ? "
OPTION_INST_AV="Ban co muon cai dat Clamav (Scan Malware) khong ? "
OPTION_INST_MEMCACHED="Ban co muon cai dat Memcached khong ? "
OPTION_INST_REDIS="Ban co muon cai dat Redis khong ? "
ENTER_OPTION="Nhap vao lua chon cua ban: "
SELECT_PHP="Hay lua chon phien ban PHP muon cai dat:"
WRONG_PHP_OPTION="Lua chon cua ban khong chinh xac, vui long chon lai."
SELECT_INST_PHP_2="Ban co muon cai dat phien ban PHP thu hai khong - Multiple PHP ?"
ENTER_OPTION_PHP_2="Nhap vao lua chon cua ban [1-2]: "
WRONG_PHP_SELECT_2="Ban nhap sai. Vui long nhap lai."
INVALID_PHP2_OPTION="${RED}Lua chon cua ban khong chinh xac. Vui long chon lai.${NC}"
SELECT_PHP_2="Lua chon phien ban PHP thu hai ban muon su dung:"
INST_MARIADB_ERR="Cai dat MariaDB that bai, vui long truy cap ${AUTHOR_CONTACT} de duoc ho tro."
INST_NGINX_ERR="Cai dat Nginx that bai, vui long truy cap ${AUTHOR_CONTACT} de duoc ho tro."
INST_PHP_ERR="Cai dat PHP that bai, vui long truy cap ${AUTHOR_CONTACT} de duoc ho tro."
INST_PHP_ERR_2="Cai dat PHP 2 that bai, vui long truy cap ${AUTHOR_CONTACT} de duoc ho tro"
INST_IGBINARY_ERR="Cai dat Igbinary that bai. Vui long cai dat lai: Igbinary, Php memcached ext, Phpredis."
INST_MEMEXT_ERR="Cai dat Php memcached extension khong thanh cong. Vui long cai dat lai."
INST_PHPREDIS_ERR="Cai dat Phpredis khong thanh cong. Vui long cai dat lai."
INST_IGBINARY_ERR_2="Cai dat Igbinary cho PHP 2 khong thanh cong. Vui long cai dat lai: Igbinary, Php memcached ext, Phpredis."
INST_MEMEXT_ERR_2="Cai dat Php memcached extension cho PHP 2 khong thanh cong. Vui long cai dat lai."
INST_PHPREDIS_ERR_2="Cai dat Phpredis cho PHP 2 khong thanh cong. Vui long cai dat lai."
NGINX_NOT_WORKING="Nginx khong hoat dong."
MARIADB_NOT_WORKING="MariaDB khong hoat dong."
PUREFTP_NOT_WORKING="Pure-ftp khong hoat dong."
PHP_NOT_WORKING="PHP-FPM khong hoat dong."
LFD_NOT_WORKING="CSF khong hoat dong."
LFD_NOT_WORKING="LFD khong hoat dong."
LOGIN_NOTI1="Cam on ban da su dung dich vu cua ${AUTHOR}."
LOGIN_NOTI2="Neu can ho tro vui long truy cap ${AUTHOR_CONTACT}"
LOGIN_NOTI3="De mo menu ban go lenh sau: hostvn"
# Random Port
RANDOM_ADMIN_PORT=$(shuf -i 49152-57343 -n 1)
CSF_UI_PORT=$(shuf -i 57344-65000 -n 1)
# Dir
DEFAULT_DIR_WEB="/usr/share/nginx/html"
DEFAULT_DIR_TOOL="/usr/share/nginx/private"
USR_DIR="/usr/share"
# Get info VPS
CPU_CORES=$(grep -c "processor" /proc/cpuinfo)
RAM_TOTAL=$(awk '/MemTotal/ {print $2}' /proc/meminfo)
SWAP_TOTAL=$(awk '/SwapFree/ {print $2}' /proc/meminfo)
PHP_MEM=${RAM_TOTAL}+${SWAP_TOTAL}
RAM_MB=$(echo "scale=0;${RAM_TOTAL}/1024" | bc)
NGINX_PROCESSES=$(grep -c ^processor /proc/cpuinfo)
MAX_CLIENT=$((NGINX_PROCESSES * 1024))
rm -rf "${DIR}"/hostvn
rm -rf "${DIR}"/install
############################################
# Function
############################################
cd_dir(){
cd "$1" || return
}
generate_random_pwd(){
< /dev/urandom tr -dc A-Za-z0-9 | head -c16
}
valid_ip()
{
if [[ "$(ipcalc -c "$1" >/dev/null 2>&1; echo $?)" -eq '0' ]]; then
return 0
else
return 1
fi
}
############################################
# Prepare install
############################################
create_bash_dir(){
mkdir -p /home/backup
chmod 710 /home/backup
chmod 711 /home
mkdir -p "${BASH_DIR}"
}
# Config Selinux
config_selinux(){
if [[ "${OS_VER}" -eq 8 ]]; then
dnf -y install policycoreutils-python-utils
fi
se_status=$(getenforce)
# if [ "${se_status}" == "Disabled" ]; then
# setenforce 0
# sed -i 's/SELINUX=disabled/SELINUX=permissive/g' /etc/selinux/config
# fi
# if [ "${se_status}" == "Enforcing" ]; then
# setenforce 0
# sed -i 's/SELINUX=enforcing/SELINUX=permissive/g' /etc/selinux/config
# fi
if [ "${se_status}" == "Permissive" ]; then
setenforce 0
sed -i 's/SELINUX=permissive/SELINUX=disabled/g' /etc/selinux/config
fi
if [ "${se_status}" == "Enforcing" ]; then
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
fi
}
#Set timezone
set_timezone(){
if [[ -f "/etc/localtime" && -f "/usr/share/zoneinfo/Asia/Ho_Chi_Minh" ]]; then
rm -f /etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Ho_Chi_Minh /etc/localtime
else
timedatectl set-timezone Asia/Ho_Chi_Minh
fi
}
#Set OS Archive
set_os_arch(){
if [[ "${OS_ARCH}" == "x86_64" ]]; then
OS_ARCH1="amd64"
elif [[ "${OS_ARCH}" == "i686" ]]; then
OS_ARCH1="x86"
fi
}
# Admin Email
set_email(){
clear
while true
do
read -r -p "Nhap vao email cua ban: " ADMIN_EMAIL
echo
if [[ "${ADMIN_EMAIL}" =~ ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$ ]];
then
echo "Email cua ban la: ${ADMIN_EMAIL}."
break
else
echo "Email ban nhap khong chinh xac vui long nhap lai."
fi
done
}
# Create log file
create_log(){
LOG="/var/log/install.log"
touch "${LOG}"
}
ssh_login_noti(){
string=$(grep -rnw "/root/.bash_profile" -e "${AUTHOR}")
if [ -z "${string}" ]; then
{
echo "echo \"${LOGIN_NOTI1}\""
echo "echo \"${LOGIN_NOTI2}\""
echo "echo \"${LOGIN_NOTI3}\""
} >> ~/.bash_profile
fi
}
############################################
# Option Install
############################################
input_ip(){
echo "Nhap vao dia chi IP cua VPS. Bam Enter de script tu detect IP Public."
read -r -p "Nhap vao dia chi IP cua VPS: " IPADDRESS_NEW
if [ -n "${IPADDRESS_NEW}" ] && valid_ip "${IPADDRESS_NEW}" ; then
IPADDRESS=${IPADDRESS_NEW}
else
printf "IP ban nhap khong chinh xac. Script se tu dong detect IP Public cua VPS."
fi
}
select_php_ver(){
clear
while true
do
printf "%s\n" "${SELECT_PHP}"
PS3="${ENTER_OPTION}"
select opt in "${PHP_LIST[@]}"
do
case $opt in
"$opt") PHP_VERSION="${opt/.}"; break;;
esac
done
echo
if [[ " ${PHP_LIST[*]} " == *" $(echo "${PHP_VERSION}" | fold -w4 | paste -sd'.') "* ]]; then
break
else
clear
printf "${RED}%s${NC}\n" "${WRONG_PHP_OPTION}"
fi
done
sleep 1
}
select_php_multi(){
clear
printf "%s\n" "${SELECT_INST_PHP_2}"
PS3="${ENTER_OPTION_PHP_2}"
options=("Yes" "No")
select opt in "${options[@]}"
do
case $opt in
"Yes") MULTI_PHP="y"; break;;
"No") MULTI_PHP="n"; break;;
*) printf "${RED}%s${NC}\n" "${WRONG_PHP_SELECT_2} $REPLY";;
esac
done
sleep 1
}
select_php_ver_2(){
clear
while true
do
printf "%s\n" "${SELECT_PHP_2}"
PS3="${ENTER_OPTION}"
select opt in "${PHP_LIST[@]}"
do
case $opt in
"$opt") PHP_VERSION_2="${opt/.}"; break;;
esac
done
echo
if [[ " ${PHP_LIST[*]} " == *" $(echo "${PHP_VERSION_2}" | fold -w4 | paste -sd'.') "* ]]; then
break
else
clear
printf "${RED}%s\n${NC}" "${INVALID_PHP2_OPTION}"
fi
done
sleep 1
}
option_clamav(){
if [[ ${RAM_TOTAL} -gt 1049576 ]]; then
clear
printf "%s\n" "${OPTION_INST_AV}"
PS3="${ENTER_OPTION}"
options=("Yes" "No")
select opt in "${options[@]}"
do
case $opt in
"Yes") prompt_inst_av="y"; break;;
"No") prompt_inst_av="n"; break;;
*) printf "${RED}%s${NC}\n" "${WRONG_PHP_SELECT_2} $REPLY";;
esac
done
sleep 1
fi
}
option_memcached(){
clear
printf "%s\n" "${OPTION_INST_MEMCACHED}"
PS3="${ENTER_OPTION}"
options=("Yes" "No")
select opt in "${options[@]}"
do
case $opt in
"Yes") prompt_memcached="y"; break;;
"No") prompt_memcached="n"; break;;
*) printf "${RED}%s${NC}\n" "${WRONG_PHP_SELECT_2} $REPLY";;
esac
done
sleep 1
}
option_redis(){
clear
printf "%s\n" "${OPTION_INST_REDIS}"
PS3="${ENTER_OPTION}"
options=("Yes" "No")
select opt in "${options[@]}"
do
case $opt in
"Yes") prompt_redis="y"; break;;
"No") prompt_redis="n"; break;;
*) printf "${RED}%s${NC}\n" "${WRONG_PHP_SELECT_2} $REPLY";;
esac
done
sleep 1
}
option_pureftp(){
clear
printf "%s\n" "${OPTION_INST_PUREFTP}"
PS3="${ENTER_OPTION}"
options=("Yes" "No")
select opt in "${options[@]}"
do
case $opt in
"Yes") prompt_pureftpd="y"; break;;
"No") prompt_pureftpd="n"; break;;
*) printf "${RED}%s${NC}\n" "${WRONG_PHP_SELECT_2} $REPLY";;
esac
done
sleep 1
}
option_change_ssh_port(){
clear
printf "%s\n" "${OPTION_CHANGE_SSH}"
PS3="${ENTER_OPTION}"
options=("Yes" "No")
select opt in "${options[@]}"
do
case $opt in
"Yes")
prompt_ssh="y";
SSH_PORT="8282";
sleep 1
printf "${RED}Port SSH moi là: 8282${NC}\n";
printf "${RED}Luu y: Voi Google Cloud cac ban can mo port 8282 trong tab VPC network${NC}\n";
sleep 1
break;;
"No") prompt_ssh="n"; SSH_PORT="22" ; break;;
*) printf "${RED}%s${NC}\n" "${WRONG_PHP_SELECT_2} $REPLY";;
esac
done
sleep 1
}
############################################
# Install LEMP Stack
############################################
# Install Nginx
install_nginx(){
if [[ -d /etc/nginx ]]; then
rm -rf /etc/nginx
fi
cat >> "/etc/yum.repos.d/nginx.repo" << EONGINXREPO
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EONGINXREPO
yum -y install yum-utils
yum -y install nginx
mkdir -p "${DEFAULT_DIR_TOOL}"
chown -R nginx:nginx "${DEFAULT_DIR_TOOL}"
semanage permissive -a httpd_t
systemctl start nginx
}
nginx_brotli(){
NGINXV=$(nginx -v 2>&1 | grep -o '[0-9.]*$'; echo)
MODULES_PATH="/etc/nginx/modules"
wget -q "${EXT_LINK}"/ngx_brotli/"${NGINXV}"/ngx_http_brotli_filter_module.so -O "${MODULES_PATH}"/ngx_http_brotli_filter_module.so
wget -q "${EXT_LINK}"/ngx_brotli/"${NGINXV}"/ngx_http_brotli_static_module.so -O "${MODULES_PATH}"/ngx_http_brotli_static_module.so
if [[ -f "${MODULES_PATH}/ngx_http_brotli_filter_module.so" && -f "${MODULES_PATH}/ngx_http_brotli_static_module.so" ]]; then
LOAD_BROTLI_FILTER="load_module modules/ngx_http_brotli_filter_module.so;"
LOAD_BROTLI_STATIC="load_module modules/ngx_http_brotli_static_module.so;"
INCLUDE_BROTLI="include /etc/nginx/extra/brotli.conf;"
BROTLI_STATIC_OFF="brotli_static off;"
fi
}
# Config naxsi
nginx_naxsi(){
mkdir -p /etc/nginx/naxsi
wget -q "${EXT_LINK}"/naxsi/"${NGINXV}"/ngx_http_naxsi_module.so -O "${MODULES_PATH}"/ngx_http_naxsi_module.so
wget -q "${EXT_LINK}"/naxsi/rule/naxsi_core.rules -O /etc/nginx/naxsi/naxsi_core.rules
wget -q "${EXT_LINK}"/naxsi/rule/wordpress.rules -O /etc/nginx/naxsi/wordpress.rules
wget -q "${EXT_LINK}"/naxsi/rule/drupal.rules -O /etc/nginx/naxsi/drupal.rules
wget -q "${EXT_LINK}"/naxsi/rule/naxsi_relax.rules -O /etc/nginx/naxsi/naxsi_relax.rules
if [[ -f "${MODULES_PATH}/ngx_http_naxsi_module.so" ]]; then
LOAD_NAXSI="load_module modules/ngx_http_naxsi_module.so;"
fi
}
create_naxsi_config(){
cat >> "/etc/nginx/naxsi/disable_admin.conf" <<EOnaxsi_config
location /RequestDenied { internal; return 404; }
location /wp-admin {
try_files \$uri \$uri/ /index.php?\$args;
SecRulesDisabled;
}
location /admin {
try_files \$uri \$uri/ /index.php?\$args;
SecRulesDisabled;
}
location /admincp {
try_files \$uri \$uri/ /index.php?\$args;
SecRulesDisabled;
}
location /administrator {
try_files \$uri \$uri/ /index.php?\$args;
SecRulesDisabled;
}
EOnaxsi_config
cat >> "/etc/nginx/naxsi/enable_naxsi.conf" <<EOenable_naxsi
## Naxsi rules
#LearningMode;
#SecRulesEnabled;
SecRulesDisabled;
DeniedUrl /RequestDenied;
## check rules
CheckRule "\$SQL >= 8" BLOCK;
CheckRule "\$RFI >= 8" BLOCK;
CheckRule "\$TRAVERSAL >= 4" BLOCK;
CheckRule "\$EVADE >= 4" BLOCK;
CheckRule "\$XSS >= 8" BLOCK;
# nginx-naxsi relaxation rules
include /etc/nginx/naxsi_config/naxsi_relax.rules;
include /etc/nginx/naxsi_config/wordpress.rules;
include /etc/nginx/naxsi_config/drupal.rules;
EOenable_naxsi
}
install_naxsi(){
nginx_naxsi
create_naxsi_config
}
#Install Mariadb
install_mariadb(){
cat >> "/etc/yum.repos.d/mariadb.repo" << EOMARIADBREPO
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/${MARIADB_VERSION}/centos${OS_VER}-${OS_ARCH1}
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
EOMARIADBREPO
if [[ "${OS_VER}" -eq 8 ]]; then
dnf makecache
dnf -y install galera-4
dnf install -y MariaDB-server MariaDB-client --disablerepo=AppStream
else
yum -y install MariaDB-server MariaDB-client
fi
}
# Install php-fpm
install_php(){
if [ "${OS_VER}" -eq 8 ]; then
PHP1_VERSION=${PHP_VERSION//php}
PHP1_VERSION=$(echo "${PHP1_VERSION}" | fold -w1 | paste -sd'.')
dnf module reset php -y
dnf makecache
dnf module enable php:remi-"${PHP1_VERSION}" -y
dnf config-manager --set-enabled PowerTools
else
yum-config-manager --enable remi-"${PHP_VERSION}"
fi
yum -y install php php-fpm php-ldap php-zip php-embedded php-cli php-mysql php-common php-gd php-xml php-mbstring \
php-mcrypt php-pdo php-soap php-json php-simplexml php-process php-curl php-bcmath php-snmp php-pspell php-gmp \
php-intl php-imap php-enchant php-pear php-zlib php-xmlrpc php-devel \
php-tidy php-opcache php-cli php-pecl-zip php-dom php-ssh2 php-xmlreader php-date php-exif php-filter php-ftp \
php-hash php-iconv php-libxml php-pecl-imagick php-openssl php-pcre php-posix php-sockets php-spl \
php-tokenizer php-bz2 php-pgsql php-sqlite3 php-fileinfo
}
install_php_2(){
if [[ "${MULTI_PHP}" =~ ^(Y|y)$ ]]; then
yum -y install "${PHP_VERSION_2}" "${PHP_VERSION_2}"-php-fpm "${PHP_VERSION_2}"-php-ldap "${PHP_VERSION_2}"-php-zip "${PHP_VERSION_2}"-php-embedded "${PHP_VERSION_2}"-php-cli "${PHP_VERSION_2}"-php-mysql "${PHP_VERSION_2}"-php-common "${PHP_VERSION_2}"-php-gd "${PHP_VERSION_2}"-php-xml "${PHP_VERSION_2}"-php-mbstring \
"${PHP_VERSION_2}"-php-mcrypt "${PHP_VERSION_2}"-php-pdo "${PHP_VERSION_2}"-php-soap "${PHP_VERSION_2}"-php-json "${PHP_VERSION_2}"-php-simplexml "${PHP_VERSION_2}"-php-process "${PHP_VERSION_2}"-php-curl "${PHP_VERSION_2}"-php-bcmath "${PHP_VERSION_2}"-php-snmp "${PHP_VERSION_2}"-php-pspell "${PHP_VERSION_2}"-php-gmp \
"${PHP_VERSION_2}"-php-intl "${PHP_VERSION_2}"-php-imap "${PHP_VERSION_2}"-php-enchant "${PHP_VERSION_2}"-php-pear "${PHP_VERSION_2}"-php-zlib "${PHP_VERSION_2}"-php-xmlrpc "${PHP_VERSION_2}"-php-devel \
"${PHP_VERSION_2}"-php-tidy "${PHP_VERSION_2}"-php-opcache "${PHP_VERSION_2}"-php-cli "${PHP_VERSION_2}"-php-pecl-zip "${PHP_VERSION_2}"-php-dom "${PHP_VERSION_2}"-php-ssh2 "${PHP_VERSION_2}"-php-xmlreader "${PHP_VERSION_2}"-php-date "${PHP_VERSION_2}"-php-exif "${PHP_VERSION_2}"-php-filter "${PHP_VERSION_2}"-php-ftp \
"${PHP_VERSION_2}"-php-hash "${PHP_VERSION_2}"-php-iconv "${PHP_VERSION_2}"-php-libxml "${PHP_VERSION_2}"-php-pecl-imagick "${PHP_VERSION_2}"-php-openssl "${PHP_VERSION_2}"-php-pcre "${PHP_VERSION_2}"-php-posix "${PHP_VERSION_2}"-php-sockets "${PHP_VERSION_2}"-php-spl \
"${PHP_VERSION_2}"-php-tokenizer "${PHP_VERSION_2}"-php-bz2 "${PHP_VERSION_2}"-php-pgsql "${PHP_VERSION_2}"-php-sqlite3 "${PHP_VERSION_2}"-php-fileinfo
PHP2_RELEASE="yes"
PHP2_INI_PATH="/etc/opt/remi/${PHP_VERSION_2}/php.d"
PHP_MODULES_DIR_2="/opt/remi/${PHP_VERSION_2}/root/usr/lib64/php/modules"
if [[ ${PHP_VERSION_2} == "php56" ]]; then
PHP2_INI_PATH="/opt/remi/${PHP_VERSION_2}/root/etc/php.d"
fi
fi
}
check_duplicate_php(){
if [[ "${PHP_VERSION_2}" == "${PHP_VERSION}" ]]; then
MULTI_PHP="n"
echo "Phien ban PHP thứ 2 trung voi phien ban mac dinh. He thong se cai dat mot phien ban PHP."
fi
}
############################################
# Install Composer
############################################
install_composer(){
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
}
############################################
# Install WP-CLI
############################################
install_wpcli(){
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
mv wp-cli.phar /usr/local/bin/wp
}
############################################
# Dynamic calculation
############################################
memory_calculation(){
if [[ "${PHP_MEM}" -le '262144' ]]; then
OPCACHE_MEM='128'
MAX_MEMORY='128'
PHP_REAL_PATH_LIMIT='512k'
PHP_REAL_PATH_TTL='14400'
MAX_INPUT_VARS="6000"
elif [[ "${PHP_MEM}" -gt '262144' && "${PHP_MEM}" -le '393216' ]]; then
OPCACHE_MEM='128'
MAX_MEMORY='128'
PHP_REAL_PATH_LIMIT='640k'
PHP_REAL_PATH_TTL='21600'
MAX_INPUT_VARS="6000"
elif [[ "${PHP_MEM}" -gt '393216' && "${PHP_MEM}" -le '524288' ]]; then
OPCACHE_MEM='128'
MAX_MEMORY='128'
PHP_REAL_PATH_LIMIT='768k'
PHP_REAL_PATH_TTL='21600'
MAX_INPUT_VARS="6000"
elif [[ "${PHP_MEM}" -gt '524288' && "${PHP_MEM}" -le '1049576' ]]; then
OPCACHE_MEM='144'
MAX_MEMORY='160'
PHP_REAL_PATH_LIMIT='768k'
PHP_REAL_PATH_TTL='28800'
MAX_INPUT_VARS="6000"
elif [[ "${PHP_MEM}" -gt '1049576' && "${PHP_MEM}" -le '2097152' ]]; then
OPCACHE_MEM='160'
MAX_MEMORY='320'
PHP_REAL_PATH_LIMIT='1536k'
PHP_REAL_PATH_TTL='28800'
MAX_INPUT_VARS="6000"
elif [[ "${PHP_MEM}" -gt '2097152' && "${PHP_MEM}" -le '3145728' ]]; then
OPCACHE_MEM='192'
MAX_MEMORY='384'
PHP_REAL_PATH_LIMIT='2048k'
PHP_REAL_PATH_TTL='43200'
MAX_INPUT_VARS="6000"
elif [[ "${PHP_MEM}" -gt '3145728' && "${PHP_MEM}" -le '4194304' ]]; then
OPCACHE_MEM='224'
MAX_MEMORY='512'
PHP_REAL_PATH_LIMIT='3072k'
PHP_REAL_PATH_TTL='43200'
MAX_INPUT_VARS="6000"
elif [[ "${PHP_MEM}" -gt '4194304' && "${PHP_MEM}" -le '8180000' ]]; then
OPCACHE_MEM='288'
MAX_MEMORY='640'
PHP_REAL_PATH_LIMIT='4096k'
PHP_REAL_PATH_TTL='43200'
MAX_INPUT_VARS="10000"
elif [[ "${PHP_MEM}" -gt '8180000' && "${PHP_MEM}" -le '16360000' ]]; then
OPCACHE_MEM='320'
MAX_MEMORY='800'
PHP_REAL_PATH_LIMIT='4096k'
PHP_REAL_PATH_TTL='43200'
MAX_INPUT_VARS="10000"
elif [[ "${PHP_MEM}" -gt '16360000' && "${PHP_MEM}" -le '32400000' ]]; then
OPCACHE_MEM='480'
MAX_MEMORY='1024'
PHP_REAL_PATH_LIMIT='4096k'
PHP_REAL_PATH_TTL='43200'
MAX_INPUT_VARS="10000"
elif [[ "${PHP_MEM}" -gt '32400000' && "${PHP_MEM}" -le '64800000' ]]; then
OPCACHE_MEM='600'
MAX_MEMORY='1280'
PHP_REAL_PATH_LIMIT='4096k'
PHP_REAL_PATH_TTL='43200'
MAX_INPUT_VARS="10000"
elif [[ "${PHP_MEM}" -gt '64800000' ]]; then
OPCACHE_MEM='800'
MAX_MEMORY='2048'
PHP_REAL_PATH_LIMIT='8192k'
PHP_REAL_PATH_TTL='86400'
MAX_INPUT_VARS="10000"
fi
}
############################################
# Install Cache
############################################
# Install Memcached
install_memcached(){
if [[ "${OS_VER}" -eq 8 ]]; then
dnf -y install memcached
else
yum -y install memcached
fi
if [[ -f "/etc/sysconfig/memcached" ]]; then
mv /etc/sysconfig/memcached /etc/sysconfig/memcached.bak
cat >> "/etc/sysconfig/memcached" << EOMEMCACHED
PORT="11211"
USER="memcached"
MAXCONN="${MAX_CLIENT}"
CACHESIZE="${MAX_MEMORY}mb"
OPTIONS="-l 127.0.0.1 -U 0"
EOMEMCACHED
fi
semanage permissive -a memcached_t
}
# Install Redis
install_redis(){
yum --enablerepo=remi install redis -y
mv /etc/redis.conf /etc/redis.conf.bak
cat >> "/etc/redis.conf" << EOFREDIS
maxmemory ${MAX_MEMORY}mb
maxmemory-policy allkeys-lru
save ""
EOFREDIS
semanage permissive -a redis_t
}
# Install igbinary
install_igbinary(){
if [[ "${PHP_VERSION}" == "php56" ]]; then
IGBINARY_VERSION="2.0.8"
fi
cd "${DIR}" && wget "${PECL_PHP_LINK}"/igbinary-"${IGBINARY_VERSION}".tgz
tar -xvf igbinary-"${IGBINARY_VERSION}".tgz
cd_dir "${DIR}/igbinary-${IGBINARY_VERSION}"
/usr/bin/phpize && ./configure --with-php-config=/usr/bin/php-config
make && make install
cd "${DIR}" && rm -rf igbinary-"${IGBINARY_VERSION}" igbinary-"${IGBINARY_VERSION}".tgz
if [[ -f "${PHP_MODULES_DIR}/igbinary.so" ]]; then
cat >> "/etc/php.d/40-igbinary.ini" << EOF
extension=igbinary.so
EOF
else
echo "${INST_IGBINARY_ERR}" >> ${LOG}
fi
}
install_igbinary_2(){
if [[ "${PHP_VERSION_2}" == "php56" ]]; then
IGBINARY_VERSION="2.0.8"
fi
cd "${DIR}" && wget "${PECL_PHP_LINK}"/igbinary-"${IGBINARY_VERSION}".tgz
tar -xvf igbinary-"${IGBINARY_VERSION}".tgz
cd_dir "${DIR}/igbinary-${IGBINARY_VERSION}"
/opt/remi/"${PHP_VERSION_2}"/root/usr/bin/phpize && ./configure --with-php-config=/opt/remi/"${PHP_VERSION_2}"/root/usr/bin/php-config
make && make install
cd "${DIR}" && rm -rf igbinary-"${IGBINARY_VERSION}" igbinary-"${IGBINARY_VERSION}".tgz
if [[ -f "${PHP_MODULES_DIR_2}/igbinary.so" ]]; then
cat >> "${PHP2_INI_PATH}/40-igbinary.ini" << EOF
extension=igbinary.so
EOF
else
echo "${INST_IGBINARY_ERR_2}" >> ${LOG}
fi
}
# Install Php memcached extension
install_php_memcached(){
if [[ "${PHP_VERSION}" == "php56" ]]; then
PHP_MEMCACHED_VERSION="2.2.0"
fi
cd "${DIR}" && wget "${PECL_PHP_LINK}"/memcached-"${PHP_MEMCACHED_VERSION}".tgz
tar -xvf memcached-"${PHP_MEMCACHED_VERSION}".tgz
cd_dir "${DIR}/memcached-${PHP_MEMCACHED_VERSION}"
/usr/bin/phpize && ./configure --enable-memcached-igbinary --with-php-config=/usr/bin/php-config
make && make install
cd "${DIR}" && rm -rf memcached-"${PHP_MEMCACHED_VERSION}".tgz memcached-"${PHP_MEMCACHED_VERSION}"
if [[ -f "${PHP_MODULES_DIR}/memcached.so" ]]; then
cat >> "/etc/php.d/50-memcached.ini" << EOF
extension=memcached.so
EOF
else
echo "${INST_MEMEXT_ERR}" >> ${LOG}
fi
}
install_php_memcached_2(){
if [[ "${PHP_VERSION_2}" == "php56" ]]; then
PHP_MEMCACHED_VERSION="2.2.0"
fi
cd "${DIR}" && wget "${PECL_PHP_LINK}"/memcached-"${PHP_MEMCACHED_VERSION}".tgz
tar -xvf memcached-"${PHP_MEMCACHED_VERSION}".tgz
cd_dir "${DIR}/memcached-${PHP_MEMCACHED_VERSION}"
/opt/remi/"${PHP_VERSION_2}"/root/usr/bin/phpize && ./configure --enable-memcached-igbinary --with-php-config=/opt/remi/"${PHP_VERSION_2}"/root/usr/bin/php-config
make && make install
cd "${DIR}" && rm -rf memcached-"${PHP_MEMCACHED_VERSION}".tgz memcached-"${PHP_MEMCACHED_VERSION}"
if [[ -f "${PHP_MODULES_DIR_2}/memcached.so" ]]; then
cat >> "${PHP2_INI_PATH}/50-memcached.ini" << EOF
extension=memcached.so
EOF
else
echo "${INST_MEMEXT_ERR_2}" >> ${LOG}
fi
}
# Install Phpredis
install_php_redis(){
if [[ "${PHP_VERSION}" == "php56" ]]; then
PHP_REDIS_VERSION="4.3.0"
fi
cd "${DIR}" && wget "${PECL_PHP_LINK}"/redis-"${PHP_REDIS_VERSION}".tgz
tar -xvf redis-"${PHP_REDIS_VERSION}".tgz
cd_dir "${DIR}/redis-${PHP_REDIS_VERSION}"
/usr/bin/phpize && ./configure --enable-redis-igbinary --with-php-config=/usr/bin/php-config
make && make install
cd "${DIR}" && rm -rf redis-"${PHP_REDIS_VERSION}".tgz redis-"${PHP_REDIS_VERSION}"
if [[ -f "${PHP_MODULES_DIR}/redis.so" ]]; then
cat >> "/etc/php.d/50-redis.ini" << EOF
extension=redis.so
EOF
else
echo "${INST_PHPREDIS_ERR}" >> ${LOG}
fi
}
install_php_redis_2(){
if [[ "${PHP_VERSION_2}" == "php56" ]]; then
PHP_REDIS_VERSION="4.3.0"
fi
cd "${DIR}" && wget "${PECL_PHP_LINK}"/redis-"${PHP_REDIS_VERSION}".tgz
tar -xvf redis-"${PHP_REDIS_VERSION}".tgz
cd_dir "${DIR}/redis-${PHP_REDIS_VERSION}"
/opt/remi/"${PHP_VERSION_2}"/root/usr/bin/phpize && ./configure --enable-redis-igbinary --with-php-config=/opt/remi/"${PHP_VERSION_2}"/root/usr/bin/php-config
make && make install
cd "${DIR}" && rm -rf redis-"${PHP_REDIS_VERSION}".tgz redis-"${PHP_REDIS_VERSION}"
if [[ -f "${PHP_MODULES_DIR_2}/redis.so" ]]; then
cat >> "${PHP2_INI_PATH}/50-redis.ini" << EOF
extension=redis.so
EOF
else
echo "${INST_PHPREDIS_ERR_2}" >> "${LOG}"
fi
}
############################################
# Config Nginx
############################################
# dynamic SSL cache size calculation
cal_ssl_cache_size(){
if [[ ${RAM_TOTAL} -gt 500000 && ${RAM_TOTAL} -le 800000 ]]; then
SSL_CACHE_SIZE=20
elif [[ ${RAM_TOTAL} -gt 800000 && ${RAM_TOTAL} -le 1000000 ]]; then
SSL_CACHE_SIZE=40
elif [[ ${RAM_TOTAL} -gt 1000000 && ${RAM_TOTAL} -le 1880000 ]]; then
SSL_CACHE_SIZE=60
elif [[ ${RAM_TOTAL} -gt 1880000 && ${RAM_TOTAL} -le 2890000 ]]; then
SSL_CACHE_SIZE=80
elif [[ ${RAM_TOTAL} -gt 2890000 && ${RAM_TOTAL} -le 3890000 ]]; then
SSL_CACHE_SIZE=150
elif [[ ${RAM_TOTAL} -gt 3890000 && ${RAM_TOTAL} -le 7800000 ]]; then
SSL_CACHE_SIZE=300
elif [[ ${RAM_TOTAL} -gt 7800000 && ${RAM_TOTAL} -le 15600000 ]]; then
SSL_CACHE_SIZE=500
elif [[ ${RAM_TOTAL} -gt 15600000 && ${RAM_TOTAL} -le 23600000 ]]; then
SSL_CACHE_SIZE=1000
elif [[ ${RAM_TOTAL} -gt 23600000 ]]; then
SSL_CACHE_SIZE=2000
else
SSL_CACHE_SIZE=10
fi
}
self_signed_ssl(){
#Create dhparams
challenge_password=$(generate_random_pwd)
self_signed_dir="/etc/nginx/ssl/server"
mkdir -p "${self_signed_dir}"
openssl dhparam -out /etc/nginx/ssl/dhparams.pem 2048
openssl genrsa -out "${self_signed_dir}/server.key" 4096
openssl req -new -days 3650 -key "${self_signed_dir}/server.key" -out "${self_signed_dir}/server.csr" <<EOF
VN
Cau Giay
Ha Noi
${AUTHOR}
IT
${IPADDRESS}
${ADMIN_EMAIL}
${challenge_password}
${AUTHOR}
EOF
openssl x509 -in "${self_signed_dir}/server.csr" -out "${self_signed_dir}/server.crt" -req -signkey "${self_signed_dir}/server.key" -days 3650
}
create_nginx_conf(){
mkdir -p /etc/nginx/backup_vhost
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig
cat >> "/etc/nginx/nginx.conf" << EONGINXCONF
user nginx;
worker_processes ${NGINX_PROCESSES};
worker_rlimit_nofile 260000;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
${LOAD_BROTLI_FILTER}
${LOAD_BROTLI_STATIC}
${LOAD_NAXSI}
events {
worker_connections ${MAX_CLIENT};
accept_mutex off;
accept_mutex_delay 200ms;
use epoll;
#multi_accept on;
}
http {
index index.html index.htm index.php;
include /etc/nginx/mime.types;
default_type application/octet-stream;
charset utf-8;
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
access_log off;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
types_hash_max_size 2048;
server_names_hash_bucket_size 128;
server_names_hash_max_size 10240;
client_max_body_size 1024m;
client_body_buffer_size 128k;
client_body_in_file_only off;
client_body_timeout 60s;
client_header_buffer_size 256k;
client_header_timeout 20s;
large_client_header_buffers 8 256k;
keepalive_timeout 15;
keepalive_disable msie6;
reset_timedout_connection on;
send_timeout 60s;
disable_symlinks if_not_owner from=\$document_root;
server_name_in_redirect off;
open_file_cache max=2000 inactive=20s;
open_file_cache_valid 120s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
# Limit Request
limit_req_status 403;
limit_conn_zone \$binary_remote_addr zone=one:10m;
limit_req_zone \$binary_remote_addr zone=two:10m rate=1r/s;
# Custom Response Headers
add_header X-Powered-By ${AUTHOR};
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# Custom Variables
map \$scheme \$https_suffix { default ''; https '-https'; }
include /etc/nginx/extra/gzip.conf;
${INCLUDE_BROTLI}
include /etc/nginx/extra/ssl.conf;
include /etc/nginx/extra/cloudflare.conf;
include /etc/nginx/extra/webp.conf;
include /etc/nginx/web_apps.conf;
include /etc/nginx/conf.d/*.conf;
}
EONGINXCONF
}
create_wp_cache_conf(){
mkdir -p /etc/nginx/wordpress
cat >> "/etc/nginx/wordpress/disable_xmlrpc.conf" << EOxmlrpc
# Disable XML-RPC
location = xmlrpc.php { deny all; access_log off; log_not_found off; }
EOxmlrpc
cat >> "/etc/nginx/wordpress/disable_user_api.conf" << EOuser_api
#Block API User
location ~* /wp-json/wp/v2/users {
allow 127.0.0.1;
deny all;
access_log off;
log_not_found off;
}
EOuser_api
cat >> "/etc/nginx/wordpress/webp_image.conf" << EOwebp_image
# webp extension
location ~ ^/wp-content/uploads/ {
location ~* ^/wp-content/uploads/(.+/)?(.+)\.(png|jpe?g)\$ {
expires 97d;
add_header Vary "Accept";
add_header Cache-Control "public, no-transform";
try_files \$uri\$webp_extension \$uri =404;
}
}
EOwebp_image
cat >> "/etc/nginx/wordpress/wordpress_secure.conf" << EOwpsecure
include /etc/nginx/wordpress/disable_user_api.conf;
rewrite /wp-admin$ \$scheme://\$host\$uri/ permanent;
location /wp-includes/{
location ~ \.(gz|tar|bzip2|7z|php|php5|php7|log|error|py|pl|kid|love|cgi)\$ {
deny all;
}
}