terminal

KROTEK

menu

CADDY + Opening ports installation

Choose your system:

Debian

Debian

Logo: Tobias Benjamin Kohl
CC BY-SA
Ubuntu

Ubuntu

Logo: Canonical
CC BY-SA
Rocky Linux

Rocky Linux

Logo: Rocky Enterprise Software Foundation
CC BY-SA
Alma Linux

Alma Linux

Logo: AlmaLinux OS Foundation
CC BY-SA
Alpine Linux

Alpine Linux

Logo: Alpine Linux
CC BY-SA

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
)

download content_copy expand_more
#!/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'

# ========== SPINNER FUNCTIONS ==========
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}[OK] ✓${NC}"
  trap - INT
}

run_step() {
  start_spinner "$1"
  shift
  "$@" &>/dev/null
  stop_spinner
}

run_multistep() {
  start_spinner "$1"
  shift
  {
    "$@"
  } &>/dev/null
  stop_spinner
}

# ========== 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}"
  local padding_left=$(( (max_len - ${#title}) / 2 ))
  local padding_right=$(( max_len - padding_left - ${#title} ))
  printf "${BLUE}│%*s%s%*s│${NC}\n" $((padding_left + 1)) "" "$title" $((padding_right + 1)) ""
  printf "${BLUE}│%*s│${NC}\n" $((max_len + 2)) ""
  for line in "${lines[@]}"; do
    local padding=$((max_len - ${#line}))
    printf "${BLUE}│ %s%*s │${NC}\n" "$line" $padding ""
  done
  echo -e "${BLUE}${border_bottom}${NC}"
}

# ========== DETECT DISTRIBUTION ==========
DISTRO=""
if [[ -f /etc/os-release ]]; then
  . /etc/os-release
  DISTRO=$(echo "$ID" | tr '[:upper:]' '[:lower:]')
fi
[[ "$DISTRO" == "rhel" ]] && DISTRO="centos"
echo -e "${BLUE}[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"
    CADDY_PKG="caddy"
    ;;
  almalinux|rocky|oracle|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"
    CADDY_PKG="caddy"
    ;;
  *)
    echo -e "${RED}[ERROR] Unsupported distribution: $DISTRO${NC}"
    exit 1
    ;;
esac

FIREWALLD_PKG="firewalld"
FIREWALLD_CMD="firewall-cmd"

# ========== USER ACTION ==========
echo -e "\n${BOLD}${YELLOW}=== Caddy + firewalld Installer / Uninstaller ===${NC}\n"
echo "1) Install"
echo "2) Uninstall"
read -rp "Choose an option [1-2]: " ACTION

# ========== INSTALL ==========
if [[ "$ACTION" == "1" ]]; then
  echo -e "\n${BOLD}${YELLOW}=== Installation ===${NC}\n"

  if command -v caddy >/dev/null 2>&1; then
    echo -e "${YELLOW}[INFO]${NC} Caddy is already installed."
    read -rp "$(echo -e "${YELLOW}[INFO]${NC} Do you want to continue and overwrite the configuration? [y/n]: ")" overwrite_install
    if [[ ! "$overwrite_install" =~ ^[Yy]$ ]]; then
      echo -e "${YELLOW}[INFO]${NC} Installation cancelled."
      exit 0
    fi
  else
    if [[ -d /etc/caddy ]]; then
      echo -e "${YELLOW}[INFO]${NC} Existing Caddy configuration found at /etc/caddy."
      read -rp "$(echo -e "${YELLOW}[INFO]${NC} Do you want to overwrite the existing Caddy configuration? [y/n]: ")" overwrite_conf
      if [[ "$overwrite_conf" =~ ^[Yy]$ ]]; then
        echo -e "${YELLOW}[INFO]${NC} Existing configuration will be overwritten during installation."
        # (Optional: handle config backup here)
      else
        echo -e "${YELLOW}[INFO]${NC} Existing configuration will be preserved. Caddy will be installed without modifying configuration."
      fi
    fi
  fi

  run_multistep "Updating system" bash -c "$PKG_UPDATE"
  run_multistep "Installing caddy and firewalld" bash -c "$PKG_INSTALL $CADDY_PKG $FIREWALLD_PKG"

  run_step "Starting firewalld" sudo systemctl start firewalld
  run_step "Enabling firewalld" sudo systemctl enable firewalld
  run_step "Starting caddy" sudo systemctl start caddy
  run_step "Enabling caddy" sudo systemctl enable caddy

  read -p "$(echo -e "${YELLOW}[INFO]${NC} Install PHP and common 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)
      echo -e "${GREEN}[INFO] Installed PHP version:${NC} $php_version"
    else
      echo -e "${RED}[ERROR] PHP installation failed or not found.${NC}"
    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 (HTTP)? [y/n]: ")" open80
    if [[ "$open80" =~ ^[Yy]$ ]]; then
      run_step "Opening port 80 (HTTP)" sudo $FIREWALLD_CMD --permanent --add-service=http
      echo -e "${GREEN}[INFO]${NC} Port 80 has been opened."
    fi
  else
    echo -e "${GREEN}[INFO]${NC} Port 80 (HTTP) is already open."
  fi

  if [[ "$open443_status" == "closed" ]]; then
    read -p "$(echo -e "${YELLOW}[INFO]${NC} Open port 443 (HTTPS)? [y/n]: ")" open443
    if [[ "$open443" =~ ^[Yy]$ ]]; then
      run_step "Opening port 443 (HTTPS)" sudo $FIREWALLD_CMD --permanent --add-service=https
      echo -e "${GREEN}[INFO]${NC} Port 443 has been opened."
    fi
  else
    echo -e "${GREEN}[INFO]${NC} Port 443 (HTTPS) is already open."
  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} Do you want to check your public IP from ifconfig.me? [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[@]}"

  echo -e "${YELLOW}[INFO]${NC} Caddy config: /etc/caddy/"
  echo -e "${YELLOW}[INFO]${NC} Firewalld config: /etc/firewalld/"
  echo -e "${YELLOW}[INFO]${NC} PHP config: /etc/php/, /etc/php-fpm.d/ (depending on distro)"

  echo -e "\n${GREEN}[DONE] ✅${NC} Installation complete."

# ========== UNINSTALL ==========
elif [[ "$ACTION" == "2" ]]; then
  echo -e "\n${BOLD}${YELLOW}=== Uninstallation ===${NC}\n"

  run_step "Stopping caddy" sudo systemctl stop caddy || true
  run_step "Disabling caddy" sudo systemctl disable caddy || true

  echo -ne "${YELLOW}[INFO]${NC} "
  read -p "Do you want to completely uninstall caddy (including config)? [y/n]: " purge_caddy
  if [[ "$purge_caddy" =~ ^[Yy]$ ]]; then
    if [[ "$DISTRO" == "debian" || "$DISTRO" == "ubuntu" ]]; then
      run_multistep "Purging caddy" bash -c "sudo apt remove --purge -y caddy"
    else
      run_multistep "Removing caddy" bash -c "$PKG_REMOVE $CADDY_PKG"
      echo -e "${YELLOW}[WARNING]${NC} Full purge not supported on this distribution."
    fi
  else
    echo -ne "${YELLOW}[INFO]${NC} "
    read -p "Do you want to remove caddy but keep config? [y/n]: " remove_caddy_only
    if [[ "$remove_caddy_only" =~ ^[Yy]$ ]]; then
      run_multistep "Removing caddy" bash -c "$PKG_REMOVE $CADDY_PKG"
    else
      echo -e "${YELLOW}[INFO]${NC} Skipping caddy removal."
    fi
  fi

  # Now ask about closing ports 80 and 443 in firewalld
  echo -ne "${YELLOW}[INFO]${NC} "
  read -p "Do you want to close ports 80 (http) and 443 (https) in firewalld? [y/n]: " close_ports
  if [[ "$close_ports" =~ ^[Yy]$ ]]; then
    run_step "Closing port 80 (HTTP)" sudo $FIREWALLD_CMD --permanent --remove-service=http
    run_step "Closing port 443 (HTTPS)" sudo $FIREWALLD_CMD --permanent --remove-service=https
    run_step "Reloading firewalld" sudo $FIREWALLD_CMD --reload
  fi

  echo -ne "${YELLOW}[INFO]${NC} "
  read -p "Do you want to 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

  echo -ne "${YELLOW}[INFO]${NC} "
  read -p "Do you want to remove PHP and extensions? [y/n]: " remove_php
  if [[ "$remove_php" =~ ^[Yy]$ ]]; then
    run_multistep "Removing PHP" bash -c "$PKG_REMOVE $PHP_PACKAGES"

    echo -ne "${YELLOW}[INFO]${NC} "
    read -p "Do you want to delete PHP configuration directories (e.g., /etc/php, /etc/php-fpm.d)? [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
      echo -e "${GREEN}[INFO]${NC} PHP configuration directories removed."
    else
      echo -e "${YELLOW}[INFO]${NC} PHP configuration directories were preserved."
    fi
  fi

  run_multistep "Cleaning unused packages" bash -c "$PKG_CLEAN"

  echo -e "\n${GREEN}[DONE] ✅${NC} Uninstallation complete."

else
  echo -e "${RED}[ERROR] Invalid option selected.${NC}"
  exit 1
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/CADDY/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