-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript_gui.sh
More file actions
1227 lines (1021 loc) · 44.6 KB
/
script_gui.sh
File metadata and controls
1227 lines (1021 loc) · 44.6 KB
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
set -euo pipefail
# Auto-install dialog if missing (silent)
if ! command -v dialog &>/dev/null; then
if command -v apt-get &>/dev/null; then
apt-get update -qq >/dev/null 2>&1 && apt-get install -y dialog >/dev/null 2>&1
else
echo "Error: This script requires apt-get (Debian/Ubuntu system)"
exit 1
fi
if ! command -v dialog &>/dev/null; then
echo "Failed to install dialog. Exiting."
exit 1
fi
fi
readonly SCRIPT_VERSION="3.0"
LOG_FILE="/tmp/dezerx-install.log"
OPERATION_MODE=""
LICENSE_KEY=""
DOMAIN=""
INSTALL_DIR=""
DB_PASSWORD=""
DB_NAME_PREFIX=""
DB_FULL_NAME=""
DB_USER_FULL=""
RESTORE_ON_FAILURE=""
PROTOCOL="https"
BACKUP_DIR=""
DB_BACKUP_FILE=""
print_banner() {
dialog --title "DezerX Installer" --msgbox "██████╗ ███████╗███████╗███████╗██████╗ ██╗ ██╗\n██╔══██╗██╔════╝╚══███╔╝██╔════╝██╔══██╗╚██╗██╔╝\n██║ ██║█████╗ ███╔╝ █████╗ ██████╔╝ ╚███╔╝ \n██║ ██║██╔══╝ ███╔╝ ██╔══╝ ██╔══██╗ ██╔██╗ \n██████╔╝███████╗███████╗███████╗██║ ██║██╔╝ ██╗\n╚═════╝ ╚══════╝╚══════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝\n\nINSTALLATION & UPDATE SCRIPT v${SCRIPT_VERSION}\nRequires Root Access\n\nThis script can install or update DezerX.\nEstimated time: 3-6 minutes (install) / 3-5 minutes (update)\nOperation log: $LOG_FILE" 20 70
}
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >>"$LOG_FILE"
}
show_loading() {
local pid=$1
local message=$2
local i=0
while kill -0 "$pid" 2>/dev/null; do
case $((i % 4)) in
0) echo -n " [|] $message" ;;
1) echo -n " [/] $message" ;;
2) echo -n " [-] $message" ;;
3) echo -n " [\\] $message" ;;
esac
sleep 0.5
echo -ne "\r"
i=$((i + 1))
done
echo " [✓] $message - Complete"
}
execute_with_loading() {
local command="$1"
local message="$2"
log_message "Executing: $command"
eval "$command" >>"$LOG_FILE" 2>&1 &
local pid=$!
show_loading $pid "$message"
wait $pid
local exit_code=$?
if [ $exit_code -ne 0 ]; then
dialog --title "Error" --msgbox "Command failed: $command\nCheck log file: $LOG_FILE" 12 70
exit $exit_code
fi
return $exit_code
}
check_required_commands() {
local cmds=(curl awk grep sed dialog)
for cmd in "${cmds[@]}"; do
if ! command -v "$cmd" &>/dev/null; then
if [[ "$cmd" == "dialog" ]]; then
echo "Installing dialog..."
apt-get update && apt-get install -y dialog
else
dialog --title "Error" --msgbox "Required command '$cmd' not found. Please install it." 10 60
exit 1
fi
fi
done
}
check_root() {
if [[ $EUID -ne 0 ]]; then
dialog --title "Error" --msgbox "This script must be run as root!\n\nPlease run: sudo $0" 10 60
exit 1
fi
}
choose_operation_mode() {
local choice
choice=$(dialog --title "DezerX Installer" --menu "What would you like to do?" 18 70 10 \
"1" "🆕 Fresh Installation - Install DezerX from scratch" \
"2" "🔄 Update Existing - Update an existing DezerX installation" \
"3" "⚠️ Delete Installation - Remove DezerX and all its data ⚠️" 3>&1 1>&2 2>&3)
case $choice in
1)
OPERATION_MODE="install"
if dialog --title "Backup Option" --yesno "Would you like to automatically restore the previous backup if an error occurs?\n\nThe non-automatic restore feature is intended for developers and testing environments only." 12 70; then
RESTORE_ON_FAILURE="yes"
else
RESTORE_ON_FAILURE="no"
fi
;;
2)
OPERATION_MODE="update"
if dialog --title "Backup Option" --yesno "Would you like to automatically restore the previous backup if an error occurs?\n\nThe non-automatic restore feature is intended for developers and testing environments only." 12 70; then
RESTORE_ON_FAILURE="yes"
else
RESTORE_ON_FAILURE="no"
fi
;;
3)
OPERATION_MODE="delete"
handle_deletion
;;
*)
dialog --title "Error" --msgbox "Invalid choice. Exiting." 10 60
exit 1
;;
esac
}
handle_deletion() {
if ! dialog --title "Delete Confirmation" --yesno "This will remove DezerX and all its data permanently!\n\nAre you absolutely sure you want to continue?" 12 70; then
dialog --title "Cancelled" --msgbox "Deletion cancelled by user." 10 60
clear
exit 0
fi
INSTALL_DIR=$(dialog --title "Installation Directory" --inputbox "Enter the DezerX installation directory to delete:" 10 60 "/var/www/DezerX" 3>&1 1>&2 2>&3)
if [[ -z "$INSTALL_DIR" ]]; then
INSTALL_DIR="/var/www/DezerX"
fi
if [[ ! -d "$INSTALL_DIR" ]]; then
dialog --title "Error" --msgbox "Directory $INSTALL_DIR does not exist. Aborting deletion." 10 60
clear
exit 1
fi
local confirmation_text=$(dialog --title "Final Confirmation" --inputbox "Type 'DELETE EVERYTHING' to confirm deletion of $INSTALL_DIR:" 10 70 "" 3>&1 1>&2 2>&3)
if [[ "$confirmation_text" != "DELETE EVERYTHING" ]]; then
dialog --title "Cancelled" --msgbox "Deletion cancelled - confirmation text did not match." 10 60
clear
exit 0
fi
perform_deletion
}
perform_deletion() {
dialog --title "Deleting" --infobox "Removing DezerX installation..." 10 60
# Load DB info from .env if available
if [[ -f "$INSTALL_DIR/.env" ]]; then
DB_FULL_NAME=$(grep '^DB_DATABASE=' "$INSTALL_DIR/.env" | cut -d '=' -f2- | tr -d '"' || echo "")
DB_USER_FULL=$(grep '^DB_USERNAME=' "$INSTALL_DIR/.env" | cut -d '=' -f2- | tr -d '"' || echo "")
fi
# Remove Nginx config
rm -f /etc/nginx/sites-enabled/dezerx.conf 2>>"$LOG_FILE" || true
rm -f /etc/nginx/sites-available/dezerx.conf 2>>"$LOG_FILE" || true
systemctl reload nginx 2>>"$LOG_FILE" || true
# Remove installation directory
if [[ -d "$INSTALL_DIR" ]]; then
rm -rf "$INSTALL_DIR" 2>>"$LOG_FILE" || true
fi
# Remove database and user if possible
if command -v mariadb &>/dev/null && [[ -n "$DB_FULL_NAME" ]]; then
mariadb -e "DROP DATABASE IF EXISTS \`$DB_FULL_NAME\`;" 2>>"$LOG_FILE" || true
fi
if command -v mariadb &>/dev/null && [[ -n "$DB_USER_FULL" ]]; then
mariadb -e "DROP USER IF EXISTS '$DB_USER_FULL'@'127.0.0.1';" 2>>"$LOG_FILE" || true
fi
# Stop and disable queue worker
systemctl stop dezerx.service 2>>"$LOG_FILE" || true
systemctl disable dezerx.service 2>>"$LOG_FILE" || true
rm -f /etc/systemd/system/dezerx.service 2>>"$LOG_FILE" || true
dialog --title "Complete" --msgbox "DezerX and all related data have been deleted successfully." 10 60
exit 0
}
check_system_requirements() {
dialog --title "System Check" --infobox "Checking system requirements..." 10 60
if ! command -v curl &>/dev/null; then
execute_with_loading "apt-get update && apt-get install -y curl" "Installing curl"
fi
if ! command -v unzip &>/dev/null; then
execute_with_loading "apt-get install -y unzip" "Installing unzip"
fi
if ! command -v lsb_release &>/dev/null; then
execute_with_loading "apt-get install -y lsb-release" "Installing lsb-release"
fi
local os_name=$(lsb_release -si)
local os_version=$(lsb_release -sr)
if [[ "$os_name" != "Ubuntu" ]] && [[ "$os_name" != "Debian" ]]; then
dialog --title "Error" --msgbox "This script only supports Ubuntu and Debian\nDetected: $os_name $os_version" 10 60
exit 1
fi
local available_space=$(df / | awk 'NR==2 {print $4}')
local required_space=5242880
if [[ "$OPERATION_MODE" == "update" ]]; then
required_space=2097152
fi
if [[ $available_space -lt $required_space ]]; then
dialog --title "Error" --msgbox "Insufficient disk space.\nRequired: $((required_space / 1024 / 1024))GB\nAvailable: $((available_space / 1024 / 1024))GB" 10 60
exit 1
fi
local total_mem=$(free -m | awk 'NR==2{print $2}')
if [[ $total_mem -lt 1024 ]]; then
dialog --title "Warning" --msgbox "Low memory detected: ${total_mem}MB\nRecommended: 2GB+\n\nContinuing anyway..." 10 60
fi
# Check dependency versions
local version_warnings=""
if command -v php &>/dev/null; then
local php_version=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;')
if [[ $(echo "$php_version < 8.1" | bc 2>/dev/null || echo 1) -eq 1 ]]; then
version_warnings+="PHP $php_version (recommended: 8.1+)\n"
fi
fi
if command -v mariadb &>/dev/null; then
local mariadb_version=$(mariadb --version | grep -oP 'Ver \K[0-9]+\.[0-9]+' | head -1)
if [[ -n "$mariadb_version" && $(echo "$mariadb_version < 10.5" | bc 2>/dev/null || echo 1) -eq 1 ]]; then
version_warnings+="MariaDB $mariadb_version (recommended: 10.5+)\n"
fi
fi
if [[ -n "$version_warnings" ]]; then
dialog --title "Version Warnings" --msgbox "Some dependencies may be outdated:\n\n$version_warnings\nInstallation will continue..." 12 70
fi
dialog --title "System Check" --msgbox "System requirements check passed!\n\nOS: $os_name $os_version\nMemory: ${total_mem}MB\nDisk Space: $((available_space / 1024 / 1024))GB available" 12 60
}
validate_domain() {
local domain=$1
if [[ $domain =~ ^https?:// ]]; then
return 2
fi
if [[ ! $domain =~ ^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*$ ]]; then
return 1
fi
return 0
}
get_install_input() {
while true; do
LICENSE_KEY=$(dialog --title "License Key" --inputbox "Enter your DezerX license key:" 10 60 "" 3>&1 1>&2 2>&3)
if [[ -n "$LICENSE_KEY" && ${#LICENSE_KEY} -ge 10 ]]; then
break
else
dialog --title "Error" --msgbox "License key must be at least 10 characters. Please try again." 10 60
fi
done
while true; do
DOMAIN=$(dialog --title "Domain" --inputbox "Enter your domain or subdomain:\n(e.g., example.com or app.example.com)\n\nDo NOT include http:// or https://" 12 70 "" 3>&1 1>&2 2>&3)
if [[ -z "$DOMAIN" ]]; then
dialog --title "Error" --msgbox "Domain cannot be empty." 10 60
continue
fi
validate_domain "$DOMAIN"
local validation_result=$?
if [[ $validation_result -eq 2 ]]; then
dialog --title "Error" --msgbox "Please enter the domain WITHOUT http:// or https://\n\nExample: Use 'example.com' instead of 'https://example.com'" 10 70
continue
elif [[ $validation_result -eq 1 ]]; then
dialog --title "Error" --msgbox "Invalid domain format. Please try again." 10 60
continue
else
break
fi
done
PROTOCOL=$(dialog --title "Protocol" --radiolist "Choose protocol:" 12 60 2 \
"https" "HTTPS - Secure (recommended)" on \
"http" "HTTP - Insecure" off 3>&1 1>&2 2>&3)
INSTALL_DIR=$(dialog --title "Install Directory" --inputbox "Enter installation directory:" 10 60 "/var/www/DezerX" 3>&1 1>&2 2>&3)
if [[ -z "$INSTALL_DIR" ]]; then
INSTALL_DIR="/var/www/DezerX"
fi
DB_NAME_PREFIX=$(echo "$DOMAIN" | grep -o '^[a-zA-Z]*' | tr '[:upper:]' '[:lower:]' | cut -c1-4)
if [[ -z "$DB_NAME_PREFIX" ]]; then
DB_NAME_PREFIX="dzrx"
fi
DB_FULL_NAME=$(dialog --title "Database Name" --inputbox "Database name:" 10 60 "${DB_NAME_PREFIX}_dezerx" 3>&1 1>&2 2>&3)
if [[ -z "$DB_FULL_NAME" ]]; then
DB_FULL_NAME="${DB_NAME_PREFIX}_dezerx"
fi
DB_USER_FULL=$(dialog --title "Database User" --inputbox "Database user:" 10 60 "${DB_NAME_PREFIX}_dezer" 3>&1 1>&2 2>&3)
if [[ -z "$DB_USER_FULL" ]]; then
DB_USER_FULL="${DB_NAME_PREFIX}_dezer"
fi
DB_PASSWORD=$(dialog --title "Database Password" --passwordbox "Database password (leave blank to auto-generate):" 10 60 "" 3>&1 1>&2 2>&3)
if [[ -z "$DB_PASSWORD" ]]; then
DB_PASSWORD=$(openssl rand -base64 32 | tr -d "=+/" | cut -c1-25)
fi
dialog --title "Installation Summary" --yesno "Please confirm your installation settings:\n\nLicense Key: ${LICENSE_KEY:0:8}***\nDomain: $DOMAIN\nProtocol: $PROTOCOL\nFull URL: ${PROTOCOL}://$DOMAIN\nInstall Directory: $INSTALL_DIR\nDatabase: $DB_FULL_NAME\nDB User: $DB_USER_FULL\n\nProceed with installation?" 18 70
if [ $? -ne 0 ]; then
dialog --title "Cancelled" --msgbox "Installation cancelled by user." 10 60
exit 0
fi
}
get_update_input() {
while true; do
LICENSE_KEY=$(dialog --title "License Key" --inputbox "Enter your DezerX license key:" 10 60 "" 3>&1 1>&2 2>&3)
if [[ -n "$LICENSE_KEY" && ${#LICENSE_KEY} -ge 10 ]]; then
break
else
dialog --title "Error" --msgbox "License key must be at least 10 characters. Please try again." 10 60
fi
done
while true; do
DOMAIN=$(dialog --title "Domain" --inputbox "Enter your domain:\n(e.g., example.com or app.example.com)\n\nDo NOT include http:// or https://" 12 70 "" 3>&1 1>&2 2>&3)
if [[ -z "$DOMAIN" ]]; then
dialog --title "Error" --msgbox "Domain cannot be empty." 10 60
continue
fi
validate_domain "$DOMAIN"
local validation_result=$?
if [[ $validation_result -eq 2 ]]; then
dialog --title "Error" --msgbox "Please enter the domain WITHOUT http:// or https://\n\nExample: Use 'example.com' instead of 'https://example.com'" 10 70
continue
elif [[ $validation_result -eq 1 ]]; then
dialog --title "Error" --msgbox "Invalid domain format. Please try again." 10 60
continue
else
break
fi
done
PROTOCOL=$(dialog --title "Protocol" --radiolist "Choose protocol:" 12 60 2 \
"https" "HTTPS - Secure (recommended)" on \
"http" "HTTP - Insecure" off 3>&1 1>&2 2>&3)
while true; do
INSTALL_DIR=$(dialog --title "Installation Directory" --inputbox "Enter your existing DezerX directory:" 10 60 "/var/www/DezerX" 3>&1 1>&2 2>&3)
if [[ -z "$INSTALL_DIR" ]]; then
INSTALL_DIR="/var/www/DezerX"
fi
if [[ ! -d "$INSTALL_DIR" ]]; then
dialog --title "Error" --msgbox "Directory $INSTALL_DIR does not exist. Please check the path." 10 60
continue
fi
if [[ ! -f "$INSTALL_DIR/.env" ]]; then
dialog --title "Error" --msgbox "No .env file found in $INSTALL_DIR.\nThis doesn't appear to be a DezerX installation." 10 70
continue
fi
if [[ ! -f "$INSTALL_DIR/artisan" ]]; then
dialog --title "Error" --msgbox "No artisan file found in $INSTALL_DIR.\nThis doesn't appear to be a Laravel/DezerX installation." 10 70
continue
fi
break
done
dialog --title "Update Summary" --yesno "Please confirm your update settings:\n\nLicense Key: ${LICENSE_KEY:0:8}***\nDomain: $DOMAIN\nProtocol: $PROTOCOL\nExisting Directory: $INSTALL_DIR\n\nProceed with update?" 15 70
if [ $? -ne 0 ]; then
dialog --title "Cancelled" --msgbox "Update cancelled by user." 10 60
exit 0
fi
}
verify_license() {
dialog --title "License Verification" --infobox "Contacting DezerX license server..." 10 60
local temp_file=$(mktemp)
local http_code
http_code=$(curl -s -w "%{http_code}" -X POST \
-H "Authorization: Bearer $LICENSE_KEY" \
-H "domain: $DOMAIN" \
-H "product: 1" \
-H "Content-Type: application/json" \
-o "$temp_file" \
--connect-timeout 30 \
--max-time 60 \
https://market.dezerx.com/api/v1/verify)
if [[ "$http_code" == "200" ]]; then
dialog --title "Success" --msgbox "License verified successfully!" 10 60
rm -f "$temp_file"
return 0
else
local error_msg="Unknown error"
if [[ -f "$temp_file" ]]; then
error_msg=$(cat "$temp_file" 2>/dev/null || echo "Unknown error")
fi
dialog --title "License Error" --msgbox "License verification failed (HTTP: $http_code)\n\nServer response: $error_msg\n\nPlease check your license key and domain." 15 70
rm -f "$temp_file"
exit 1
fi
}
create_backup() {
dialog --title "Creating Backup" --infobox "Creating backup of existing installation..." 10 60
BACKUP_DIR="/tmp/dezerx-backup-$(date +%Y%m%d-%H%M%S)"
execute_with_loading "cp -r $INSTALL_DIR $BACKUP_DIR" "Creating backup"
if [[ ! -f "$BACKUP_DIR/.env" ]]; then
dialog --title "Error" --msgbox "Backup verification failed - .env file not found in backup" 10 60
exit 1
fi
dialog --title "Backup Complete" --msgbox "Backup created successfully!\n\nLocation: $BACKUP_DIR" 10 70
}
backup_database() {
dialog --title "Database Backup" --infobox "Backing up database..." 10 60
local env_file="$INSTALL_DIR/.env"
if [[ ! -f "$env_file" ]]; then
dialog --title "Warning" --msgbox ".env file not found. Skipping database backup." 10 60
return 0
fi
local db_connection=$(grep '^DB_CONNECTION=' "$env_file" | cut -d '=' -f2- | tr -d '"' || echo "")
local db_host=$(grep '^DB_HOST=' "$env_file" | cut -d '=' -f2- | tr -d '"' || echo "")
local db_database=$(grep '^DB_DATABASE=' "$env_file" | cut -d '=' -f2- | tr -d '"' || echo "")
local db_username=$(grep '^DB_USERNAME=' "$env_file" | cut -d '=' -f2- | tr -d '"' || echo "")
local db_password=$(grep '^DB_PASSWORD=' "$env_file" | cut -d '=' -f2- | tr -d '"' || echo "")
if [[ "$db_connection" != "mysql" ]]; then
dialog --title "Warning" --msgbox "Database connection is not 'mysql'. Skipping database backup." 10 60
return 0
fi
if [[ -z "$db_host" || -z "$db_database" || -z "$db_username" ]]; then
dialog --title "Error" --msgbox "Missing database credentials in .env file. Cannot perform database backup." 10 60
exit 1
fi
BACKUP_DIR="/tmp/dezerx-backup-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$BACKUP_DIR"
DB_BACKUP_FILE="$BACKUP_DIR/database_$(date +%Y%m%d-%H%M%S).sql.gz"
export MYSQL_PWD="$db_password"
local mysqldump_cmd="mysqldump -h $db_host -u $db_username $db_database | gzip > \"$DB_BACKUP_FILE\""
if ! command -v mysqldump &>/dev/null; then
dialog --title "Error" --msgbox "mysqldump command not found. Cannot perform database backup." 10 60
unset MYSQL_PWD
exit 1
fi
execute_with_loading "$mysqldump_cmd" "Creating database backup"
unset MYSQL_PWD
if [[ ! -s "$DB_BACKUP_FILE" ]]; then
dialog --title "Error" --msgbox "Database backup file is empty or not created!" 10 60
return 1
fi
dialog --title "Database Backup" --msgbox "Database backup completed successfully!\n\nLocation: $DB_BACKUP_FILE" 12 70
}
restore_backup() {
dialog --title "Restoring Backup" --infobox "Restoring from backup due to update failure..." 10 60
if [[ -n "$BACKUP_DIR" && -d "$BACKUP_DIR" ]]; then
rm -rf "$INSTALL_DIR"
mv "$BACKUP_DIR" "$INSTALL_DIR"
chown -R www-data:www-data "$INSTALL_DIR" 2>/dev/null || true
chmod -R 755 "$INSTALL_DIR" 2>/dev/null || true
chmod -R 775 "$INSTALL_DIR/storage" 2>/dev/null || true
chmod -R 775 "$INSTALL_DIR/bootstrap/cache" 2>/dev/null || true
dialog --title "Backup Restored" --msgbox "Backup restored successfully!\nYour original installation has been restored." 10 60
else
dialog --title "Error" --msgbox "No backup found to restore from!" 10 60
fi
}
install_dependencies() {
dialog --title "Installing Dependencies" --infobox "Installing system dependencies..." 10 60
execute_with_loading "apt-get update" "Updating package lists"
execute_with_loading "DEBIAN_FRONTEND=noninteractive apt-get upgrade -y" "Upgrading system packages"
execute_with_loading "apt-get install -y software-properties-common curl apt-transport-https ca-certificates gnupg lsb-release wget unzip git cron" "Installing basic dependencies"
# Add repositories
if ! LC_ALL=C.UTF-8 add-apt-repository -y ppa:ondrej/php >>"$LOG_FILE" 2>&1; then
dialog --title "Warning" --msgbox "Failed to add PHP PPA, trying alternative method..." 10 60
fi
curl -fsSL https://packages.redis.io/gpg | gpg --dearmor >/usr/share/keyrings/redis-archive-keyring.gpg 2>/dev/null
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" >/etc/apt/sources.list.d/redis.list
curl -LsS https://r.mariadb.com/downloads/mariadb_repo_setup | bash >>"$LOG_FILE" 2>&1
execute_with_loading "apt-get update" "Updating package lists with new repositories"
local packages="nginx php8.3 php8.3-common php8.3-cli php8.3-gd php8.3-mysql php8.3-mbstring php8.3-bcmath php8.3-xml php8.3-fpm php8.3-curl php8.3-zip mariadb-server tar unzip git redis-server ufw"
execute_with_loading "DEBIAN_FRONTEND=noninteractive apt-get install -y $packages" "Installing main packages"
execute_with_loading "systemctl start nginx && systemctl enable nginx" "Starting Nginx"
execute_with_loading "systemctl start php8.3-fpm && systemctl enable php8.3-fpm" "Starting PHP-FPM"
execute_with_loading "systemctl start redis-server && systemctl enable redis-server" "Starting Redis"
execute_with_loading "systemctl start cron && systemctl enable cron" "Starting Cron"
execute_with_loading "systemctl stop ufw && systemctl disable ufw" "Stopping UFW for configuration"
execute_with_loading "mkdir -p /var/www" "Creating web directory"
dialog --title "Dependencies" --msgbox "System dependencies installed successfully!" 10 60
}
install_composer() {
dialog --title "Installing Composer" --infobox "Installing Composer..." 10 60
local composer_installer="/tmp/composer-installer.php"
execute_with_loading "curl -sS https://getcomposer.org/installer -o $composer_installer" "Downloading Composer"
execute_with_loading "php $composer_installer --install-dir=/usr/local/bin --filename=composer" "Installing Composer"
rm -f "$composer_installer"
if ! command -v composer &>/dev/null; then
dialog --title "Error" --msgbox "Composer installation failed" 10 60
exit 1
fi
dialog --title "Composer" --msgbox "Composer installed successfully!" 10 60
}
setup_database() {
dialog --title "Setting up Database" --infobox "Configuring MariaDB..." 10 60
execute_with_loading "systemctl start mariadb && systemctl enable mariadb" "Starting MariaDB"
# Secure MariaDB
local sql_file=$(mktemp)
cat >"$sql_file" <<'EOF'
DROP USER IF EXISTS ''@'%';
DROP USER IF EXISTS ''@'localhost';
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test_%';
FLUSH PRIVILEGES;
EOF
if ! mariadb <"$sql_file" >>"$LOG_FILE" 2>&1; then
dialog --title "Error" --msgbox "Failed to secure MariaDB installation" 10 60
rm -f "$sql_file"
exit 1
fi
# Create database and user
cat >"$sql_file" <<EOF
CREATE DATABASE IF NOT EXISTS \`$DB_FULL_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS '$DB_USER_FULL'@'127.0.0.1' IDENTIFIED BY '$DB_PASSWORD';
GRANT ALL PRIVILEGES ON \`$DB_FULL_NAME\`.* TO '$DB_USER_FULL'@'127.0.0.1' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
if ! mariadb <"$sql_file" >>"$LOG_FILE" 2>&1; then
dialog --title "Error" --msgbox "Failed to create database and user" 10 60
rm -f "$sql_file"
exit 1
fi
rm -f "$sql_file"
dialog --title "Database" --msgbox "Database setup completed successfully!\n\nDatabase: $DB_FULL_NAME\nUser: $DB_USER_FULL" 12 70
}
download_dezerx() {
dialog --title "Downloading DezerX" --infobox "Requesting download URL from DezerX servers..." 10 60
local temp_file=$(mktemp)
local http_code
http_code=$(curl -s -w "%{http_code}" -X POST \
-H "Authorization: Bearer $LICENSE_KEY" \
-H "domain: $DOMAIN" \
-H "product: 1" \
-H "Content-Type: application/json" \
-o "$temp_file" \
--connect-timeout 30 \
--max-time 60 \
https://market.dezerx.com/api/v1/download)
if [[ "$http_code" != "200" ]]; then
local error_msg="Unknown error"
if [[ -f "$temp_file" ]]; then
error_msg=$(cat "$temp_file" 2>/dev/null || echo "Unknown error")
fi
dialog --title "Download Error" --msgbox "Failed to get download URL (HTTP: $http_code)\n\nServer response: $error_msg" 12 70
rm -f "$temp_file"
if [[ "$OPERATION_MODE" == "update" ]]; then
restore_backup
fi
exit 1
fi
local download_url
if command -v jq &>/dev/null; then
download_url=$(jq -r '.download_url' "$temp_file" 2>/dev/null)
else
download_url=$(grep -o '"download_url":"[^"]*' "$temp_file" | cut -d'"' -f4 | sed 's/\\//g')
fi
rm -f "$temp_file"
if [[ -z "$download_url" || "$download_url" == "null" ]]; then
dialog --title "Error" --msgbox "Failed to extract download URL from server response" 10 60
if [[ "$OPERATION_MODE" == "update" ]]; then
restore_backup
fi
exit 1
fi
if [[ "$OPERATION_MODE" == "install" ]]; then
mkdir -p "$INSTALL_DIR"
fi
local download_file="/tmp/dezerx-$(date +%s).zip"
dialog --title "Downloading" --infobox "Downloading DezerX package..." 10 60
if ! curl -L -o "$download_file" --progress-bar --connect-timeout 30 --max-time 300 "$download_url"; then
dialog --title "Error" --msgbox "Download failed" 10 60
rm -f "$download_file"
if [[ "$OPERATION_MODE" == "update" ]]; then
restore_backup
fi
exit 1
fi
# Extract and install
local temp_extract_dir=$(mktemp -d)
if ! unzip -q "$download_file" -d "$temp_extract_dir"; then
dialog --title "Error" --msgbox "Failed to extract files" 10 60
rm -f "$download_file"
rm -rf "$temp_extract_dir"
if [[ "$OPERATION_MODE" == "update" ]]; then
restore_backup
fi
exit 1
fi
rm -f "$download_file"
# Find DezerX directory
local dezerx_source_dir=""
local found_dirs=()
while IFS= read -r -d '' dir; do
found_dirs+=("$dir")
done < <(find "$temp_extract_dir" -maxdepth 1 -type d -name "*DezerX*" -print0)
if [[ ${#found_dirs[@]} -eq 0 ]]; then
dialog --title "Error" --msgbox "No DezerX directory found in the extracted archive" 10 60
rm -rf "$temp_extract_dir"
if [[ "$OPERATION_MODE" == "update" ]]; then
restore_backup
fi
exit 1
fi
dezerx_source_dir="${found_dirs[0]}"
if [[ ! -f "$dezerx_source_dir/.env.example" ]]; then
dialog --title "Error" --msgbox "Invalid DezerX package - .env.example not found" 10 60
rm -rf "$temp_extract_dir"
if [[ "$OPERATION_MODE" == "update" ]]; then
restore_backup
fi
exit 1
fi
# Move files
if [[ "$OPERATION_MODE" == "install" ]]; then
rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
if ! mv "$dezerx_source_dir"/* "$INSTALL_DIR"/; then
dialog --title "Error" --msgbox "Failed to move DezerX files to installation directory" 10 60
rm -rf "$temp_extract_dir"
exit 1
fi
if ls "$dezerx_source_dir"/.[^.]* >/dev/null 2>&1; then
for file in "$dezerx_source_dir"/.[^.]*; do
mv "$file" "$INSTALL_DIR"/ 2>/dev/null || true
done
fi
else
if ! rsync -a --exclude='.env.example' --exclude='storage' "$dezerx_source_dir"/ "$INSTALL_DIR"/; then
dialog --title "Error" --msgbox "Failed to copy updated files to installation directory" 10 60
rm -rf "$temp_extract_dir"
restore_backup
exit 1
fi
fi
rm -rf "$temp_extract_dir"
dialog --title "Download Complete" --msgbox "DezerX files downloaded and extracted successfully!" 10 60
}
update_env_file() {
local key="$1"
local value="$2"
local env_file="$3"
if grep -q "^${key}=" "$env_file"; then
if command -v perl >/dev/null 2>&1; then
perl -i -pe "s|^${key}=.*|${key}=${value}|" "$env_file"
else
local temp_file=$(mktemp)
grep -v "^${key}=" "$env_file" >"$temp_file"
echo "${key}=${value}" >>"$temp_file"
mv "$temp_file" "$env_file"
fi
else
echo "${key}=${value}" >>"$env_file"
fi
}
sync_env_files() {
local install_dir="$1"
local env_example_file="$install_dir/.env.example"
local env_file="$install_dir/.env"
if [[ ! -f "$env_example_file" ]] || [[ ! -f "$env_file" ]]; then
return 0
fi
local env_example_lines
mapfile -t env_example_lines <"$env_example_file"
for line in "${env_example_lines[@]}"; do
if [[ "$line" =~ ^[[:alnum:]_]+= ]]; then
local key=$(echo "$line" | cut -d= -f1)
if ! grep -q "^${key}=" "$env_file"; then
printf "%s\n" "$line" >>"$env_file"
fi
fi
done
chown www-data:www-data "$env_file" 2>/dev/null || true
chmod 644 "$env_file" 2>/dev/null || true
}
configure_laravel() {
dialog --title "Configuring Laravel" --infobox "Configuring Laravel application..." 10 60
cd "$INSTALL_DIR"
if [[ "$OPERATION_MODE" == "install" ]]; then
if [[ ! -f ".env.example" ]]; then
dialog --title "Error" --msgbox ".env.example file not found in $INSTALL_DIR" 10 60
exit 1
fi
execute_with_loading "cp .env.example .env" "Creating environment file"
else
if [[ ! -f ".env" ]]; then
dialog --title "Error" --msgbox ".env file not found in $INSTALL_DIR" 10 60
restore_backup
exit 1
fi
sync_env_files "$INSTALL_DIR"
fi
# Install composer dependencies
dialog --title "Composer" --infobox "Installing Composer dependencies..." 10 60
echo "yes" | composer install --no-dev --optimize-autoloader >>"$LOG_FILE" 2>&1 &
local composer_pid=$!
show_loading $composer_pid "Installing Composer dependencies"
wait $composer_pid
if [ $? -ne 0 ]; then
dialog --title "Error" --msgbox "Composer installation failed\nCheck log file: $LOG_FILE" 10 60
if [[ "$OPERATION_MODE" == "update" ]]; then
restore_backup
fi
exit 1
fi
execute_with_loading "php artisan storage:link" "Linking storage"
if [[ "$OPERATION_MODE" == "install" ]]; then
execute_with_loading "php artisan key:generate --force" "Generating app key"
update_env_file "DB_CONNECTION" "mysql" ".env"
update_env_file "DB_HOST" "127.0.0.1" ".env"
update_env_file "DB_PORT" "3306" ".env"
update_env_file "DB_DATABASE" "$DB_FULL_NAME" ".env"
update_env_file "DB_USERNAME" "$DB_USER_FULL" ".env"
update_env_file "DB_PASSWORD" "$DB_PASSWORD" ".env"
update_env_file "APP_URL" "${PROTOCOL}://$DOMAIN" ".env"
update_env_file "KEY" "$LICENSE_KEY" ".env"
else
update_env_file "KEY" "$LICENSE_KEY" ".env"
fi
dialog --title "Laravel" --msgbox "Laravel configuration completed successfully!" 10 60
}
check_dns() {
local server_ip=$(curl -s --connect-timeout 10 ifconfig.me || curl -s --connect-timeout 10 ipinfo.io/ip || echo "Unable to detect")
if ! dialog --title "DNS Configuration" --yesno "Server IP Address: $server_ip\nDomain: $DOMAIN\n\nHave you pointed $DOMAIN to this server's IP address?\n\nSelect 'No' if you need instructions." 12 70; then
dialog --title "DNS Instructions" --msgbox "Please configure your DNS settings:\n\n1. Log into your domain registrar or DNS provider\n2. Create an A record pointing $DOMAIN to $server_ip\n3. Wait for DNS propagation (usually 5-30 minutes)\n\nPress OK when DNS is configured..." 15 70
fi
}
prompt_ufw_firewall() {
if ! command -v ufw &>/dev/null; then
dialog --title "Firewall" --msgbox "ufw (Uncomplicated Firewall) is not installed.\nSkipping firewall configuration." 10 60
return
fi
if dialog --title "Firewall Configuration" --yesno "Would you like to automatically configure the firewall (ufw) to allow HTTP/HTTPS traffic?" 10 60; then
execute_with_loading "systemctl start ufw && systemctl enable ufw" "Starting UFW"
ufw allow 80/tcp >>"$LOG_FILE" 2>&1
ufw allow 443/tcp >>"$LOG_FILE" 2>&1
ufw reload >>"$LOG_FILE" 2>&1
dialog --title "Firewall" --msgbox "UFW configured to allow HTTP/HTTPS traffic." 10 60
else
dialog --title "Firewall" --msgbox "Skipped UFW firewall configuration.\nMake sure ports 80 and 443 are open." 10 60
if dialog --title "Firewall" --yesno "Do you want ufw to be started and enabled (without opening ports)?" 10 60; then
execute_with_loading "systemctl start ufw && systemctl enable ufw" "Starting UFW"
dialog --title "Firewall" --msgbox "UFW started and enabled, but no ports were opened." 10 60
fi
fi
}
setup_ssl() {
dialog --title "SSL Certificate" --infobox "Setting up SSL certificate..." 10 60
execute_with_loading "apt-get install -y certbot python3-certbot-nginx" "Installing Certbot"
# Create temporary nginx config
cat >/etc/nginx/sites-available/temp-dezerx <<EOF
server {
listen 80;
server_name $DOMAIN;
root /var/www/html;
index index.html;
location / {
try_files \$uri \$uri/ =404;
}
}
EOF
ln -sf /etc/nginx/sites-available/temp-dezerx /etc/nginx/sites-enabled/temp-dezerx
systemctl reload nginx
if ! certbot certonly --nginx -d "$DOMAIN" --non-interactive --agree-tos --email "admin@$DOMAIN" --no-eff-email; then
dialog --title "SSL Error" --msgbox "Failed to obtain SSL certificate\n\nPlease ensure:\n1. Domain $DOMAIN points to this server\n2. Ports 80 and 443 are open\n3. No firewall is blocking the connection" 15 70
exit 1
fi
rm -f /etc/nginx/sites-enabled/temp-dezerx
rm -f /etc/nginx/sites-available/temp-dezerx
dialog --title "SSL" --msgbox "SSL certificate obtained successfully!" 10 60
}
setup_ssl_skip() {
dialog --title "SSL Certificate" --msgbox "You selected HTTP. Skipping SSL certificate setup." 10 60
}
configure_nginx() {
dialog --title "Configuring Nginx" --infobox "Setting up Nginx configuration..." 10 60
rm -f /etc/nginx/sites-available/default
rm -f /etc/nginx/sites-enabled/default
if [[ "$PROTOCOL" == "https" ]]; then
cat >/etc/nginx/sites-available/dezerx.conf <<EOF
server {
listen 80;
server_name $DOMAIN;
return 301 https://\$server_name\$request_uri;
}
server {
listen 443 ssl http2;
server_name $DOMAIN;
root $INSTALL_DIR/public;
index index.php;
ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem;
client_max_body_size 100m;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
EOF
else
cat >/etc/nginx/sites-available/dezerx.conf <<EOF
server {
listen 80;
server_name $DOMAIN;
root $INSTALL_DIR/public;
index index.php;
client_max_body_size 100m;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
EOF
fi
ln -sf /etc/nginx/sites-available/dezerx.conf /etc/nginx/sites-enabled/dezerx.conf
if ! nginx -t >>"$LOG_FILE" 2>&1; then
dialog --title "Error" --msgbox "Nginx configuration test failed" 10 60
exit 1
fi
execute_with_loading "systemctl restart nginx" "Restarting Nginx"
dialog --title "Nginx" --msgbox "Nginx configured successfully!" 10 60
}
install_nodejs_and_build() {
dialog --title "Building Assets" --infobox "Installing Node.js and building assets..." 10 60
if [[ "$OPERATION_MODE" == "install" ]]; then
execute_with_loading "curl -fsSL https://deb.nodesource.com/setup_20.x | bash -" "Adding Node.js repository"
execute_with_loading "apt-get install -y nodejs" "Installing Node.js"
fi
cd "$INSTALL_DIR"
if [[ -f "package.json" ]]; then
execute_with_loading "npm install" "Installing npm dependencies"
execute_with_loading "npm run build" "Building assets"
fi
dialog --title "Assets" --msgbox "Assets built successfully!" 10 60
}
set_permissions() {
dialog --title "Setting Permissions" --infobox "Setting file permissions..." 10 60
execute_with_loading "chown -R www-data:www-data $INSTALL_DIR" "Setting ownership"
execute_with_loading "chmod -R 755 $INSTALL_DIR" "Setting base permissions"
execute_with_loading "chmod -R 775 $INSTALL_DIR/storage" "Setting storage permissions"
execute_with_loading "chmod -R 775 $INSTALL_DIR/bootstrap/cache" "Setting cache permissions"
if [[ "$OPERATION_MODE" == "update" ]]; then
execute_with_loading "chown -R www-data:www-data $INSTALL_DIR/*" "Additional ownership fixes"
execute_with_loading "chown -R www-data:www-data $INSTALL_DIR/.[^.]*" "Hidden files ownership"
fi