Skip to content

Zabbix Server 7.0LTS Installation

1. OS Tuning

  • 1.1 Install zabbix repository (Optional)

Please use ansible to config server (basic)

2. Zabbix Server Deploy

  • 2.1 Create script for One-click deployment
vim zabbix-one-install.sh

Add below content

#!/bin/bash
# Zabbix 7.0 LTS + PostgreSQL 16 + Nginx 一键安装脚本 (Rocky Linux 9)
# 使用 su - postgres -c 避免 sudo 账户过期问题
# 数据目录: /pgdata/zabbix
# Usage: sh zabbix7-nginx-pg16-final.sh (run as root)

set -e

# ================== 配置 ==================
PGDATA_DIR="/pgdata/zabbix"
ZABBIX_DB_USER="zabbix"
ZABBIX_DB_PASS="zabbix"  # 生产环境请修改
TIMEZONE="Asia/Shanghai"
PG_BIN="/usr/pgsql-16/bin"

# ================== 工具函数 ==================
log() {
    echo -e "\033[1;32m[$(date '+%Y-%m-%d %H:%M:%S')] $1\033[0m"
}
error() {
    echo -e "\033[1;31m[ERROR] $1\033[0m" >&2
    exit 1
}

# ================== 权限检查 ==================
if [[ $EUID -ne 0 ]]; then
    error "必须以 root 用户运行!"
fi

if ! grep -q "Rocky Linux" /etc/os-release; then
    error "仅支持 Rocky Linux 9"
fi

log "开始部署 Zabbix 7.0 LTS + PostgreSQL 16 + Nginx..."

# ================== 1. 系统更新 ==================
log "更新系统..."
dnf update -y

# ================== 2. 安装 PostgreSQL 16 ==================
log "安装 PostgreSQL 16..."
#dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm (OS Tuning已配置)
dnf -qy module disable postgresql
dnf install -y postgresql16-server postgresql16

# ================== 3. 创建数据目录 ==================
log "创建 PostgreSQL 数据目录: $PGDATA_DIR"
mkdir -p "$PGDATA_DIR"
chown postgres:postgres "$PGDATA_DIR"
chmod 700 "$PGDATA_DIR"

# ================== 4. 初始化数据库(使用完整路径)==================
log "初始化 PostgreSQL 数据库到 $PGDATA_DIR"
su - postgres -c "$PG_BIN/initdb -D '$PGDATA_DIR' --locale=en_US.UTF-8 --encoding=UTF8"

# ================== 5. 配置 systemd 使用自定义 PGDATA ==================
log "配置Systemd..."
mkdir -p /etc/systemd/system/postgresql-16.service.d
cat > /etc/systemd/system/postgresql-16.service.d/pgdata.conf <<EOF
[Service]
Environment=PGDATA=$PGDATA_DIR
EOF
systemctl daemon-reload

# ================== 6. SELinux 配置 ==================
log "配置SELinux..."
if command -v getenforce &> /dev/null && [ "$(getenforce)" = "Enforcing" ]; then
    log "配置 SELinux 上下文..."
    dnf install -y policycoreutils-python-utils
    semanage fcontext -a -t postgresql_db_t "$PGDATA_DIR(/.*)?"
    restorecon -Rv "$PGDATA_DIR"
fi

# ================== 7. 启动 PostgreSQL ==================
log "启动PostgreSQL16..."
systemctl enable --now postgresql-16

# ================== 8. 配置 pg_hba.conf ==================
log "配置 pg_hba.conf 允许本地连接..."
PG_HBA="$PGDATA_DIR/pg_hba.conf"
cp "$PG_HBA"{,.bak}
# 允许本地 socket 无密码(用于初始化)
sed -i '/^local.*all.*all/ s|peer\|ident\|md5\|scram-sha-256|trust|' "$PG_HBA"
# IPv4/IPv6 使用 scram-sha-256
sed -i 's/^host.*all.*all.*127.0.0.1\/32.*/host all all 127.0.0.1\/32 scram-sha-256/' "$PG_HBA"
sed -i 's/^host.*all.*all.*::1\/128.*/host all all ::1\/128 scram-sha-256/' "$PG_HBA"

systemctl restart postgresql-16

