NGINX + Opening ports installation
Choose your system:
Variants:
Install/Uninstall script (Clean /
Warning: Undefined array key "
Deprecated: htmlspecialchars(): Passing null to parameter " in /var/www/scripts/PHP_HTML/maininstall.php on line 49
Warning: Trying to access array offset on null in /var/www/scripts/PHP_HTML/maininstall.php on line 49
Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/scripts/PHP_HTML/maininstall.php on line 49
)
#!/bin/bash
set +e
# ========== COLORS ==========
NC='\033[0m'
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
BLUE='\033[38;5;117m'
BOLD='\033[1m'
# ========== LOG FUNCTIONS ==========
log_info() { echo -e "${BLUE}[INFO]${NC} $*"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
log_error() { echo -e "${RED}[ERROR]${NC} $*"; }
log_success() { echo -e "${GREEN}[OK]${NC} $*"; }
# ========== PRINT BOX FUNCTION ==========
print_in_box() {
local title="$1"
shift
local lines=("$@")
local max_len=${#title}
for line in "${lines[@]}"; do
(( ${#line} > max_len )) && max_len=${#line}
done
local border_top="┌$(printf '─%.0s' $(seq 1 $((max_len + 2))))┐"
local border_bottom="└$(printf '─%.0s' $(seq 1 $((max_len + 2))))┘"
echo -e "${BLUE}${border_top}${NC}"
printf "${BLUE}│ ${BOLD}%-*s${BLUE} │${NC}\n" "$max_len" "$title"
echo -e "${BLUE}├$(printf '─%.0s' $(seq 1 $((max_len + 2))))┤${NC}"
for line in "${lines[@]}"; do
printf "${BLUE}│ %-*s │${NC}\n" "$max_len" "$line"
done
echo -e "${BLUE}${border_bottom}${NC}"
}
# ========== SPINNER & RUN TASK ==========
spinner_pid=""
start_spinner() {
local msg="$1"
echo -ne "${BLUE}[INFO]${NC} $msg... "
trap stop_spinner INT
(
local spin='-\|/'
while true; do
for i in $(seq 0 $(( ${#spin} - 1 )) ); do
echo -ne "\b${spin:$i:1}"
sleep 0.1
done
done
) & spinner_pid=$!
disown
}
stop_spinner() {
if [[ -n "$spinner_pid" ]]; then
kill "$spinner_pid" &>/dev/null || true
wait "$spinner_pid" 2>/dev/null || true
spinner_pid=""
fi
echo -e "\b${GREEN}✓${NC}"
trap - INT
}
run_step() {
start_spinner "$1"
shift
"$@" &>/dev/null
stop_spinner
}
run_multistep() {
start_spinner "$1"
shift
{
"$@"
} &>/dev/null
stop_spinner
}
# ========== DETECT DISTRIBUTION ==========
DISTRO=""
if [[ -f /etc/os-release ]]; then
. /etc/os-release
DISTRO=$(echo "$ID" | tr '[:upper:]' '[:lower:]')
fi
[[ "$DISTRO" == "rhel" ]] && DISTRO="centos"
log_info "Detected distribution: ${BOLD}${DISTRO}${NC}"
# ========== PACKAGE MANAGER COMMANDS ==========
case "$DISTRO" in
debian|ubuntu)
PKG_UPDATE="sudo apt update -y && sudo apt upgrade -y"
PKG_INSTALL="sudo apt install -y"
PKG_REMOVE="sudo apt remove --purge -y"
PKG_CLEAN="sudo apt autoremove -y && sudo apt clean"
PHP_PACKAGES="php php-cli php-fpm php-common php-mbstring php-xml php-mysql"
;;
almalinux|rocky|centos)
PKG_UPDATE="sudo dnf update -y"
PKG_INSTALL="sudo dnf install -y"
PKG_REMOVE="sudo dnf remove -y"
PKG_CLEAN="sudo dnf autoremove -y && sudo dnf clean all"
PHP_PACKAGES="php php-cli php-fpm php-common php-mbstring php-xml php-mysqlnd"
;;
*)
log_error "Unsupported distribution: $DISTRO"
exit 1
;;
esac
FIREWALLD_PKG="firewalld"
NGINX_PKG="nginx"
FIREWALLD_CMD="firewall-cmd"
# ========== MENU ==========
echo -e "\n${BOLD}${YELLOW}=== NGINX + firewalld Installer / Uninstaller ===${NC}\n"
PS3="Choose an option: "
select opt in "Install" "Uninstall" "Exit"; do
case $opt in
Install) ACTION=1; break;;
Uninstall) ACTION=2; break;;
Exit) exit 0;;
*) log_error "Invalid option";;
esac
done
# ========== INSTALL ==========
if [[ "$ACTION" == "1" ]]; then
echo -e "\n${BOLD}${YELLOW}=== Installation ===${NC}\n"
if command -v nginx >/dev/null 2>&1; then
log_warn "nginx is already installed."
read -rp "$(echo -e "${YELLOW}[INFO]${NC} Overwrite configuration? [y/n]: ")" overwrite_install
if [[ ! "$overwrite_install" =~ ^[Yy]$ ]]; then
log_info "Installation cancelled."
exit 0
fi
fi
run_multistep "Updating system" bash -c "$PKG_UPDATE"
run_multistep "Installing nginx and firewalld" bash -c "$PKG_INSTALL $NGINX_PKG $FIREWALLD_PKG"
run_step "Starting firewalld" sudo systemctl start firewalld
run_step "Enabling firewalld" sudo systemctl enable firewalld
run_step "Starting nginx" sudo systemctl start nginx
run_step "Enabling nginx" sudo systemctl enable nginx
read -p "$(echo -e "${YELLOW}[INFO]${NC} Install PHP and extensions? [y/n]: ")" install_php
if [[ "$install_php" =~ ^[Yy]$ ]]; then
run_multistep "Installing PHP" bash -c "$PKG_INSTALL $PHP_PACKAGES"
if command -v php >/dev/null 2>&1; then
php_version=$(php -v | head -n 1)
log_info "Installed PHP version: $php_version"
else
log_error "PHP installation failed."
fi
fi
open80_status=$(sudo $FIREWALLD_CMD --list-services | grep -qw http && echo "open" || echo "closed")
open443_status=$(sudo $FIREWALLD_CMD --list-services | grep -qw https && echo "open" || echo "closed")
if [[ "$open80_status" == "closed" ]]; then
read -p "$(echo -e "${YELLOW}[INFO]${NC} Open port 80? [y/n]: ")" open80
[[ "$open80" =~ ^[Yy]$ ]] && run_step "Opening port 80" sudo $FIREWALLD_CMD --permanent --add-service=http
fi
if [[ "$open443_status" == "closed" ]]; then
read -p "$(echo -e "${YELLOW}[INFO]${NC} Open port 443? [y/n]: ")" open443
[[ "$open443" =~ ^[Yy]$ ]] && run_step "Opening port 443" sudo $FIREWALLD_CMD --permanent --add-service=https
fi
run_step "Reloading firewalld" sudo $FIREWALLD_CMD --reload
mapfile -t fw_lines < <(sudo $FIREWALLD_CMD --list-all)
print_in_box "Current firewalld settings" "${fw_lines[@]}"
local_ip=$(hostname -I | awk '{print $1}')
read -p "$(echo -e "${YELLOW}[INFO]${NC} Check public IP? [y/n]: ")" check_ip
public_ip="Skipped"
if [[ "$check_ip" =~ ^[Yy]$ ]]; then
public_ip=$(curl -s ifconfig.me || echo "Unavailable")
fi
ip_lines=("Local IP : $local_ip" "Public IP : $public_ip")
print_in_box "IP Address Info" "${ip_lines[@]}"
log_info "Nginx config: /etc/nginx/"
log_info "Firewalld config: /etc/firewalld/"
log_info "PHP config: /etc/php/, /etc/php-fpm.d/"
log_success "Installation complete."
# ========== UNINSTALL ==========
elif [[ "$ACTION" == "2" ]]; then
echo -e "\n${BOLD}${YELLOW}=== Uninstallation ===${NC}\n"
run_step "Stopping nginx" sudo systemctl stop nginx || true
run_step "Disabling nginx" sudo systemctl disable nginx || true
read -p "Completely uninstall nginx (including config)? [y/n]: " purge_nginx
if [[ "$purge_nginx" =~ ^[Yy]$ ]]; then
if [[ "$DISTRO" == "debian" || "$DISTRO" == "ubuntu" ]]; then
run_multistep "Purging nginx" bash -c "sudo apt remove --purge -y nginx*"
else
run_multistep "Removing nginx" bash -c "$PKG_REMOVE $NGINX_PKG"
log_warn "Full purge not supported on this distro."
fi
else
read -p "Remove nginx but keep config? [y/n]: " remove_nginx_only
[[ "$remove_nginx_only" =~ ^[Yy]$ ]] && run_multistep "Removing nginx" bash -c "$PKG_REMOVE $NGINX_PKG"
fi
read -p "Close ports 80 and 443 in firewalld? [y/n]: " close_ports
if [[ "$close_ports" =~ ^[Yy]$ ]]; then
run_step "Closing port 80" sudo $FIREWALLD_CMD --permanent --remove-service=http
run_step "Closing port 443" sudo $FIREWALLD_CMD --permanent --remove-service=https
run_step "Reloading firewalld" sudo $FIREWALLD_CMD --reload
fi
read -p "Remove firewalld? [y/n]: " remove_fw
if [[ "$remove_fw" =~ ^[Yy]$ ]]; then
run_step "Stopping firewalld" sudo systemctl stop firewalld || true
run_step "Disabling firewalld" sudo systemctl disable firewalld || true
run_multistep "Removing firewalld" bash -c "$PKG_REMOVE $FIREWALLD_PKG"
fi
read -p "Remove PHP and extensions? [y/n]: " remove_php
if [[ "$remove_php" =~ ^[Yy]$ ]]; then
run_multistep "Removing PHP" bash -c "$PKG_REMOVE $PHP_PACKAGES"
read -p "Delete PHP config directories? [y/n]: " delete_php_dirs
if [[ "$delete_php_dirs" =~ ^[Yy]$ ]]; then
[[ -d /etc/php ]] && run_step "Removing /etc/php" sudo rm -rf /etc/php
[[ -d /etc/php-fpm.d ]] && run_step "Removing /etc/php-fpm.d" sudo rm -rf /etc/php-fpm.d
log_success "PHP configuration directories removed."
else
log_info "PHP configuration directories preserved."
fi
fi
run_multistep "Cleaning unused packages" bash -c "$PKG_CLEAN"
log_success "Uninstallation complete."
fi
Use the following command to auto-download and run:
Warning: Undefined array key "
Deprecated: htmlspecialchars(): Passing null to parameter " in /var/www/scripts/PHP_HTML/autoscripts.php on line 87
Warning: Trying to access array offset on null in /var/www/scripts/PHP_HTML/autoscripts.php on line 87
curl -fsSL -o script.sh "https://krotek.serveblog.net/how-to-install/WEB-SERVER/NGINX/scripts.php?variant=clean&type=script&system=%3Cbr+%2F%3E%3Cb%3EDeprecated%3C%2Fb%3E%3A++htmlspecialchars%28%29%3A+Passing+null+to+parameter+" && chmod +x script.sh && sudo ./script.sh