commit f89936a0fb4373efa90e7662bebf7da50bdce6d2 Author: allan Date: Thu Oct 23 15:57:04 2025 +0200 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3eaaa81 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Allan Christensen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e161c03 --- /dev/null +++ b/README.md @@ -0,0 +1,180 @@ +# Zabbix for Ubuntu 24.04 Server +[![OS](https://img.shields.io/badge/ubuntu-24.04-E95420)](#) +[![Shell](https://img.shields.io/badge/shell-bash-121011)](#) +[![WebServer](https://img.shields.io/badge/server-nginx-009639)](#) +[![PHP](https://img.shields.io/badge/php-fpm-777BB4)](#) +[![DB](https://img.shields.io/badge/server-mariadb-003545)](#) +[![App](https://img.shields.io/badge/app-zabbix-DC382D)](#) +[![License](https://img.shields.io/badge/License-MIT-green)](./LICENSE) + +Installs and configures the **latest available Zabbix LTS release** on Ubuntu 24.04 Server. + +This is a **production-focused installer**, not a demo. + +It assumes Nginx, PHP-FPM, and MariaDB are already installed and running. + +--- + +## Related Installers + +If you don’t have the required components, you can use these compatible installers: + +- [Nginx + PHP-FPM Installer](https://git.x-files.dk/webserver/nginx-ubuntu) +- [MariaDB Installer](https://git.x-files.dk/database/mariadb-ubuntu) + +--- + +### Download the Script + +Clone this repository to your server: + +``` +git clone https://git.x-files.dk/network/zabbix-ubuntu.git +``` + +``` +cd zabbix-ubuntu +``` + +--- + +### Usage + +Run the script using: + +``` +sudo ./zabbixinstall -n -p [options] +``` + +### Examples + +The installer automatically detects if socket authentication is active. Only include `-a` and `-m` if your MariaDB setup does **not** use socket authentication. + +``` +sudo ./zabbixinstall -n zabbix.example.com -p zabbixdbpwd +sudo ./zabbixinstall -n zabbix.example.com -p zabbixdbpwd -m rootpwd +sudo ./zabbixinstall -n zabbix.example.com -p zabbixdbpwd -a admin -m adminpwd +``` + +### Options + +| Flag | Description | +|------|--------------| +| `-p ` | Zabbix database user password | +| `-a ` | Optional MariaDB admin username (defaults to `root` if not specified) | +| `-m ` | MariaDB root or admin password — *required only if socket authentication is disabled* | +| `-h`, `--help` | Show the help screen (reflects detected socket status) | + +### Configuration + +Once the installer completes, visit: + +``` +http:// +``` + +And complete the setup. + +--- + +### Features + +- Installs and configures Zabbix LTS automatically +- Creates the MariaDB database and user with the correct privileges +- Enables and starts Zabbix server and agent services +- Detects MariaDB socket or password authentication automatically +- Generates an Nginx host configuration dynamically +- Integrates cleanly with PHP-FPM and Nginx setups +- Supports re-runs via `IF NOT EXISTS` logic for safe idempotency + +--- + +### Configuration Details + +#### File Locations + +| File | Description | +|------|--------------| +| `/etc/zabbix/zabbix_server.conf` | Main Zabbix server configuration file | +| `/usr/share/zabbix-sql-scripts/mysql/server.sql.gz` | Database schema file imported by installer | +| `/etc/nginx/conf.d/.conf` | Generated Nginx host configuration file | +| `/etc/nginx/nginx-snippets` | Shared include snippets and templates | + +--- + +#### Behavior + +- The installer checks if **MariaDB socket authentication** is active. + If available, it connects locally as `root` (no password). +- If socket authentication is **not detected**, use `-a` and `-m` to supply credentials. +- Automatically sets and resets `log_bin_trust_function_creators` during import. +- Nginx configuration is validated (`nginx -t`) before restarting. + +--- + +### Nginx Integration + +The generated Zabbix configuration file listens on port 80. +To enable HTTPS (port 443), use the example provided [here](https://git.x-files.dk/webserver/nginx-snippets/src/branch/main/hostfiles/zabbix.443.conf). + +> **NOTE** +> The file also includes optional caching directives (commented out by default). +> You can enable them to improve load times and performance if needed. +> The lines you are looking for are at the bottom of the generated config file and look like this: +> ``` +> ##### Cache js css static content and open files start ##################### +> # include /etc/nginx/nginx-snippets/cache-open-files.conf; +> # include /etc/nginx/nginx-snippets/cache-statics.conf; +> # include /etc/nginx/nginx-snippets/cache-js-css.conf; +> ##### Cache js css static content and open files stop ###################### + +--- + +### Troubleshooting + +**Nginx fails to restart** +Run `nginx -t` and review any syntax errors reported in `/etc/nginx/conf.d/.conf`. + +**Access denied during database creation** +Your MariaDB setup likely does not use socket authentication. +Re-run the installer with the `-m` flag (and optionally `-a`) to provide credentials. +The installer will automatically re-detect your configuration. + +**PHP version mismatch** +The script automatically detects your installed PHP version, +but ensure that the PHP-FPM service (e.g., `php8.3-fpm`) is active. + +**Schema import fails** +Verify that `/usr/share/zabbix-sql-scripts/mysql/server.sql.gz` exists. +If missing, reinstall the `zabbix-sql-scripts` package. + +--- + +### FAQ + +**Q:** Why doesn’t the script ask for a MariaDB password by default? +**A:** Ubuntu 24.04’s MariaDB defaults to *socket authentication*, +allowing the local `root` user to connect without a password. +The installer detects this automatically and skips password prompts when applicable. + +**Q:** Can I run the installer multiple times? +**A:** Yes. Database and user creation use `IF NOT EXISTS`, making re-runs safe. +Existing Nginx configuration files are replaced, but no data is deleted. + +**Q:** Does this modify existing Nginx settings? +**A:** No. It adds a standalone host file in `/etc/nginx/conf.d/` +and validates configuration changes before applying them. + +--- + +### More Information + +More guides and documentation can be found on [wiki.x-files.dk](https://wiki.x-files.dk) + +--- + +### License + +Licensed under the [MIT License](./LICENSE). + +--- diff --git a/zabbixinstall b/zabbixinstall new file mode 100755 index 0000000..75e5417 --- /dev/null +++ b/zabbixinstall @@ -0,0 +1,248 @@ +#!/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 + +# +# Define variables and functions +# +phpver=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;' 2>/dev/null || echo "8.3") ; phpfpm="php$phpver-fpm" +ubuntuversion="24.04" +fallbackversion="7.0" + +# +# Function usage +# +usage () { + printf -- "\nzabbixinstall (Ubuntu 24.04 + Zabbix 7.0 LTS)\n\n" + printf -- "%s\n\n" "$socket" + printf -- "Usage:\n" + printf -- "sudo ./zabbixinstall -n -p [options]\n\n" + printf -- "Options:\n" + printf -- " -a Optional admin username $socketusage\n" + printf -- " -m Optional admin password $socketusage\n" + printf -- " -h | -help | --help Show this help screen\n\n" + printf -- "Examples:\n" + printf -- " sudo ./zabbixinstall -n zabbix.something.xyz -p zabbixdbpwd\n" + printf -- " sudo ./zabbixinstall -n zabbix.something.xyz -p zabbixdbpwd -a admin -m adminpwd\n\n" +} + +# +# Function to check if a service is already running or not +# +serviceno () { printf "\n%s" "$service" ; printf " is not running cannot continue...\n\n"; } +servicedead () { status=$(systemctl is-active "$service"); if [[ "$status" != "active" ]]; then serviceno ; exit 1 ; fi; } + +# +# Function inputcheck +# +inputcheck () { + [[ -z "$hostname" ]] && { usage; echo ""; echo "Error! Hostname empty."; echo ""; exit 1; } + [[ -z "$dbpass" ]] && { usage; echo ""; echo "Error! Database password empty."; echo ""; exit 1; } +} + +# +# If Nginx and MariaDB is not running then die +# +service="mariadb" ; servicedead ; service="nginx" ; servicedead + +# +# 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 + +# +# Let's go +# +clear + +# +# 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" + +# +# 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 +inputcheck "$hostname" "$dbpass" + +# +# 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 <