一 环境准备
linux+nginx+postgresql+php
[root@test-zabbix-switch src]# hostnamectl
Static hostname: test-zabbix-switch
Icon name: computer-vm
Chassis: vm
Machine ID: 517f0e9e0b314922ab417b4a3662a9a1
Boot ID: e4356089e84142e6b7257e499819a48e
Virtualization: kvm
Operating System: Kylin Linux Advanced Server V10 (Sword)
Kernel: Linux 4.19.90-25.38.v2101.ky10.x86_64
Architecture: x86-64
1.1 下载安装包
wget https://nginx.org/download/nginx-1.24.0.tar.gz
wget https://www.php.net/distributions/php-8.3.3.tar.gz
wget https://ftp.postgresql.org/pub/source/v16.2/postgresql-16.2.tar.gz
wget https://codeload.github.com/timescale/timescaledb/tar.gz/refs/tags/2.13.1
wget https://cdn.zabbix.com/zabbix/sources/stable/7.0/zabbix-7.0.1.tar.gz
1.2安装编译软件包
yum install -y unzip pcre-devel openssl-devel gcc gcc-c++ perl readline readline-devel openssl openssl-devel zlib zlib-devel ncurses-devel perl-ExtUtils-Embed python python-devel libxslt* python3-devel cmake
2 创建用户
[root@test-zabbix-switch src]# groupadd zabbix
[root@test-zabbix-switch src]# useradd -g zabbix zabbix
二 编译安装
1 nginx安装
1.1 nginx编译安装
[root@test-zabbix-switch src]# tar -zxvf nginx-1.24.0.tar.gz -C /usr/local/
[root@test-zabbix-switch src]# cd ../nginx-1.24.0/
[root@test-zabbix-switch nginx-1.24.0]# ./configure --user=zabbix --group=zabbix --prefix=/zabbix/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre
[root@test-zabbix-switch nginx-1.24.0]# make -j 8 && make install
1.2 nginx配置文件
[root@test-zabbix-switch nginx-1.24.0]# mv /zabbix/nginx/conf/nginx.conf /zabbix/nginx/conf/nginx.conf.bak
[root@test-zabbix-switch nginx-1.24.0]# vim /zabbix/nginx/conf/nginx.conf
user zabbix zabbix;
worker_processes auto;
error_log logs/error.log crit;
error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
测试nginx配置格式
chown itops: -R /zabbix
/zabbix/nginx/sbin/nginx -t
1.3 nginx服务services启动文件
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/zabbix/nginx/sbin/nginx -c /zabbix/nginx/conf/nginx.conf
ExecReload=/zabbix/nginx/sbin/nginx -s reload
ExecStop=/zabbix/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
1.4 启动服务
[root@test-zabbix-switch ~]# systemctl start nginx
[root@test-zabbix-switch ~]# systemctl status nginx
[root@test-zabbix-switch ~]# systemctl enable nginx
1.5 web测试访问服务
提醒:关闭防火墙,或者放行80端口
2 数据库安装
选择postgresql 版本16.2
选择timescaledb 版本2.13.1
2.1 yum安装工具及环境依赖
yum install -y unzip gcc gcc-c++ perl readline readline-devel openssl openssl-devel zlib zlib-devel ncurses-devel perl-ExtUtils-Embed python python-devel libxslt* python3-devel
yum install cmake -y
# cmake版本需大于3.4版本,如yum方式安装cmake版本低,则需要用手动编译方式进行替换
cmake --version
2.2PG编译安装
[root@test-zabbix-switch src]# tar -zxvf postgresql-16.2.tar.gz -C /usr/local/
[root@test-zabbix-switch src]# cd ../postgresql-16.2/
[root@test-zabbix-switch src]# ./configure --prefix=/zabbix/postgresql --with-pgport=5432 --with-segsize=16 --with-blocksize=32 --with-wal-blocksize=64 --with-libedit-preferred --with-perl --with-openssl --with-libxml --with-python --with-libxslt --enable-thread-safety --enable-nls=en_US.UTF-8 --without-icu
[root@test-zabbix-switch src]# make -j 8 && make install
2.3初始化
mkdir /data
chown zabbix: /data
su - zabbix
/zabbix/postgresql/bin/initdb -D /data/postgresql -E utf8
# 启动
/zabbix/postgresql/bin/pg_ctl -D /data/postgresql/ start
/zabbix/postgresql/bin/pg_ctl -D /data/postgresql -l logfile start
使用root用户添加环境变量
# 设定全局变量
vim /etc/profile
#i # 尾行追加如下部分
PATH=/zabbix/postgresql/bin:/usr/bin:/usr/sbin:/bin:/sbin/bin
export PATH
PG_CONFIG=/zabbix/postgresql/bin/pg_config
export PG_CONFIG
PGDATA=/data/postgresql
export PGDATA
LD_LIBRARY_PATH=/zabbix/postgresql/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
# 执行如下命令使变量生效
source /etc/profile
2.4 TS时序库编译(可选)
2.4.1编译安装
[root@test-zabbix-switch build]# tar xf timescaledb-2.13.1.tar.gz
[root@test-zabbix-switch build]# cd timescaledb-2.13.1/
[root@test-zabbix-switch build]# echo y | ./bootstrap -DREGRESS_CHECKS=OFF
[root@test-zabbix-switch build]# cd ./build && make
[root@test-zabbix-switch build]# make install
# PG配置载入ts模块
[root@test-zabbix-switch build]# echo "shared_preload_libraries = 'timescaledb'" >> /data/postgresql/postgresql.conf
# 重启pg令模块生效
[root@test-zabbix-switch build]# su - zabbix -c 'pg_ctl restart'
2.5pg服务service文件编写
vim /usr/lib/systemd/system/postgresql.service
#i# 输入
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=zabbix
Group=zabbix
Environment=PGPORT=5432
Environment=PGDATA=/data/postgresql
OOMScoreAdjust=-1000
ExecStart=/zabbix/postgresql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/zabbix/postgresql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/zabbix/postgresql/bin/pg_ctl reload -D ${PGDATA} -s
TimeoutSec=300
[Install]
WantedBy=multi-user.target
2.6 启动服务及开机自启动
[root@test-zabbix-switch build]# su - zabbix -c 'pg_ctl stop'
[root@test-zabbix-switch build]# systemctl enable postgresql --now
[root@test-zabbix-switch build]# systemctl status postgresql.service
3 php编译安装
使用php版本 8.3.3
3.1 yum 安装工具及环境依赖
[root@test-zabbix-switch build]# yum -y install oniguruma oniguruma-devel libxml2-devel bzip2-devel libcurl-devel libpng-devel libjpeg-devel freetype-devel gmp-devel openldap-devel readline-devel libxslt-devel net-snmp-devel
[root@test-zabbix-switch build]# cp -frp /usr/lib64/libldap* /usr/lib/
[root@test-zabbix-switch build]# yum install oniguruma-* -y
3.2 编译安装
[root@test-zabbix-switch src]# tar -zxf php-8.3.3.tar.gz -C /usr/local/
[root@test-zabbix-switch src]# cd ../php-8.3.3/
[root@test-zabbix-switch php-8.3.3]# ./configure --prefix=/zabbix/php --with-config-file-path=/zabbix/php/etc --with-pgsql=/zabbix/postgresql --with-pdo-pgsql=/zabbix/postgresql --enable-gd --enable-bcmath --with-jpeg --with-freetype --enable-ctype --enable-xml --enable-session --enable-sockets --enable-mbstring --with-gettext --with-ldap --with-openssl --without-pdo-sqlite --without-sqlite3 --enable-fpm
[root@test-zabbix-switch php-8.3.3]# sed -i "s@-lcrypto@-lcrypto -llber@g" Makefile
[root@test-zabbix-switch php-8.3.3]# make -j 8 && make install
3.3 配置php相关参数
[root@test-zabbix-switch php-8.3.3]# cp php.ini-production /zabbix/php/etc/php.ini
[root@test-zabbix-switch php-8.3.3]# ln -s /zabbix/php/etc/php.ini /etc/php.ini
[root@test-zabbix-switch php-8.3.3]# cp /zabbix/php/etc/php-fpm.conf.default /zabbix/php/etc/php-fpm.conf
[root@test-zabbix-switch php-8.3.3]# cp /zabbix/php/etc/php-fpm.d/www.conf.default /zabbix/php/etc/php-fpm.d/www.conf
[root@test-zabbix-switch php-8.3.3]# sed -i "s@user = nobody@user = zabbix@g" /zabbix/php/etc/php-fpm.d/www.conf
[root@test-zabbix-switch php-8.3.3]# sed -i "s@group = nobody@group = zabbix@g" /zabbix/php/etc/php-fpm.d/www.conf
[root@test-zabbix-switch php-8.3.3]# sed -i "s@pm.max_children = 5@pm.max_children = 30@g" /zabbix/php/etc/php-fpm.d/www.conf
[root@test-zabbix-switch php-8.3.3]# sed -i "s@;pid = run/php-fpm.pid@pid = run/php-fpm.pid@g" /zabbix/php/etc/php-fpm.d/www.conf
[root@test-zabbix-switch php-8.3.3]# sed -i "s@post_max_size = 8M@post_max_size = 16M@g" /zabbix/php/etc/php.ini
[root@test-zabbix-switch php-8.3.3]# sed -i "s@max_execution_time = 30@max_execution_time = 300@g" /zabbix/php/etc/php.ini
[root@test-zabbix-switch php-8.3.3]# sed -i "s@max_input_time = 60@max_input_time = 300@g" /zabbix/php/etc/php.ini
# 生成php-fpm启动文件
[root@test-zabbix-switch php-8.3.3]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@test-zabbix-switch php-8.3.3]# chmod +x /etc/init.d/php-fpm
[root@test-zabbix-switch php-8.3.3]# chown -R zabbix: /zabbix/php
[root@test-zabbix-switch php-8.3.3]# systemctl enable php-fpm --now #生成service文件
[root@test-zabbix-switch php-8.3.3]# cat /usr/local/php-8.3.3/sapi/fpm/php-fpm.service >> /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=The PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
PIDFile=/zabbix/php/var/run/php-fpm.pid
ExecStart=/zabbix/php/sbin/php-fpm --nodaemonize --fpm-config /zabbix/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
[root@test-zabbix-switch php-8.3.3]# systemctl daemon-reload
[root@test-zabbix-switch php-8.3.3]# systemctl start php-fpm.service
[root@test-zabbix-switch php-8.3.3]# systemctl status php-fpm.service
[root@test-zabbix-switch logs]# systemctl enable php-fpm.service
4 zabbix安装及启动
4.1 yum 安装编译工具及依赖
选择zabbix版本zabbix-7.0.1
yum -y install libssh2 libssh2-devel OpenIPMI-devel libevent-devel unixODBC unixODBC-devel java-1.8.0-openjdk-devel openssl-devel
4.2 zabbix编译及配置参数定义
[root@test-zabbix-switch src]# tar -zxf zabbix-7.0.1.tar.gz -C /usr/local/
[root@test-zabbix-switch src]# cd ../zabbix-7.0.1/
[root@test-zabbix-switch zabbix-7.0.1]# ./configure --prefix=/zabbix/zabbix --enable-server --enable-agent --with-postgresql=/zabbix/postgresql/bin/pg_config --with-net-snmp --with-libcurl --with-libxml2 --with-unixodbc --with-openipmi --enable-ipv6 --enable-java --with-openssl --with-ssh2 --with-iconv --with-iconv-include --with-iconv-lib --with-libpcre --with-libevent --with-zlib --with-zlib-include --with-zlib-lib --with-libpthread --with-ldap
[root@test-zabbix-switch zabbix-7.0.1]# make -j 8 && make install
"""
注意:不进行一下步骤,直接配置service文件即可
[root@test-zabbix-switch zabbix-7.0.1]# cp misc/init.d/fedora/core/zabbix_* /etc/init.d/
[root@test-zabbix-switch zabbix-7.0.1]# ll -d /etc/init.d/zabbix_*
[root@test-zabbix-switch zabbix-7.0.1]# chmod +x /etc/init.d/zabbix_*
[root@test-zabbix-switch zabbix-7.0.1]# chown -R zabbix: /zabbix
"""
修改配置文件:
[root@test-zabbix-switch zabbix-7.0.1]# grep -Ev "^#|^$" /zabbix/zabbix/etc/zabbix_server.conf
[root@test-zabbix-switch postgresql]# grep -Ev "^#|^$" /zabbix/zabbix/etc/zabbix_server.conf
LogFile=/zabbix/zabbix/logs/zabbix_server.log
PidFile=/zabbix/zabbix/run/zabbix_server.pid
DBName=zabbix
DBUser=zabbix
DBPassword=XXXXXXXX
DBPort=5432
StartPollers=100
StartTrappers=10
StartPingers=100
StartDiscoverers=100
Timeout=4
LogSlowQueries=3000
Include=/zabbix/zabbix/etc/zabbix_server.conf.d/*.conf
StatsAllowedIP=127.0.0.1
EnableGlobalScripts=0
4.3 zabbix库创建
# 创建zabbix用户
su - zabbix -c 'createuser --pwprompt zabbix'
# 输入用户密码
# 创建zabbix库
su - zabbix -c 'createdb -O zabbix -E Unicode -T template0 zabbix'
4.4数据库表结构导入
# 进入编译包数据库路径下
[root@test-zabbix-switch zabbix-7.0.1]# cd database/postgresql/
# 导入表结构
[root@test-zabbix-switch postgresql]# cat schema.sql | /zabbix/postgresql/bin/psql -Uzabbix zabbix
[root@test-zabbix-switch postgresql]# cat images.sql | /zabbix/postgresql/bin/psql -Uzabbix zabbix
[root@test-zabbix-switch postgresql]# cat data.sql | /zabbix/postgresql/bin/psql -Uzabbix zabbix
[root@test-zabbix-switch postgresql]# /zabbix/postgresql/bin/psql -Uzabbix zabbix -c 'CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE'
# 关闭压缩,如果需要正常压缩,则跳过下方sed命令
[root@test-zabbix-switch postgresql]# sed -i 's#compression_status=1#compression_status=0#g' timescaledb/schema.sql
[root@test-zabbix-switch postgresql]# cat timescaledb/schema.sql | /zabbix/postgresql/bin/psql -Uzabbix zabbix
# 抛出 TimescaleDB is configured successfully 即视为超表创建成功,其余提示信息可忽略
4.5 zabbix 服务及agent程序启动
4.5.1启动zabbix_server
service启动文件编写:
vim /usr/lib/systemd/system/zabbix-server.service
[root@test-zabbix-switch ~]# cat /usr/lib/systemd/system/zabbix-server.service
[Unit]
Description=Zabbix Server
After=syslog.target
After=network.target
[Service]
Environment="CONFFILE=/zabbix/zabbix/etc/zabbix_server.conf"
EnvironmentFile=-/zabbix/zabbix
Type=forking
#PIDFile=/var/run/zabbix/zabbix_server.pid
PIDFile=/tmp/zabbix_server.pid
ExecStart=/zabbix/zabbix/sbin/zabbix_server -c /zabbix/zabbix/etc/zabbix_server.conf
ExecStop=/usr/bin/kill $MAINPID
Restart=always
RestartSec=5
User=zabbix
Group=zabbix
[Install]
WantedBy=multi-user.target
启动服务:systemctl daemon-reload && systemctl enable --now zabbix-server.service && systemctl status zabbix-server.service
4.5.2 zabbix agent启动
service启动文件
[root@test-zabbix-switch ~]# cat /usr/lib/systemd/system/zabbix-agent.service
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target
[Service]
Environment="CONFFILE=/zabbix/zabbix/etc/zabbix_agentd.conf"
EnvironmentFile=-/zabbix/zabbix
Type=forking
PIDFile=/tmp/zabbix_agentd.pid
#PIDFile=/var/run/zabbix/zabbix_agentd.pid
ExecStart=/zabbix/zabbix/sbin/zabbix_agentd -c /zabbix/zabbix/etc/zabbix_agentd.conf
ExecStop=/usr/bin/kill $MAINPID
Restart=always
RestartSec=5
User=zabbix
Group=zabbix
[Install]
WantedBy=multi-user.target
启动服务:
systemctl daemon-reload && systemctl enable --now zabbix-agent.services && systemctl tatus zabbix-agent.service
4.6 zabbix_web 配置及初始化
4.6.1 web部署
# 进入编译包路径下
[root@test-zabbix-switch zabbix-7.0.1]# cd /usr/local/zabbix-7.0.1/
# 拷贝ui代码至nginx
[root@test-zabbix-switch zabbix-7.0.1]# cp -r ui/ /zabbix/nginx/html/zabbix
[root@test-zabbix-switch zabbix-7.0.1]# chown zabbix: -R /zabbix/nginx/html/zabbix
4.6.2 web页面初始化配置
4.6.3 配置完成即部署完成。跳转登录页,默认账号密码为Admin/zabbix。
5 乱码解决
[root@test-zabbix-switch ~]# find / -name font* | grep zabbix
/zabbix/nginx/html/zabbix/assets/fonts
[root@test-zabbix-switch ~]# cd /zabbix/nginx/html/zabbix/assets/fonts
[root@test-zabbix-switch ~]# mv DejaVuSans.ttf DejaVuSans.ttf.bak
上传需要字体之后,更名为DejaVuSans.ttf,重启服务,刷新页面生效
6 报错信息
原因:zabbix启动ICMP监测时,需要fping组件,否则会有如下提示。
184853:20240725:103939.635 At least one of '/usr/sbin/fping', '/usr/sbin/fping6' must exist. Both are missing in the system.
解决:
1.安装fping
wget http://fping.org/dist/fping-4.2.tar.gz
tar -xvf fping-4.2.tar.gz
cd fping-4.2
./configure --prefix=/usr/local --enable-ipv4 --enable-ipv6
make
make install
安装完成后,fping目录为/usr/local/sbin
2.修改zabbix_server.conf配置文件
vim /usr/local/zabbix/etc/zabbix_server.conf
把FpingLocation路径修改为刚安装的fping路径。
FpingLocation=/usr/local/sbin/fping
如果不修改zabbix_server.conf配置件需要使用软连接到/usr/local/sbin/fping,zabbix默认fping的路径是/usr/sbin/fping
ln -s /usr/local/sbin/fping /usr/sbin/fping
3.修改fping权限(如果不设下面权限,zabbix服务端会采集不到数据)
chown root:zabbix /usr/local/sbin/fping
chmod 4710 /usr/local/sbin/fping
service zabbix_server restart
4.zabbix用户测试fping命令
/usr/local/sbin/fping www.baidu.com
www.baidu.com is alive # 说明命令返回成功。