-
Notifications
You must be signed in to change notification settings - Fork 86
/
magenx.install.latest.sh
2432 lines (2244 loc) · 86.7 KB
/
magenx.install.latest.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
#=================================================================================#
# MagenX e-commerce stack for Magento 2 #
# Copyright (C) 2013-present [email protected] #
# All rights reserved. #
#=================================================================================#
SELF=$(basename $0)
MAGENX_VERSION=$(curl -s https://api.github.com/repos/magenx/Magento-2-server-installation/tags 2>&1 | head -3 | grep -oP '(?<=")\d.*(?=")')
MAGENX_BASE="https://magenx.sh"
###################################################################################
### REPOSITORY AND PACKAGES ###
###################################################################################
# Github installation repository raw url
MAGENX_INSTALL_GITHUB_REPO="https://raw.githubusercontent.com/magenx/Magento-2-server-installation/master"
# Magento
VERSION_LIST=$(curl -s https://api.github.com/repos/magento/magento2/tags 2>&1 | grep -oP '(?<=name": ").*(?=")' | sort -r)
PROJECT="composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition"
COMPOSER_NAME="8c681734f22763b50ea0c29dff9e7af2"
COMPOSER_PASSWORD="02dfee497e669b5db1fe1c8d481d6974"
## Version lock
COMPOSER_VERSION="2.2"
RABBITMQ_VERSION="3.12*"
MARIADB_VERSION="10.11"
OPENSEARCH_VERSION="2.x"
VARNISH_VERSION="73"
REDIS_VERSION="7"
NODE_VERSION="20"
NVM_VERSION="0.40.0"
# Repositories
MARIADB_REPO_CONFIG="https://downloads.mariadb.com/MariaDB/mariadb_repo_setup"
# Nginx configuration
NGINX_VERSION=$(curl -s http://nginx.org/en/download.html | grep -oP '(?<=gz">nginx-).*?(?=</a>)' | head -1)
MAGENX_NGINX_GITHUB_REPO="https://raw.githubusercontent.com/magenx/Magento-nginx-config/master/"
MAGENX_NGINX_GITHUB_REPO_API="https://api.github.com/repos/magenx/Magento-nginx-config/contents/magento2"
# Debug Tools
MYSQL_TUNER="https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl"
# Malware detector
MALDET="https://www.rfxn.com/downloads/maldetect-current.tar.gz"
# WebStack Packages .deb
WEB_STACK_CHECK="mysql* rabbitmq* elasticsearch opensearch percona-server* maria* php* nginx* ufw varnish* certbot* redis* webmin"
EXTRA_PACKAGES="curl jq gnupg2 auditd apt-transport-https apt-show-versions ca-certificates lsb-release make autoconf snapd automake libtool uuid-runtime \
perl openssl unzip screen nfs-common inotify-tools iptables smartmontools mlocate vim wget sudo apache2-utils \
logrotate git netcat-openbsd patch ipset postfix strace rsyslog moreutils lsof sysstat acl attr iotop expect imagemagick snmp"
PERL_MODULES="liblwp-protocol-https-perl libdbi-perl libconfig-inifiles-perl libdbd-mysql-perl libterm-readkey-perl"
PHP_PACKAGES=(cli fpm common mysql zip lz4 gd mbstring curl xml bcmath intl ldap soap oauth apcu)
###################################################################################
### COLORS ###
###################################################################################
RED="\e[31;40m"
GREEN="\e[32;40m"
YELLOW="\e[33;40m"
WHITE="\e[37;40m"
BLUE="\e[0;34m"
### Background
DGREYBG=" \e[100m"
BLUEBG=" \e[1;44m"
REDBG=" \e[41m"
### Styles
BOLD="\e[1m"
### Reset
RESET="\e[0m"
###################################################################################
### ECHO MESSAGES DESIGN ###
###################################################################################
WHITETXT () {
MESSAGE=${@:-"${RESET}Error: No message passed"}
echo -e " ${WHITE}${MESSAGE}${RESET}"
}
BLUETXT () {
MESSAGE=${@:-"${RESET}Error: No message passed"}
echo -e " ${BLUE}${MESSAGE}${RESET}"
}
REDTXT () {
MESSAGE=${@:-"${RESET}Error: No message passed"}
echo -e " ${RED}${MESSAGE}${RESET}"
}
GREENTXT () {
MESSAGE=${@:-"${RESET}Error: No message passed"}
echo -e " ${GREEN}${MESSAGE}${RESET}"
}
YELLOWTXT () {
MESSAGE=${@:-"${RESET}Error: No message passed"}
echo -e " ${YELLOW}${MESSAGE}${RESET}"
}
BLUEBG () {
MESSAGE=${@:-"${RESET}Error: No message passed"}
echo -e "${BLUEBG}${MESSAGE}${RESET}"
}
pause () {
read -p " $*"
}
_echo () {
echo -en " $@"
}
PACKAGES_INSTALLED () {
GREENTXT "Installed: "
apt -qq list --installed $@ 2>/dev/null | awk '{print " " $0}'
}
###################################################################################
### ARROW KEYS UP/DOWN MENU ###
###################################################################################
updown_menu () {
i=1;for items in $(echo $1); do item[$i]="${items}"; let i=$i+1; done
i=1
echo -e "\n Use up/down arrow keys then press [ Enter ] to select $2"
while [ 0 ]; do
if [ "$i" -eq 0 ]; then i=1; fi
if [ ! "${item[$i]}" ]; then let i=i-1; fi
echo -en "\r "
echo -en "\r${item[$i]}"
read -sn 1 selector
case "${selector}" in
"B") let i=i+1;;
"A") let i=i-1;;
"") echo; read -sn 1 -p " [?] To confirm [ "$(echo -e $BOLD${item[$i]}$RESET)" ] press "$(echo -e $BOLD$GREEN"y"$RESET)" or "$(echo -e $BOLD$RED"n"$RESET)" for new selection" confirm
if [[ "${confirm}" =~ ^[Yy]$ ]]; then
printf -v "$2" '%s' "${item[$i]}"
break
else
echo
echo -e "\n Use up/down arrow keys then press [ Enter ] to select $2"
fi
;;
esac
done }
###################################################################################
### CHECK IF ROOT AND CREATE DATABASE TO SAVE ALL SETTINGS ###
###################################################################################
echo ""
echo ""
# root?
if [[ ${EUID} -ne 0 ]]; then
echo
REDTXT "[!] This script must be run as root user!"
YELLOWTXT "[!] Login as root and run this script again."
exit 1
else
GREENTXT "PASS: ROOT!"
fi
# Config path
MAGENX_CONFIG_PATH="/opt/magenx/config"
if [ ! -d "${MAGENX_CONFIG_PATH}" ]; then
mkdir -p ${MAGENX_CONFIG_PATH}
fi
# SQLite check, create database path and command
if ! which sqlite3 >/dev/null; then
echo ""
YELLOWTXT "[!] SQLite is not installed on this system!"
YELLOWTXT "[!] Installing..."
echo ""
echo ""
apt update
apt -y install sqlite3
fi
SQLITE3_DB="magenx.db"
SQLITE3_DB_PATH="${MAGENX_CONFIG_PATH}/${SQLITE3_DB}"
SQLITE3="sqlite3 ${SQLITE3_DB_PATH}"
if [ ! -f "${SQLITE3_DB_PATH}" ]; then
${SQLITE3} "" ""
# Create base tables to save configuration
${SQLITE3} "CREATE TABLE IF NOT EXISTS system(
machine_id text,
distro_name text,
distro_version text,
web_stack text,
timezone text,
system_test text,
ssh_port text,
terms text,
system_update text,
php_version text,
phpmyadmin_password text,
webmin_password text,
mysql_root_password text,
opensearch_admin_password text
);"
${SQLITE3} "CREATE TABLE IF NOT EXISTS magento(
env text,
mode text,
redis_password text,
rabbitmq_password text,
indexer_password text,
version_installed text,
domain text,
owner text,
php_user text,
root_path text,
database_host text,
database_name text,
database_user text,
database_password text,
admin_login text,
admin_password text,
admin_email text,
locale text,
admin_path text,
crypt_key text,
tfa_key text,
private_ssh_key text,
public_ssh_key text,
github_actions_private_ssh_key text,
github_actions_public_ssh_key text
);"
${SQLITE3} "CREATE TABLE IF NOT EXISTS menu(
lemp text,
magento text,
database text,
install text,
config text,
csf text,
webmin text
);"
${SQLITE3} "INSERT INTO menu (lemp, magento, database, install, config, csf, webmin)
VALUES('-', '-', '-', '-', '-', '-', '-');"
fi
###################################################################################
### CHECK IF WE CAN RUN IT ###
###################################################################################
## Ubuntu Debian
## Distro detectction
distro_error() {
echo ""
REDTXT "[!] ${OS_NAME} ${OS_VERSION} detected"
echo ""
echo " Unfortunately, your operating system distribution and version are not supported by this script"
echo " Supported: Ubuntu 20|22.04; Debian 11|12"
echo " Please email [email protected] and let us know if you run into any issues"
echo ""
exit 1
}
# Check if distribution name and version are already in the database
DISTRO_INFO=($(${SQLITE3} -list -separator ' ' "SELECT distro_name, distro_version FROM system;"))
if [ -n "${DISTRO_INFO[0]}" ]; then
DISTRO_NAME="${DISTRO_INFO[0]}"
DISTRO_VERSION="${DISTRO_INFO[1]}"
GREENTXT "PASS: [ ${DISTRO_NAME} ${DISTRO_VERSION} ]"
else
# Detect distribution name and version
if [ -f "/etc/os-release" ]; then
. /etc/os-release
DISTRO_NAME="${NAME}"
DISTRO_VERSION="${VERSION_ID}"
# Check if distribution is supported
if [ "${DISTRO_NAME%% *}" == "Ubuntu" ] && [[ "${DISTRO_VERSION}" =~ ^(20.04|22.04) ]]; then
DISTRO_NAME="Ubuntu"
elif [ "${DISTRO_NAME%% *}" == "Debian" ] && [[ "${DISTRO_VERSION}" =~ ^(11|12) ]]; then
DISTRO_NAME="Debian"
else
distro_error
fi
# Confirm distribution detection with user input
echo ""
_echo "${YELLOW}[?]${REDBG}${BOLD}[ ${DISTRO_NAME} ${DISTRO_VERSION} ]${RESET} ${YELLOW}detected correctly ? [y/n][n]: ${RESET}"
read distro_detect
if [ "${distro_detect}" = "y" ]; then
echo ""
GREENTXT "PASS: [ ${DISTRO_NAME} ${DISTRO_VERSION} ]"
# Get machine id
MACHINE_ID="$(cat /etc/machine-id)"
${SQLITE3} "INSERT INTO system (machine_id, distro_name, distro_version) VALUES ('${MACHINE_ID}', '${DISTRO_NAME}', '${DISTRO_VERSION}');"
else
distro_error
fi
else
distro_error
fi
fi
# network is up?
host1=${MAGENX_BASE}
host2=github.com
RESULT=$(((ping -w3 -c2 ${host1} || ping -w3 -c2 ${host2}) > /dev/null 2>&1) && echo "up" || (echo "down" && exit 1))
if [[ ${RESULT} == up ]]; then
GREENTXT "PASS: NETWORK IS UP. GREAT, LETS START!"
else
echo ""
REDTXT "[!] Network is down ?"
YELLOWTXT "[!] Please check your network settings."
echo ""
echo ""
exit 1
fi
# install packages to run CPU and HDD test
dpkg-query -l curl time bc bzip2 tar >/dev/null || { echo; echo; apt update -o Acquire::ForceIPv4=true; apt -y install curl time bc bzip2 tar; }
# check if you need self update
MD5_NEW=$(curl -sL ${MAGENX_BASE} > ${SELF}.new && md5sum ${SELF}.new | awk '{print $1}')
MD5=$(md5sum ${SELF} | awk '{print $1}')
if [[ "${MD5_NEW}" == "${MD5}" ]]; then
GREENTXT "PASS: INTEGRITY CHECK FOR '${SELF}' OK"
rm ${SELF}.new
elif [[ "${MD5_NEW}" != "${MD5}" ]]; then
echo ""
YELLOWTXT "Integrity check for '${SELF}'"
YELLOWTXT "detected different md5 checksum"
YELLOWTXT "remote repository file has some changes"
echo ""
REDTXT "IF YOU HAVE LOCAL CHANGES REMOVE INTEGRITY CHECK OR SKIP UPDATES"
echo ""
_echo "[?] Would you like to update the file now? [y/n][y]: "
read update_agree
if [ "${update_agree}" == "y" ];then
mv ${SELF}.new ${SELF}
echo ""
GREENTXT "The file has been upgraded, please run it again"
echo ""
exit 1
else
echo ""
YELLOWTXT "New file saved to ${SELF}.new"
echo
fi
fi
# check if memory is enough
TOTALMEM=$(awk '/MemTotal/{print $2}' /proc/meminfo | xargs -I {} echo "scale=4; {}/1024^2" | bc | xargs printf "%1.0f")
if [ "${TOTALMEM}" -ge "4" ]; then
GREENTXT "PASS: TOTAL RAM [${TOTALMEM}Gb]"
else
echo
REDTXT "[!] Total RAM less than ${BOLD}4Gb"
YELLOWTXT "[!] To run complete stack you need more RAM"
echo
fi
# check if web stack is clean
WEB_STACK=$(${SQLITE3} "SELECT web_stack FROM system;")
if [ "${WEB_STACK}" != "magenx" ]; then
installed_packages="$(apt -qq list --installed ${WEB_STACK_CHECK} 2> /dev/null | cut -d'/' -f1 | tr '\n' ' ')"
if [ ! -z "$installed_packages" ]; then
REDTXT "[!] Some webstack packages already installed"
YELLOWTXT "[!] You need to remove them or reinstall minimal OS version"
echo
echo -e "\t\t apt -y remove ${installed_packages}"
echo
echo
exit 1
else
# set web_stack clean
${SQLITE3} "UPDATE system SET web_stack = 'magenx';"
fi
fi
# print path
GREENTXT "PATH: ${PATH}"
# configure system/magento timezone
TIMEZONE="$(${SQLITE3} "SELECT timezone FROM system;")"
if [ -z "${TIMEZONE}" ]; then
echo ""
echo ""
YELLOWTXT "[!] Server and Magento timezone configuration:"
echo ""
pause "[] Press [Enter] key to proceed"
echo ""
dpkg-reconfigure tzdata
TIMEZONE=$(timedatectl | awk '/Time zone:/ {print $3}')
${SQLITE3} "UPDATE system SET timezone = '${TIMEZONE}';"
fi
GREENTXT "TIMEZONE: ${TIMEZONE}"
echo
echo
SYSTEM_TEST=$(${SQLITE3} "SELECT system_test FROM system;")
if [ -z "${SYSTEM_TEST}" ]; then
echo
BLUEBG "~ QUICK SYSTEM TEST ~"
WHITETXT "-------------------------------------------------------------------------------------"
echo
# run I/O and CPU tests
TEST_FILE="TEST_FILE__$$"
TAR_FILE="TAR_FILE"
_echo "${YELLOW}[?] I/O PERFORMANCE${RESET}:"
IO=$( ( dd if=/dev/zero of=${TEST_FILE} bs=64k count=16k conv=fdatasync && rm -f ${TEST_FILE} ) 2>&1 | awk -F, '{IO=$NF} END { print IO}' )
_echo ${IO}
echo
_echo "${YELLOW}[?] CPU PERFORMANCE${RESET}:"
dd if=/dev/urandom of=${TAR_FILE} bs=1024 count=25000 >>/dev/null 2>&1
CPU_TIME=$( (/usr/bin/time -f "%es" tar cfj ${TAR_FILE}.bz2 ${TAR_FILE}) 2>&1 )
rm -f ${TAR_FILE}*
_echo ${CPU_TIME}
echo
echo
echo
# set system_test tested
${SQLITE3} "UPDATE system SET system_test = 'I/O:${IO} CPU:${CPU_TIME}';"
echo
pause "[] Press [Enter] key to proceed"
echo
fi
echo
# ssh port test
SSH_PORT=$(${SQLITE3} "SELECT ssh_port FROM system;")
if [ -z "${SSH_PORT}" ]; then
echo
sed -i "s/.*LoginGraceTime.*/LoginGraceTime 30/" /etc/ssh/sshd_config
sed -i "s/.*MaxAuthTries.*/MaxAuthTries 6/" /etc/ssh/sshd_config
sed -i "s/.*X11Forwarding.*/X11Forwarding no/" /etc/ssh/sshd_config
sed -i "s/.*PrintLastLog.*/PrintLastLog yes/" /etc/ssh/sshd_config
sed -i "s/.*TCPKeepAlive.*/TCPKeepAlive yes/" /etc/ssh/sshd_config
sed -i "s/.*ClientAliveInterval.*/ClientAliveInterval 600/" /etc/ssh/sshd_config
sed -i "s/.*ClientAliveCountMax.*/ClientAliveCountMax 3/" /etc/ssh/sshd_config
sed -i "s/.*UseDNS.*/UseDNS no/" /etc/ssh/sshd_config
sed -i "s/.*PrintMotd.*/PrintMotd no/" /etc/ssh/sshd_config
echo
SSH_PORT="$(awk '/#?Port [0-9]/ {print $2}' /etc/ssh/sshd_config)"
if [ "${SSH_PORT}" == "22" ]; then
REDTXT "[!] Default ssh port :22 detected"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.BACK
SSH_PORT_NEW=$(shuf -i 9537-9554 -n 1)
sed -i "s/.*Port 22/Port ${SSH_PORT_NEW}/g" /etc/ssh/sshd_config
SSH_PORT=${SSH_PORT_NEW}
fi
echo
GREENTXT "SSH port and settings were updated - OK"
echo
GREENTXT "[!] SSH Port: ${SSH_PORT}"
echo
systemctl restart sshd.service
ss -tlp | grep sshd
echo
echo
REDTXT "[!] IMPORTANT: Now open new SSH session with the new port!"
REDTXT "[!] IMPORTANT: Do not close your current session!"
echo
_echo "[?] Have you logged in another session? [y/n][n]: "
read ssh_test
if [ "${ssh_test}" == "y" ]; then
echo
GREENTXT "[!] SSH Port: ${SSH_PORT}"
echo
${SQLITE3} "UPDATE system SET ssh_port = '${SSH_PORT}';"
echo
echo
pause "[] Press [Enter] key to proceed"
else
echo
mv /etc/ssh/sshd_config.BACK /etc/ssh/sshd_config
REDTXT "Restoring sshd_config file back to defaults ${GREEN} [ok]"
systemctl restart sshd.service
echo
GREENTXT "SSH port has been restored - OK"
ss -tlp | grep sshd
fi
fi
# Lets set magento mode/environment type to configure
ENV=($(${SQLITE3} "SELECT DISTINCT env FROM magento;"))
if [ ${#ENV[@]} -eq 0 ]; then
echo ""
echo ""
echo ""
YELLOWTXT "[?] Select magento environment type and mode:"
echo ""
_echo "${BOLD}production${RESET} - write disabled! production mode.
${BOLD}staging${RESET} - write disabled! production mode.
${BOLD}developer${RESET} - write enabled! developer mode.
${BOLD}all_3${RESET} - configure all 3 environments on this server"
echo ""
updown_menu "production staging developer all_3" ENV_SELECTED
if [ "${ENV_SELECTED}" == "all_3" ]; then
ENV=("production" "staging" "developer")
else
ENV=("${ENV_SELECTED}")
fi
for ENV_SELECTED in "${ENV[@]}"
do
# magento mode? LOL
# if magento is running in production, staging, developer environment and has production mode, default mode, developer mode
# and in the production environment it runs by default, should the default mode in production environment be the default mode?
# since having a default mode in between production and developer is kind of brain-crap...
# feels like the default mode should be staging mode to run in staging environment, or remove it completely
# cause staging environment runs in production mode as well. maybe that would make some sense then.
[[ "${ENV_SELECTED}" == "staging" ]] && MODE="production" || MODE="${ENV_SELECTED}"
${SQLITE3} "INSERT INTO magento (env, mode) VALUES ('${ENV_SELECTED}', '${MODE}');"
done
else
GREENTXT "ENVIRONMENT: ${ENV[@]}"
fi
# Enter domain name and ssh user per environment
DOMAIN=($(${SQLITE3} "SELECT DISTINCT domain FROM magento;"))
if [ ${#DOMAIN[@]} -eq 0 ]; then
echo ""
echo ""
echo ""
ENV=($(${SQLITE3} "SELECT DISTINCT env FROM magento;"))
for ENV_SELECTED in "${ENV[@]}"
do
echo ""
read -e -p "$(echo -e ${YELLOW}" [?] Store domain name for [ ${ENV_SELECTED} ]: "${RESET})" -i "yourdomain.tld" DOMAIN
read -e -p "$(echo -e ${YELLOW}" [?] Files owner/SSH user for [ ${ENV_SELECTED} ]: "${RESET})" -i "${DOMAIN//[-.]/}" OWNER
${SQLITE3} "UPDATE magento SET
domain = '${DOMAIN}',
owner = '${OWNER}',
php_user = 'php-${OWNER}',
root_path = '/home/${OWNER}/public_html'
WHERE
env = '${ENV_SELECTED}'
;"
done
else
GREENTXT "DOMAINS: ${DOMAIN[@]}"
fi
echo ""
echo ""
###################################################################################
### AGREEMENT ###
###################################################################################
echo ""
TERMS=$(${SQLITE3} "SELECT terms FROM system;")
if [ "${TERMS}" != "agreed" ]; then
echo
YELLOWTXT "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
YELLOWTXT "BY INSTALLING THIS SOFTWARE AND BY USING ANY AND ALL SOFTWARE"
YELLOWTXT "YOU ACKNOWLEDGE AND AGREE:"
echo
YELLOWTXT "THIS SOFTWARE AND ALL SOFTWARE PROVIDED IS PROVIDED AS IS"
YELLOWTXT "UNSUPPORTED AND WE ARE NOT RESPONSIBLE FOR ANY DAMAGE"
echo
YELLOWTXT "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo
echo
_echo "[?] Do you agree to these terms ? [y/n][y]: "
read terms_agree
if [ "${terms_agree}" == "y" ]; then
# set terms agreed
${SQLITE3} "UPDATE system SET terms = 'agreed';"
else
REDTXT "Going out."
echo
exit 1
fi
fi
###################################################################################
### MAIN MENU ###
###################################################################################
showMenu () {
MENU_CHECK=($(${SQLITE3} -list -separator ' ' "SELECT lemp, magento, database, install, config, csf, webmin FROM menu;"))
printf "\033c"
echo ""
echo ""
echo -e "${DGREYBG}${BOLD} MAGENTO SERVER CONFIGURATION v.${MAGENX_VERSION} ${RESET}"
BLUETXT ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
echo ""
WHITETXT "[${MENU_CHECK[0]}] Install repository and LEMP packages : ${YELLOW}\tlemp"
WHITETXT "[${MENU_CHECK[1]}] Download Magento latest version : ${YELLOW}\tmagento"
WHITETXT "[${MENU_CHECK[2]}] Setup Magento database : ${YELLOW}\tdatabase"
WHITETXT "[${MENU_CHECK[3]}] Install Magento no sample data : ${YELLOW}\tinstall"
WHITETXT "[${MENU_CHECK[4]}] Post-Installation config : ${YELLOW}\tconfig"
echo ""
BLUETXT ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
echo ""
WHITETXT "[${MENU_CHECK[5]}] Install CSF Firewall : ${YELLOW}\tfirewall"
WHITETXT "[${MENU_CHECK[6]}] Install Webmin control panel : ${YELLOW}\twebmin"
echo ""
BLUETXT ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"
echo ""
WHITETXT "[-] To quit and exit : ${RED}\texit"
echo ""
echo ""
}
while [ 1 ]
do
showMenu
read CHOICE
case "${CHOICE}" in
"lemp")
echo ""
echo ""
###################################################################################
### SYSTEM UPGRADE ###
###################################################################################
# Get distro_name to make sure its set
DISTRO_NAME=$(${SQLITE3} "SELECT distro_name FROM system;")
# check if system update still required
SYSTEM_UPDATE=$(${SQLITE3} "SELECT system_update FROM system;")
if [ -z "${SYSTEM_UPDATE}" ]; then
## install all extra packages
echo
BLUEBG "[~] SYSTEM UPDATE AND PACKAGES INSTALLATION [~]"
WHITETXT "-------------------------------------------------------------------------------------"
echo ""
debconf-set-selections <<< "postfix postfix/mailname string localhost"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Local only'"
apt update && apt upgrade -y
apt -y install software-properties-common
apt-add-repository -y contrib
apt update
DEBIAN_FRONTEND=noninteractive apt -y install ${EXTRA_PACKAGES} ${PERL_MODULES}
echo ""
if [ "$?" != 0 ]; then
echo ""
REDTXT "[!] Installation error."
REDTXT "[!] Please correct errors and run it again."
exit 1
echo ""
fi
# Set system_update to full release version
[ "${DISTRO_NAME}" == "Debian" ] && FULL_VERSION="$(cat /etc/debian_version )" || FULL_VERSION="$(lsb_release -d | awk '/(20|22)\.04.+/{print $3}')"
${SQLITE3} "UPDATE system SET system_update = 'installed @ ${FULL_VERSION}';"
echo ""
fi
echo ""
echo ""
BLUEBG "[~] LEMP WEB STACK INSTALLATION [~]"
WHITETXT "-------------------------------------------------------------------------------------"
echo ""
echo ""
_echo "${YELLOW}[?] Install MariaDB ${MARIADB_VERSION} database ? [y/n][n]:${RESET} "
read mariadb_install
if [ "${mariadb_install}" == "y" ]; then
echo
curl -sS ${MARIADB_REPO_CONFIG} | bash -s -- --mariadb-server-version="mariadb-${MARIADB_VERSION}" --skip-maxscale --skip-verify --skip-eol-check
echo
if [ "$?" = 0 ] # if repository installed then install package
then
echo
GREENTXT "MariaDB repository installed - OK"
echo
YELLOWTXT "MariaDB ${MARIADB_VERSION} database installation:"
echo
apt update
apt install -y mariadb-server
if [ "$?" = 0 ] # if package installed then configure
then
echo
GREENTXT "MariaDB installed - OK"
echo
systemctl enable mariadb
echo
PACKAGES_INSTALLED mariadb*
echo "127.0.0.1 mariadb" >> /etc/hosts
echo
WHITETXT "Downloading my.cnf file from MagenX Github repository"
curl -sSo /etc/my.cnf https://raw.githubusercontent.com/magenx/magento-mysql/master/my.cnf/my.cnf
echo
WHITETXT "[?] Calculating [innodb_buffer_pool_size]:"
INNODB_BUFFER_POOL_SIZE=$(echo "0.5*$(awk '/MemTotal/ { print $2 / (1024*1024)}' /proc/meminfo | cut -d'.' -f1)" | bc | xargs printf "%1.0f")
if [ "${INNODB_BUFFER_POOL_SIZE}" == "0" ]; then IBPS=1; fi
sed -i "s/innodb_buffer_pool_size = 4G/innodb_buffer_pool_size = ${INNODB_BUFFER_POOL_SIZE}G/" /etc/my.cnf
##sed -i "s/innodb_buffer_pool_instances = 4/innodb_buffer_pool_instances = ${INNODB_BUFFER_POOL_SIZE}/" /etc/my.cnf
echo
WHITETXT "innodb_buffer_pool_size = ${INNODB_BUFFER_POOL_SIZE}G"
WHITETXT "innodb_buffer_pool_instances = ${INNODB_BUFFER_POOL_SIZE}"
echo
else
echo
REDTXT "MariaDB installation error"
exit # if package is not installed then exit
fi
else
echo
REDTXT "MariaDB repository installation error"
exit # if repository is not installed then exit
fi
else
echo
YELLOWTXT "MariaDB installation was skipped by user input. Proceeding to next step."
fi
echo
WHITETXT "============================================================================="
echo
_echo "${YELLOW}[?] Install Nginx ${NGINX_VERSION} ? [y/n][n]:${RESET} "
read nginx_install
if [ "${nginx_install}" == "y" ]; then
echo ""
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/mainline/${DISTRO_NAME,,} `lsb_release -cs` nginx" | tee /etc/apt/sources.list.d/nginx.list
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | tee /etc/apt/preferences.d/99nginx
if [ "$?" = 0 ]; then # if repository installed then install package
echo
GREENTXT "Nginx repository installed - OK"
echo
YELLOWTXT "Nginx ${NGINX_VERSION} installation:"
echo
apt update
apt -y install nginx nginx-module-perl nginx-module-image-filter nginx-module-geoip
if [ "$?" = 0 ]; then
echo
GREENTXT "Nginx ${NGINX_VERSION} installed - OK"
echo
systemctl enable nginx >/dev/null 2>&1
PACKAGES_INSTALLED nginx*
else
echo
REDTXT "Nginx ${NGINX_VERSION} installation error"
exit # if package is not installed then exit
fi
else
echo
REDTXT "Nginx repository installation error"
exit
fi
else
echo
YELLOWTXT "Nginx installation was skipped by user input. Proceeding to next step."
fi
echo
WHITETXT "============================================================================="
echo
_echo "${YELLOW}[?] Install PHP ? [y/n][n]:${RESET} "
read php_install
if [ "${php_install}" == "y" ]; then
echo ""
read -e -p "$(echo -e ${YELLOW}" [?] Enter required PHP version: "${RESET})" -i "8.2" PHP_VERSION
# Set php_version
${SQLITE3} "UPDATE system SET php_version = '${PHP_VERSION}';"
echo ""
if [ "${DISTRO_NAME}" == "Debian" ]; then
curl -o /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list
else
add-apt-repository ppa:ondrej/php -y
fi
if [ "$?" = 0 ]; then
echo ""
GREENTXT "PHP repository installed - OK"
echo ""
echo ""
YELLOWTXT "PHP ${PHP_VERSION} installation:"
echo
apt update
apt -y install php${PHP_VERSION} ${PHP_PACKAGES[@]/#/php${PHP_VERSION}-} php-pear
if [ "$?" = 0 ]; then
echo ""
GREENTXT "PHP ${PHP_VERSION} installed - OK"
echo ""
PACKAGES_INSTALLED php${PHP_VERSION}*
echo ""
# composer download
echo ""
YELLOWTXT "Composer ${COMPOSER_VERSION} installation:"
echo ""
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --${COMPOSER_VERSION} --install-dir=/usr/bin --filename=composer
php -r "unlink('composer-setup.php');"
else
echo ""
REDTXT "PHP installation error"
exit 1 # if package is not installed then exit
fi
else
echo
REDTXT "PHP repository installation error"
exit 1 # if repository is not installed then exit
fi
else
echo
YELLOWTXT "PHP installation was skipped by user input. Proceeding to next step."
fi
echo
echo
WHITETXT "============================================================================="
echo
_echo "${YELLOW}[?] Install Redis ${REDIS_VERSION} ? [y/n][n]:${RESET} "
read redis_install
if [ "${redis_install}" == "y" ]; then
curl -fL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/redis.list
if [ "$?" = 0 ]; then #
echo
GREENTXT "Redis repository installed - OK"
echo
YELLOWTXT "Redis installation:"
echo
apt update
apt -y install redis
echo ""
if [ "$?" = 0 ]; then
echo ""
GREENTXT "Redis installed - OK"
echo ""
PACKAGES_INSTALLED redis-server*
echo ""
echo ""
YELLOWTXT "Redis configuration per environment:"
echo ""
systemctl stop redis-server
systemctl disable redis-server
# Create Redis config
[Unit]
Description=Advanced key-value store at %i
After=network.target
[Service]
Type=notify
User=redis
Group=redis
# Security options
PrivateTmp=true
ProtectSystem=full
ProtectHome=true
ReadOnlyPaths=/
# Resource limits
LimitNOFILE=65535
# Directories to create and permissions
RuntimeDirectory=redis
RuntimeDirectoryMode=2755
UMask=007
# Directories and files that Redis can read and write
ReadWritePaths=-/var/lib/redis
ReadWritePaths=-/var/log/redis
ReadWritePaths=-/run/redis
# Command-line options
PIDFile=/run/redis/%i.pid
ExecStartPre=/usr/bin/test -f /etc/redis/%i.conf
ExecStart=/usr/bin/redis-server /etc/redis/%i.conf --daemonize yes --supervised systemd
# Timeouts
Restart=on-failure
TimeoutStartSec=5s
TimeoutStopSec=5s
[Install]
WantedBy=multi-user.target
END
mkdir -p /var/lib/redis
chmod 750 /var/lib/redis
chown redis /var/lib/redis
mkdir -p /etc/redis/
rm /etc/redis/redis.conf
PORT=6379
# Loop through the environments and services to create redis config
for ENV_SELECTED in "${ENV[@]}"
do
REDIS_PASSWORD="$(head -c 500 /dev/urandom | tr -dc 'a-zA-Z0-9@%&?' | fold -w 32 | head -n 1)"
${SQLITE3} "UPDATE magento SET redis_password = '${REDIS_PASSWORD}' WHERE env = '${ENV_SELECTED}';"
for SERVICE in session cache
do
OWNER=$(${SQLITE3} "SELECT owner FROM magento WHERE env = '${ENV_SELECTED}';")
if [ "${SERVICE}" = "session" ]; then
# Perfect options for sessions
CONFIG_OPTIONS="
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfsync everysec
"
else
# Default options for cache
CONFIG_OPTIONS="save \"\""
fi
cat > /etc/redis/${SERVICE}-${OWNER}.conf<<END
bind 127.0.0.1
port ${PORT}
daemonize yes
supervised auto
protected-mode yes
timeout 0
requirepass ${REDIS_PASSWORD}
dir /var/lib/redis
logfile /var/log/redis/${SERVICE}-${OWNER}.log
pidfile /run/redis/${SERVICE}-${OWNER}.pid
${CONFIG_OPTIONS}
maxmemory 1024mb
maxmemory-policy allkeys-lru
lazyfree-lazy-eviction yes
lazyfree-lazy-expire yes
lazyfree-lazy-server-del yes
lazyfree-lazy-user-del yes
rename-command SLAVEOF ""
rename-command CONFIG ""
rename-command PUBLISH ""
rename-command SAVE ""
rename-command SHUTDOWN ""
rename-command DEBUG ""
rename-command BGSAVE ""
rename-command BGREWRITEAOF ""
END
((PORT++))
chown redis /etc/redis/${SERVICE}-${OWNER}.conf
chmod 640 /etc/redis/${SERVICE}-${OWNER}.conf
echo "127.0.0.1 ${SERVICE}-${OWNER}" >> /etc/hosts
systemctl daemon-reload
systemctl enable redis@${SERVICE}-${OWNER}
systemctl restart redis@${SERVICE}-${OWNER}
done
done
else
echo
REDTXT "Redis installation error"
exit 1 # if package is not installed then exit
fi
else
echo
REDTXT "Redis repository installation error"
exit 1
fi
else
echo
YELLOWTXT "Redis installation was skipped by user input. Proceeding to next step."
fi
echo
WHITETXT "============================================================================="
echo
echo
_echo "${YELLOW}[?] Install RabbitMQ ${RABBITMQ_VERSION} ? [y/n][n]:${RESET} "
read rabbitmq_install
if [ "${rabbitmq_install}" == "y" ];then
curl -1sLf 'https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-server/setup.deb.sh' | bash
curl -1sLf 'https://dl.cloudsmith.io/public/rabbitmq/rabbitmq-erlang/setup.deb.sh' | bash
if [ "$?" = 0 ]; then
echo
GREENTXT "RabbitMQ repository installed - OK"
echo
YELLOWTXT "RabbitMQ ${RABBITMQ_VERSION} installation:"
echo ""
apt update
apt -y install rabbitmq-server=${RABBITMQ_VERSION}
if [ "$?" = 0 ]; then
echo ""
GREENTXT "RabbitMQ ${RABBITMQ_VERSION} installed - OK"
echo ""
PACKAGES_INSTALLED rabbitmq* erlang*
echo "127.0.0.1 rabbitmq" >> /etc/hosts
echo ""
echo ""
YELLOWTXT "RabbitMQ ${RABBITMQ_VERSION} configuration per environment:"
echo ""
systemctl stop rabbitmq-server
systemctl stop epmd*
epmd -kill
cat > /etc/rabbitmq/rabbitmq-env.conf <<END
NODENAME=rabbit@localhost
NODE_IP_ADDRESS=127.0.0.1
ERL_EPMD_ADDRESS=127.0.0.1
PID_FILE=/var/lib/rabbitmq/mnesia/rabbitmq_pid
END
echo '[{kernel, [{inet_dist_use_interface, {127,0,0,1}}]},{rabbit, [{tcp_listeners, [{"127.0.0.1", 5672}]}]}].' > /etc/rabbitmq/rabbitmq.config
cat >> /etc/sysctl.conf <<END
net.ipv6.conf.lo.disable_ipv6 = 0
END
sysctl -q -p
cat > /etc/systemd/system/epmd.service <<END
[Unit]
Description=Erlang Port Mapper Daemon
After=network.target
Requires=epmd.socket