-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
1297 lines (1098 loc) · 39.2 KB
/
install.sh
File metadata and controls
1297 lines (1098 loc) · 39.2 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
#!/bin/bash
# Server Configuration Script
# This script provides a menu-driven interface to perform various server configuration tasks.
# It allows users to install essential apps, set up a web server, configure NVM, enable passwordless sudo,
# set up SSH key-based authentication and more.
# Author: Decaded (https://github.com/Decaded)
# Script version
SCRIPT_VERSION="2.1.0"
SCRIPT_URL="https://raw.githubusercontent.com/Decaded/install-script/refs/heads/main/install.sh"
# Function to display a menu and get user's choice
show_menu() {
clear
echo "╔════════════════════════════════════════════════════════╗"
echo "║ Server Configuration Script v$SCRIPT_VERSION"
echo "║ Running as: $USER"
echo "╚════════════════════════════════════════════════════════╝"
echo
echo "1) Install Essential Apps"
echo "2) Install Web Server (LEMP/NGINX)"
echo "3) Install Node Version Manager (NVM)"
echo "4) Enable Passwordless sudo access"
echo "5) Set up SSH key-based authentication"
echo "6) Configure Static IP Address"
echo "7) Configure Fail2ban"
echo
echo "r) Revert Static IP to DHCP"
echo "u) Check for script updates"
echo
if [ -f "/etc/ssh/sshd_config_decoscript.backup" ]; then
echo "9) Restore SSH Configuration"
fi
echo "0) Exit"
echo
read -rp "Enter your choice: " choice
case $choice in
1) install_essential_apps ;;
2) install_web_server_menu ;;
3) install_nvm ;;
4) enable_passwordless_sudo "$USER" ;;
5) setup_ssh_key_authentication ;;
6) configure_static_ip ;;
7) configure_fail2ban ;;
r|R) revert_static_ip ;;
u|U) check_for_updates ;;
9)
if [ -f "/etc/ssh/sshd_config_decoscript.backup" ]; then
restore_ssh_config
else
echo "Invalid choice. Please select a valid option."
show_menu
fi
;;
0)
clear
read -rp "Do you want to remove the 'install.sh' script file? (Y/n): " delete_script
if [[ "$delete_script" =~ ^[Yy]$ || "$delete_script" == "" ]]; then
echo "Deleting the script file..."
rm "$0"
else
echo
echo "You chose to keep the script file."
echo "You can remove this script manually using 'rm install.sh'"
fi
echo
echo "Exiting. Goodbye!"
exit
;;
*)
echo "Invalid choice. Please select a valid option."
show_menu
;;
esac
}
# Function to check if the script has sudo privileges
check_sudo_privileges() {
sudo -n true
if [ $? -ne 0 ]; then
echo "You need sudo privilege to run this script."
exit 1
fi
}
# Bash-native CIDR to netmask conversion
cidr_to_netmask() {
local bits=$1
local mask=$((0xffffffff ^ ((1 << (32 - bits)) - 1)))
printf "%d.%d.%d.%d\n" \
$(((mask >> 24) & 0xff)) \
$(((mask >> 16) & 0xff)) \
$(((mask >> 8) & 0xff)) \
$((mask & 0xff))
}
# Function to check for script updates
check_for_updates() {
clear
echo "Checking for script updates..."
echo
# Check if curl is available
if ! command -v curl >/dev/null 2>&1; then
echo "curl is not installed. Cannot check for updates."
echo "Install curl to enable update checking: sudo apt install curl"
return 0
fi
# Try to fetch remote version (allow failure)
local remote_version=""
remote_version=$(curl -fsSL --max-time 5 "$SCRIPT_URL" 2>/dev/null | grep -m1 "^SCRIPT_VERSION=" | cut -d'"' -f2 || true)
# If we couldn't get the remote version, fail gracefully
if [ -z "$remote_version" ]; then
echo "Could not check for updates (network issue or GitHub unavailable)."
echo "Current version: $SCRIPT_VERSION"
return 0
fi
# Compare versions
if [ "$remote_version" != "$SCRIPT_VERSION" ]; then
echo "================================"
echo " UPDATE AVAILABLE!"
echo "================================"
echo "Current version: $SCRIPT_VERSION"
echo "Latest version: $remote_version"
echo
read -rp "Would you like to update now? (y/N): " update_choice
if [[ "$update_choice" =~ ^[Yy]$ ]]; then
update_script
else
echo "Update skipped. You can update later by selecting option 'u' from the menu."
fi
else
echo "You are running the latest version ($SCRIPT_VERSION)"
echo "No update needed."
fi
echo
read -rp "Press Enter to return to menu..."
return 0
}
# Function to update the script
update_script() {
echo
echo "Downloading latest version..."
local temp_script="/tmp/install_sh_update_$"
if curl -fsSL --max-time 10 -o "$temp_script" "$SCRIPT_URL" 2>/dev/null; then
# Verify the download
if [ -f "$temp_script" ] && [ -s "$temp_script" ]; then
chmod +x "$temp_script"
# Backup current script and remove execute permissions from backup
cp "$0" "${0}.backup" 2>/dev/null && chmod -x "${0}.backup" 2>/dev/null || true
# Replace with new version
mv "$temp_script" "$0"
echo "✓ Script updated successfully!"
echo " Backup saved as: ${0}.backup"
echo
echo "Please run the script again to use the new version."
echo "Exiting..."
exit 0
else
echo "Error: Downloaded file is empty or invalid."
rm -f "$temp_script" 2>/dev/null
return 1
fi
else
echo "Error: Failed to download update."
rm -f "$temp_script" 2>/dev/null
return 1
fi
}
# Function to install essential apps using dialog
install_essential_apps() {
clear
# Check if dialog is installed, and if not, install it
if ! [ -x "$(command -v dialog)" ]; then
echo "Dialog is not installed. Installing dialog..."
sudo apt update && sudo apt install dialog -y
# Check if the installation was successful
if [ $? -ne 0 ]; then
echo "Error: Failed to install dialog. Exiting."
return
fi
fi
if ! [ -x "$(command -v curl)" ]; then
echo "Curl is not installed. Installing curl..."
sudo apt update && sudo apt install curl -y
# Check if the installation was successful
if [ $? -ne 0 ]; then
echo "Error: Failed to install curl. Exiting."
return
fi
fi
# Define the dialog menu options
app_options=("1" "htop - Interactive process viewer" off
"2" "screen - Terminal multiplexer" off
"3" "nload - Network traffic monitor" off
"4" "nano - Text editor" off
"5" "firewalld - Firewall management" off
"6" "fail2ban - Intrusion prevention system" off
"7" "unattended-upgrades - Automatic updates" off
"8" "git - Version control system" off
"9" "pi-hole - Ad blocker and DHCP server" off)
# Display the dialog menu and store the user's choices
choices=$(dialog --clear --title "Essential Apps Installer" --checklist "Choose which apps to install:" 0 0 0 "${app_options[@]}" 2>&1 >/dev/tty)
# Check if the user canceled or made no selection
if [ $? -ne 0 ]; then
clear
echo "Canceled. Returning to the main menu."
return
fi
# Strip quotes from dialog output
choices=$(echo "$choices" | tr -d '"')
# Process user choices and install selected apps
selected_applications=""
for choice in $choices; do
case $choice in
1) selected_applications+=" htop" ;;
2) selected_applications+=" screen" ;;
3) selected_applications+=" nload" ;;
4) selected_applications+=" nano" ;;
5) selected_applications+=" firewalld" ;;
6) selected_applications+=" fail2ban" ;;
7) selected_applications+=" unattended-upgrades" ;;
8) selected_applications+=" git" ;;
9) selected_applications+=" pi-hole" ;;
esac
done
if [ -z "$selected_applications" ]; then
echo "No apps selected. Returning to the main menu."
return
fi
# Check if Pi-hole was selected
if [[ "$selected_applications" == *"pi-hole"* ]]; then
# Pi-hole installation
echo "Installing Pi-hole..."
curl -sSL https://install.pi-hole.net | bash
if [ $? -ne 0 ]; then
echo "Error: Failed to install Pi-hole. Please check your internet connection and try again."
return
fi
fi
# Remove Pi-hole from the list of selected applications
selected_applications="${selected_applications//pi-hole/}"
# Check if there are any remaining selected applications
if [ -z "$selected_applications" ]; then
echo "No apps selected. Returning to the main menu."
return
fi
# Install the remaining selected applications using apt
echo "Installing selected apps: $selected_applications"
sudo apt update && sudo apt install $selected_applications -y
# Check if there was an error during installation
if [ $? -ne 0 ]; then
echo "Error: Failed to install some or all of the selected apps. Please check your internet connection and try again."
return
fi
# Check if firewalld was selected
if [[ "$selected_applications" == *"firewalld"* ]]; then
configure_firewall
fi
# Check if Fail2ban was selected
if [[ "$selected_applications" == *"fail2ban"* ]]; then
configure_fail2ban
fi
# Check if unattended-upgrades was selected
if [[ "$selected_applications" == *"unattended-upgrades"* ]]; then
sudo dpkg-reconfigure -plow unattended-upgrades
fi
# Configure Git only if it was selected
if [[ "$selected_applications" == *"git"* ]]; then
configure_git
fi
echo "Installation complete."
return
}
# Function to configure the firewall with checks
configure_firewall() {
clear
# Check if firewalld is installed
if ! command -v firewall-cmd &>/dev/null; then
echo "Firewalld is not installed. Please install it before configuring firewall rules."
return
fi
# Enable firewalld
sudo systemctl enable firewalld
echo "#######################################################"
echo "Firewall configuration"
echo "## WARNING ##"
echo "## THIS CAN CUT YOU OUT OF THE SERVER ##"
echo "## CHECK TWICE BEFORE PROCEEDING ##"
echo "## YOU HAVE BEEN WARNED ##"
echo "#######################################################"
read -rp "Please provide your current SSH port (default is 22): " sshPort
# Check if the SSH port is already open
if sudo firewall-cmd --list-ports | grep -q "$sshPort/tcp"; then
echo "Port $sshPort [TCP] is already open. Skipping."
return
fi
validate_port "$sshPort"
if [ $? -ne 0 ]; then
echo "Invalid port input. Exiting."
exit 1
fi
echo "Opening port $sshPort TCP..."
sudo firewall-cmd --permanent --zone=public --add-port="$sshPort"/tcp
if [ $? -ne 0 ]; then
echo "Error: Failed to open firewall port."
exit 1
fi
echo "Reload configuration..."
sudo firewall-cmd --reload
if [ $? -ne 0 ]; then
echo "Error: Failed to reload firewall configuration."
exit 1
fi
echo
}
# Function to set up SSH key-based authentication
setup_ssh_key_authentication() {
clear
# Check if SSH service is installed
if ! dpkg -l | grep -q "openssh-server"; then
echo "SSH service (openssh-server) is not installed."
# Ask the user if they want to install SSH service
read -rp "Do you want to install SSH service? (Y/n): " install_ssh_service
if [[ "$install_ssh_service" =~ ^[Yy]$ || "$install_ssh_service" == "" ]]; then
sudo apt update
sudo apt install openssh-server -y
else
echo "SSH service will not be installed. Returning to the main menu."
return
fi
fi
clear
# Backup with max 5 kept
local max_backups=5
local backup_name="/etc/ssh/sshd_config_decoscript.backup.$(date +%Y%m%d%H%M%S)"
# Create a backup of the sshd_config file
sudo cp /etc/ssh/sshd_config "$backup_name"
echo "Backup created: $backup_name"
# Clean old backups, keep only the most recent $max_backups
local backup_count=$(ls -1t /etc/ssh/sshd_config_decoscript.backup.* 2>/dev/null | wc -l)
if [ "$backup_count" -gt "$max_backups" ]; then
echo "Cleaning old backups (keeping $max_backups most recent)..."
ls -1t /etc/ssh/sshd_config_decoscript.backup.* 2>/dev/null | tail -n +$((max_backups + 1)) | xargs -r sudo rm -f
fi
echo "#######################################################"
echo "SSH configuration"
echo "Backup was made to '$backup_name'."
echo "You can restore it using restore function in this script."
echo "Please provide your public key below."
echo "#######################################################"
# Read the user-provided public key and save it to a variable
IFS= read -r ssh_public_key
# Create the ~/.ssh directory if it doesn't exist
mkdir -p "$HOME/.ssh"
authorized_keys_file="$HOME/.ssh/authorized_keys"
# Check if the authorized_keys file exists and the key is not already present
if [ -f "$authorized_keys_file" ] && ! grep -q "$ssh_public_key" "$authorized_keys_file"; then
# Save the public key to the authorized_keys file
echo "$ssh_public_key" >>"$authorized_keys_file"
if [ $? -ne 0 ]; then
echo "Error: Failed to save the public key to authorized_keys file."
exit 1
fi
echo
echo "Public key added to authorized_keys."
elif [ ! -f "$authorized_keys_file" ]; then
echo "Creating authorized_keys file..."
echo "$ssh_public_key" >"$authorized_keys_file"
if [ $? -ne 0 ]; then
echo "Error: Failed to create authorized_keys file."
exit 1
fi
echo
echo "Public key added to authorized_keys."
else
echo
echo "The provided public key is already present in authorized_keys. No changes were made."
fi
# Enable key-based authentication and disable password-based authentication for SSH
sudo sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
sudo sed -i 's/^#PubkeyAuthentication yes/PubkeyAuthentication yes/' /etc/ssh/sshd_config
sudo sed -i 's/^#PubkeyAuthentication no/PubkeyAuthentication yes/' /etc/ssh/sshd_config
sudo sed -i 's/^PubkeyAuthentication no/PubkeyAuthentication yes/' /etc/ssh/sshd_config
# Restart the SSH service for changes to take effect
sudo service ssh restart
if [ $? -ne 0 ]; then
echo "Error: Failed to restart the SSH service."
exit 1
fi
echo "SSH key-based authentication has been enabled, and password-based authentication has been disabled."
echo "#######################################################"
echo
}
# Function to enable passwordless sudo access
enable_passwordless_sudo() {
clear
local username="$1"
if sudo grep -qE "^\s*$username\s+ALL=\(ALL\) NOPASSWD:ALL\s*$" /etc/sudoers; then
echo "Passwordless sudo access is already enabled for '$username'."
else
echo -n "Do you really want to enable passwordless sudo access for '$username'? (y/n): "
read -r enable_sudo_option
if [[ "$enable_sudo_option" =~ ^[Yy]$ ]]; then
# Append to /etc/sudoers using echo and sudo
echo "$username ALL=(ALL) NOPASSWD:ALL" | sudo EDITOR='tee -a' visudo
if [ $? -ne 0 ]; then
echo "Error: Failed to enable passwordless sudo access."
exit 1
fi
echo "Passwordless sudo access has been enabled for '$username'."
echo "Please log out and log back in for the changes to take effect."
else
echo "Passwordless sudo access will not be enabled."
fi
fi
echo
}
# Function to install NGINX and PHP with firewall checks
install_nginx_and_php() {
clear
local nginx_installed=false
# Check if NGINX is already installed
if dpkg -l | grep -q "nginx"; then
echo "NGINX is already installed. Skipping NGINX installation."
nginx_installed=true
else
# Install NGINX
sudo apt install nginx -y
nginx_installed=true
fi
# Detect latest available PHP version
echo "Detecting latest available PHP version..."
local php_versions=(8.4 8.3 8.2 8.1 8.0)
local php_version=""
for ver in "${php_versions[@]}"; do
if apt-cache policy "php$ver" 2>/dev/null | grep -q 'Candidate:'; then
php_version="$ver"
break
fi
done
if [ -z "$php_version" ]; then
echo "No specific PHP version found, using default 'php' package."
if $nginx_installed; then
sudo apt install php php-fpm -y
else
sudo apt install php -y
fi
else
echo "Found PHP $php_version available."
if $nginx_installed; then
sudo apt install "php$php_version" "php$php_version-fpm" -y
else
sudo apt install "php$php_version" -y
fi
fi
# Remove apache2 if it exists
if dpkg -l | awk '/apache2/ {print }' | grep -q .; then
echo "Apache2 is installed. Removing."
sudo service apache2 stop
sudo apt remove apache2 -y
sudo apt purge apache2 -y
sudo apt autoremove -y
sudo systemctl start nginx || true
fi
echo "#######################################################"
echo "Firewall configuration"
echo "#######################################################"
# Check if firewalld is installed
if ! command -v firewall-cmd &>/dev/null; then
echo "Firewalld is not installed. Skipping."
else
# Check if port 80 is open
if ! sudo firewall-cmd --list-ports | grep -q "80/tcp"; then
echo "Opening port 80 [TCP]..."
sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
else
echo "Port 80 [TCP] is already open. Skipping."
fi
# Check if port 443 is open
if ! sudo firewall-cmd --list-ports | grep -q "443/tcp"; then
echo "Opening port 443 [TCP]..."
sudo firewall-cmd --permanent --zone=public --add-port=443/tcp
else
echo "Port 443 [TCP] is already open. Skipping."
fi
echo "Reload configuration..."
sudo firewall-cmd --reload
echo
fi
# Create a directory for SSL certs if it doesn't exist
if [ ! -d "/etc/nginx/cert" ]; then
echo "Creating directory /etc/nginx/cert"
sudo mkdir -p /etc/nginx/cert
sudo chmod 700 /etc/nginx/cert
fi
echo
echo "Finished setting up NGINX and PHP."
echo "You can upload SSL certificates into /etc/nginx/cert"
echo
}
# Function to show web server installation menu
install_web_server_menu() {
clear
echo "╔════════════════════════════════════════════════════════╗"
echo "║ Web Server Installation Menu ║"
echo "╚════════════════════════════════════════════════════════╝"
echo
echo "Choose what to install:"
echo
echo "1) LEMP Stack (Linux + NGINX + MySQL + PHP)"
echo "2) NGINX + PHP"
echo "3) NGINX only"
echo "0) Back to main menu"
echo
read -rp "Enter your choice: " web_choice
case $web_choice in
1) install_lemp_stack ;;
2) install_nginx_php ;;
3) install_nginx_only ;;
0) return ;;
*)
echo "Invalid choice."
sleep 2
install_web_server_menu
;;
esac
}
# Function to install LEMP stack
install_lemp_stack() {
clear
echo "Installing LEMP Stack (NGINX + MySQL + PHP)..."
echo
# Install NGINX
install_nginx_base
# Install MySQL
echo
echo "Installing MySQL Server..."
sudo apt install mysql-server -y
# Secure MySQL installation prompt
echo
echo "MySQL installed. It's recommended to run mysql_secure_installation."
read -rp "Do you want to run mysql_secure_installation now? (Y/n): " run_secure
if [[ "$run_secure" =~ ^[Nn]$ ]]; then
echo "You can run 'sudo mysql_secure_installation' manually later."
else
sudo mysql_secure_installation
fi
# Install PHP
echo
install_php_with_extensions
# Configure NGINX for PHP
configure_nginx_php
# Final setup
finalize_web_server_install
echo
echo "═══════════════════════════════════════════════════════"
echo "LEMP Stack installation completed!"
echo "═══════════════════════════════════════════════════════"
echo "Services installed:"
echo " - NGINX web server"
echo " - MySQL database server"
echo " - PHP with PHP-FPM"
echo
echo "Next steps:"
echo " 1. Place your website files in /var/www/html/"
echo " 2. Configure NGINX sites in /etc/nginx/sites-available/"
echo " 3. SSL certificates go in /etc/nginx/cert/"
echo " 4. MySQL: sudo mysql -u root -p"
echo "═══════════════════════════════════════════════════════"
read -rp "Press Enter to continue..."
}
# Function to install NGINX + PHP
install_nginx_php() {
clear
echo "Installing NGINX + PHP..."
echo
install_nginx_base
echo
install_php_with_extensions
configure_nginx_php
finalize_web_server_install
echo
echo "═══════════════════════════════════════════════════════"
echo "NGINX + PHP installation completed!"
echo "═══════════════════════════════════════════════════════"
echo "Services installed:"
echo " - NGINX web server"
echo " - PHP with PHP-FPM"
echo
echo "Next steps:"
echo " 1. Place your website files in /var/www/html/"
echo " 2. Configure NGINX sites in /etc/nginx/sites-available/"
echo " 3. SSL certificates go in /etc/nginx/cert/"
echo "═══════════════════════════════════════════════════════"
read -rp "Press Enter to continue..."
}
# Function to install NGINX only
install_nginx_only() {
clear
echo "Installing NGINX only..."
echo
install_nginx_base
configure_nginx_static
finalize_web_server_install
echo
echo "═══════════════════════════════════════════════════════"
echo "NGINX installation completed!"
echo "═══════════════════════════════════════════════════════"
echo "Service installed:"
echo " - NGINX web server"
echo
echo "Next steps:"
echo " 1. Place your static files in /var/www/html/"
echo " 2. Configure NGINX sites in /etc/nginx/sites-available/"
echo " 3. SSL certificates go in /etc/nginx/cert/"
echo "═══════════════════════════════════════════════════════"
read -rp "Press Enter to continue..."
}
# Helper: Install NGINX base
install_nginx_base() {
if dpkg -l | grep -q "^ii.*nginx"; then
echo "NGINX is already installed."
else
echo "Installing NGINX..."
sudo apt install nginx -y
fi
# Remove apache2 if it exists
if dpkg -l | grep -q "^ii.*apache2"; then
echo "Apache2 detected. Removing to avoid conflicts..."
sudo systemctl stop apache2 2>/dev/null || true
sudo apt remove --purge apache2 apache2-utils apache2-bin apache2.2-common -y
sudo apt autoremove -y
fi
# Ensure NGINX is enabled and started
sudo systemctl enable nginx
sudo systemctl start nginx
}
# Helper: Install PHP with common extensions
install_php_with_extensions() {
echo "Detecting latest available PHP version..."
local php_versions=(8.4 8.3 8.2 8.1 8.0)
local php_version=""
for ver in "${php_versions[@]}"; do
if apt-cache policy "php$ver" 2>/dev/null | grep -q 'Candidate:'; then
php_version="$ver"
break
fi
done
if [ -z "$php_version" ]; then
echo "Using default PHP packages..."
sudo apt install php php-fpm php-mysql php-cli php-common php-curl php-gd php-mbstring php-xml php-zip -y
else
echo "Installing PHP $php_version with extensions..."
sudo apt install "php$php_version" "php$php_version-fpm" "php$php_version-mysql" \
"php$php_version-cli" "php$php_version-common" "php$php_version-curl" \
"php$php_version-gd" "php$php_version-mbstring" "php$php_version-xml" \
"php$php_version-zip" -y
fi
# Enable and start PHP-FPM
sudo systemctl enable php*-fpm
sudo systemctl start php*-fpm
}
# Helper: Configure NGINX for PHP
configure_nginx_php() {
echo
echo "Configuring NGINX for PHP..."
# Backup default config
if [ -f /etc/nginx/sites-available/default ]; then
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup
fi
# Detect PHP-FPM socket
local php_socket=$(ls /run/php/php*-fpm.sock 2>/dev/null | head -1)
if [ -z "$php_socket" ]; then
echo "Warning: Could not detect PHP-FPM socket. Using default."
php_socket="/run/php/php-fpm.sock"
fi
# Create a working default config
sudo tee /etc/nginx/sites-available/default >/dev/null <<EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm;
server_name _;
location / {
try_files \$uri \$uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:$php_socket;
}
location ~ /\.ht {
deny all;
}
}
EOF
# Test and reload NGINX
if sudo nginx -t 2>/dev/null; then
sudo systemctl reload nginx
echo "NGINX configured for PHP successfully."
else
echo "Warning: NGINX configuration test failed. Please check manually."
fi
# Create a test PHP file
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php >/dev/null
echo
echo "Test PHP installation: http://your-server-ip/info.php"
echo "Remember to delete /var/www/html/info.php after testing!"
}
# Helper: Configure NGINX for static files only
configure_nginx_static() {
echo
echo "Configuring NGINX for static content..."
# Backup default config
if [ -f /etc/nginx/sites-available/default ]; then
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup
fi
# Create a clean static config
sudo tee /etc/nginx/sites-available/default >/dev/null <<EOF
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm;
server_name _;
location / {
try_files \$uri \$uri/ =404;
}
location ~ /\.ht {
deny all;
}
}
EOF
# Test and reload NGINX
if sudo nginx -t 2>/dev/null; then
sudo systemctl reload nginx
echo "NGINX configured successfully."
else
echo "Warning: NGINX configuration test failed. Please check manually."
fi
}
# Helper: Finalize web server installation
finalize_web_server_install() {
echo
echo "Finalizing installation..."
# Create cert directory
if [ ! -d "/etc/nginx/cert" ]; then
sudo mkdir -p /etc/nginx/cert
sudo chmod 700 /etc/nginx/cert
echo "Created /etc/nginx/cert for SSL certificates."
fi
# Ensure web root exists with correct permissions
if [ ! -d "/var/www/html" ]; then
sudo mkdir -p /var/www/html
fi
sudo chown -R www-data:www-data /var/www/html
# Create a simple index.html if none exists
if [ ! -f "/var/www/html/index.html" ] && [ ! -f "/var/www/html/index.php" ]; then
sudo tee /var/www/html/index.html >/dev/null <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Welcome to NGINX</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; padding: 50px; }
h1 { color: #009639; }
</style>
</head>
<body>
<h1>NGINX is working!</h1>
<p>If you see this page, the web server is successfully installed and working.</p>
</body>
</html>
EOF
sudo chown www-data:www-data /var/www/html/index.html
fi
# Configure firewall if available
if command -v firewall-cmd &>/dev/null; then
echo
echo "Configuring firewall..."
if ! sudo firewall-cmd --list-ports | grep -q "80/tcp"; then
sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
echo "Opened port 80 (HTTP)."
fi
if ! sudo firewall-cmd --list-ports | grep -q "443/tcp"; then
sudo firewall-cmd --permanent --zone=public --add-port=443/tcp
echo "Opened port 443 (HTTPS)."
fi
sudo firewall-cmd --reload
fi
echo "Setup complete!"
}
# Function to install Node Version Manager (NVM)
install_nvm() {
clear
# Using master branch to always get latest NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
echo "Fetching available NodeJS versions (showing latest 20)..."
nvm ls-remote | tail -20
echo
echo "Above you can see a list of the latest available NodeJS versions."
echo "Choose NodeJS version to install (e.g., 18.17.0):"
read -r versionToInstall
if [ -n "$versionToInstall" ]; then
nvm install "$versionToInstall"
else
echo "No version specified, skipping Node installation."
fi
echo
}
# Function to restore SSH configuration
restore_ssh_config() {
clear
# Find the most recent backup
local backup
backup=$(ls -1t /etc/ssh/sshd_config_decoscript.backup.* 2>/dev/null | head -n1)
if [ -z "$backup" ]; then
echo "Error: No backup files found matching /etc/ssh/sshd_config_decoscript.backup.*"
return
fi
echo "Found backup: $backup"
read -rp "Do you want to restore SSH configuration from this backup? (y/N): " confirm_restore
if [[ ! "$confirm_restore" =~ ^[Yy]$ ]]; then
echo "Restore cancelled."
return
fi
echo "Restoring SSH configuration..."
sudo cp "$backup" /etc/ssh/sshd_config
if [ $? -ne 0 ]; then
echo "Error: Failed to restore SSH configuration."
exit 1
fi
sudo service ssh restart
if [ $? -ne 0 ]; then
echo "Error: Failed to restart the SSH service."
exit 1
fi
echo "SSH configuration has been restored."
read -rp "Do you want to keep the backup file? (Y/n): " keep_backup
if [[ "$keep_backup" =~ ^[Nn]$ ]]; then
sudo rm "$backup"
echo "Backup file deleted."
else
echo "Backup file kept at: $backup"
fi
}
# Function to validate if a given input is a valid port number
validate_port() {
local port="$1"