#!/usr/bin/env bash # Author : Allan Christensen # First Created : 11012021 (DD-MM-YYYY) # Description : Installs Zabbix on Ubuntu 24.04 # License : MIT License (see LICENSE file for details) # # Are we root # if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit 1 ; fi # # Get php-fpm version # phpfpm=$(systemctl list-unit-files --type=service | awk '/php[0-9]+\.[0-9]+-fpm\.service/ {sub(".service","",$1); print $1; exit}') if [[ -z "$phpfpm" ]]; then printf "\nUnable to detect php-fpm version. Is PHP-FPM installed?\n\n" ; exit 1 ; fi # # Check if required services are running or not # for svc in nginx mariadb "$phpfpm"; do systemctl is-active --quiet "$svc" || { printf "\n%s is not running, cannot continue...\n\n" "${svc^}" ; exit 1 ; }; done # # Check MariaDB authentication method (socket or not) # if mysql -u root -e ";" 2>/dev/null; then socket="SOCKET DETECTED — no need for -a or -m" socketusage="SOCKET DETECTED — this flag is not needed" socketauth="yes" else socket="NO SOCKET DETECTED — you must use -a and -m" socketusage="NO SOCKET DETECTED — these flags are required" socketauth="no" fi # # Define variables and functions # ubuntuversion="24.04" fallbackversion="7.0" # # Function usage # usage() { printf -- "\nzabbixinstall (Ubuntu 24.04 + Zabbix 7.0 LTS)\n\n" if [[ "$socketauth" == "yes" ]]; then printf -- "SOCKET DETECTED — no need for -a or -m\n\n" printf -- "Installs Zabbix Server using MariaDB socket authentication.\n\n" printf -- "Usage:\n" printf -- " sudo ./zabbixinstall -n -p \n\n" printf -- "Example:\n" printf -- " sudo ./zabbixinstall -n zabbix.something.xyz -p zabbixdbpwd\n\n" else printf -- "NO SOCKET DETECTED — you must use -a and -m\n\n" printf -- "Installs Zabbix Server using MariaDB admin credentials.\n\n" printf -- "Usage:\n" printf -- " sudo ./zabbixinstall -n -p -m [-a ]\n\n" printf -- "Examples:\n" printf -- " sudo ./zabbixinstall -n zabbix.something.xyz -p zabbixdbpwd -m rootpwd\n" printf -- " sudo ./zabbixinstall -n zabbix.something.xyz -p zabbixdbpwd -a admin -m adminpwd\n\n" fi printf -- "Options:\n" printf -- " -h | -help | --help Show this help screen\n\n" } # # Let's go # clear # # Configure command line options # if [[ "$1" == "-help" || "$1" == "--help" ]]; then usage ; exit 0 ; fi if [[ $# -eq 0 || ! $1 =~ ^- ]]; then usage ; exit 1 ; fi while getopts ":n:p:m:a:h" option; do case "$option" in n) hostname=$(echo "$OPTARG" | tr '[:upper:]' '[:lower:]');; p) dbpass="$OPTARG";; m) mariadbpwd="$OPTARG";; a) mariadbadmin="$OPTARG";; h) usage; exit 0;; :) usage; echo ""; echo "Error! Option -$OPTARG requires an argument."; echo ""; exit 1;; \?) usage; echo ""; echo "Error! Invalid option: -$OPTARG"; echo ""; exit 1;; esac done # # Parse and validate input # if [[ -z "$hostname" || -z "$dbpass" ]]; then usage printf "\nERROR: Both -n (domain) and -p (database password) are required.\n\n" exit 1 fi # Convert to lowercase (domains are case-insensitive) hostname=$(echo "$hostname" | tr '[:upper:]' '[:lower:]') # Disallow leading hyphen if [[ "$hostname" =~ ^- ]]; then printf "\nERROR: Domain cannot start with a hyphen.\n" printf "Example of valid input: zabbix.example.com\n\n" exit 1 fi # Disallow spaces, slashes, underscores if [[ "$hostname" =~ [[:space:]/_] ]]; then printf "\nERROR: Domain cannot contain spaces, slashes, or underscores.\n\n" exit 1 fi # Validate domain format (RFC 1123) if [[ ! "$hostname" =~ ^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$ ]]; then printf "\nERROR: Invalid domain format.\n" printf "Example of valid input: zabbix.example.com\n\n" exit 1 fi # Check for spaces in DB credentials if [[ "$dbpass" =~ [[:space:]] ]]; then printf "\nERROR: Database password cannot contain spaces.\n\n" exit 1 fi # # Try to determine the latest Zabbix version # zabbix_version=$(curl -s https://repo.zabbix.com/zabbix/ | grep -oP '(?<=href=")[0-9]+\.[0]' | sort -V | tail -1) # Use fallback version if fetch failed if [[ -z "$zabbix_version" ]]; then printf "Could not determine latest Zabbix version. Falling back to version %s\n\n" "$fallbackversion" ; zabbix_version="$fallbackversion" ; fi printf "Using Zabbix version: %s\n\n" "$zabbix_version" # # Ensure curl and wget are installed # for tool in curl wget; do dpkg -s "$tool" &>/dev/null || apt install -y -qq "$tool" ; done # # Download and install the Zabbix repository # tmpfile="/tmp/zabbix-release_latest_${zabbix_version}+ubuntu${ubuntuversion}_all.deb" download="https://repo.zabbix.com/zabbix/${zabbix_version}/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_${zabbix_version}+ubuntu${ubuntuversion}_all.deb" wget --no-verbose -O "$tmpfile" "$download" dpkg -i "$tmpfile" apt update # # Install Zabbix components # apt install -y zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent fping # # Clone nginx-snippets; if nginx-snippets exists then just pull latest changes # nginxsnippets="/etc/nginx/nginx-snippets" repo="https://git.x-files.dk/webserver/nginx-snippets.git" if [[ -d "$nginxsnippets/.git" ]]; then git -C "$nginxsnippets" pull --quiet; else git clone --quiet "$repo" "$nginxsnippets"; fi # # Determine MariaDB login method # mariadbadmin="${mariadbadmin:-root}" printf "\nChecking MariaDB access method...\n" if [[ "$socketauth" == "yes" ]]; then dbmethod="socket" printf "Socket authentication detected (root)\n" elif [[ -n "$mariadbpwd" && -n "$mariadbadmin" ]]; then dbmethod="admin" printf "Using admin user authentication (%s)\n" "$mariadbadmin" else printf "\nERROR: No valid MariaDB authentication method found.\n" printf "Tried socket, admin user, and root password.\n\n" exit 1 fi # # Create Zabbix database # case "$dbmethod" in socket) mysql -u root <