# ================== 9. 创建 Zabbix 数据库和用户 ==================
log "创建 Zabbix 数据库和用户..."
su - postgres -c "$PG_BIN/psql -c \"CREATE USER $ZABBIX_DB_USER WITH PASSWORD '$ZABBIX_DB_PASS';\""
su - postgres -c "$PG_BIN/psql -c \"CREATE DATABASE zabbix OWNER $ZABBIX_DB_USER ENCODING 'UTF8' LC_COLLATE 'C' LC_CTYPE 'C' TEMPLATE template0;\""

# ================== 10. 安装 Zabbix 7.0 ==================
log "添加 Zabbix 7.0 仓库... (OS Tuning已配置)"
#rpm -Uvh https://repo.zabbix.com/zabbix/7.0/rhel/9/x86_64/zabbix-release-7.0-1.el9.noarch.rpm
dnf clean all

log "安装 Nginx、PHP 和 Zabbix 组件..."
dnf install -y nginx php php-fpm php-pgsql php-gd php-bcmath php-mbstring php-xml php-gettext php-sockets
dnf install -y zabbix-server-pgsql zabbix-web-pgsql zabbix-sql-scripts zabbix-agent zabbix-nginx-conf

# ================== 11. 导入 Zabbix Schema ==================
log "导入 Zabbix 数据库结构..."
zcat /usr/share/zabbix-sql-scripts/postgresql/server.sql.gz | su - postgres -c "$PG_BIN/psql -d zabbix"

# ================== 12. 配置 PHP ==================
log "配置PHP..."
sed -i "s|;date.timezone =|date.timezone = $TIMEZONE|" /etc/php.ini
systemctl enable --now php-fpm

# ================== 13. 配置 Nginx ==================
log "配置Nginx..."
if [ -f /etc/nginx/conf.d/zabbix.conf.disabled ]; then
    mv /etc/nginx/conf.d/zabbix.conf.disabled /etc/nginx/conf.d/zabbix.conf
fi
sed -i 's/listen .*/listen 80;/g' /etc/nginx/conf.d/zabbix.conf

# ================== 14. 配置 Zabbix Server ==================
log "配置Zabbix Server..."
ZABBIX_CONF="/etc/zabbix/zabbix_server.conf"
sed -i "s|# DBPassword=.*|DBPassword=$ZABBIX_DB_PASS|" "$ZABBIX_CONF"
sed -i "s|# DBUser=.*|DBUser=$ZABBIX_DB_USER|" "$ZABBIX_CONF"
sed -i "s|# DBName=.*|DBName=zabbix|" "$ZABBIX_CONF"
sed -i 's|^DBHost=localhost|#DBHost=localhost|' "$ZABBIX_CONF"

# ================== 15. 启动服务 ==================
log "启动 Zabbix 和 Web 服务..."
systemctl enable --now zabbix-server zabbix-agent nginx

# ================== 16. 防火墙 ==================
log "配置Firewall..."
if systemctl is-active firewalld &> /dev/null; then
    log "开放 HTTP 服务..."
    firewall-cmd --permanent --add-service=http
    firewall-cmd --reload
fi

# ================== 17. 完成提示 ==================
IP=$(hostname -I | awk '{print $1}')
log "✅ 部署成功!"
echo
echo "🌐 访问 Zabbix: http://$IP/zabbix"
echo "   DB 用户: $ZABBIX_DB_USER"
echo "   DB 密码: $ZABBIX_DB_PASS"
echo "   数据目录: $PGDATA_DIR"
echo
echo "💡 首次访问需完成 Web 安装向导(数据库类型选 PostgreSQL)"
echo "🔒 建议:安装后将 pg_hba.conf 中 local 行改为 scram-sha-256 并重启 PostgreSQL"
  • 2.2 Deploy zabbix 7 LTS
chmod +x zabbix-one-install.sh
sh zabbix-one-install.sh

After script execute finish, you can see ther successful notic

image-20251221214108227

3. Zabbix config

  • Login with web url

http://zabbix-server-ip

Select Default language, and click Next step

image-20251221214404545

Confirm all requisites are OK, then click Next step

image-20251221214434924

Config zabbix db info, and click Next step

image-20251222173452323

image-20251222173537384

image-20251222174511039