Skip to content

Zabbix 部署

环境准备

主机名IP配置磁盘操作系统预装软件
zabbix-server192.168.148.1052C4G20GopenEuler-24.03(LTS)LNMP、zabbix_server、zabbix-agent2
zabbix-agent192.168.148.1061C2G20GopenEuler-24.03(LTS)zabbix-agent2

Zabbix Server部署

安装Nginx

安装服务

bash
dnf -y install nginx

# 创建程序用户
useradd -s /sbin/nologin -M www
chown -R www:www /var/lib/nginx

nginx.conf

nginx
cat > /etc/nginx/nginx.conf << "EOF"
worker_processes auto;
user www;
events {
  worker_connections 20240;
}
http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;
  server {
    listen 80;
    server_name localhost;
    location / {
      root html;
      index index.php index.html index.htm;
    }
    location ~ \.(php|php5)?$ {
      root html;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
      include fastcgi_params;
    }
  }
}
EOF

启动服务

bash
systemctl enable nginx --now

安装MySQL

bash
dnf -y install mysql-devel libevent-devel pcre-devel
dnf -y install mysql-server

systemctl enable mysqld --now

安装PHP

安装基础依赖

bash
dnf -y install gcc gcc-c++ cmake libxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo  libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel libjpeg libjpeg-devel curl curl-devel openssl openssl-devel sqlite-devel libwebp libwebp-devel oniguruma-devel oniguruma

安装libzip依赖

bash
# 卸载旧版
dnf -y remove libzip

# 下载源码包
wget --no-check-certificate  https://libzip.org/download/libzip-1.3.2.tar.gz
# 解压
tar xf libzip-1.3.2.tar.gz -C /usr/src
# 编译安装
cd /usr/src/libzip-1.3.2/
./configure && make -j$(nproc) && make install
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
ldconfig

安装freetype依赖

bash
# 下载源码包
wget --no-check-certificate https://download.savannah.gnu.org/releases/freetype/freetype-2.10.4.tar.gz
# 解压
tar xf freetype-2.10.4.tar.gz -C /usr/src
# 编译安装
cd /usr/src/freetype-2.10.4
./configure --prefix=/usr/local/freetype
make -j$(nproc) && make install

编译libiconv依赖

bash
# 安装libiconv依赖
dnf install libjpeg libjpeg-devel libxslt-devel  libxml2 libxml2-devel openssl-devel openssl-perl curl curl-devel libpng libpng-devel freetype freetype-devel libicu-devel -y

# 下载源码包
wget --no-check-certificate https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.17.tar.gz
# 解压
tar xf libiconv-1.17.tar.gz -C /usr/src
# 编译安装
cd /usr/src/libiconv-1.17
./configure --prefix=/usr/local/libiconv
make -j$(nproc) && make install

编译PHP

下载源码包
bash
wget --no-check-certificate https://www.php.net/distributions/php-8.1.18.tar.gz
tar xf php-8.1.18.tar.gz -C /usr/src
编译安装
bash
cd /usr/src/php-8.1.18
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv \
--with-freetype=/usr/local/freetype \
--with-jpeg \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-pear \
--with-webp 

# 编译安装
make -j$(nproc) && make install
修改php.ini配置
bash
# 创软连接
ln -s /usr/local/php/bin/* /usr/bin/
ln -s /usr/local/php/sbin/* /usr/bin/

# 复制模板配置文件
cp /usr/local/php/etc/php.ini-production /usr/local/php/etc/php.ini
mkdir -p /usr/local/php/{etc,conf.d}

# 修改php配置优化项
sed -i 's/post_max_size =.*/post_max_size = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/upload_max_filesize =.*/upload_max_filesize = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/;date.timezone =.*/date.timezone = PRC/g' /usr/local/php/etc/php.ini
sed -i 's/short_open_tag =.*/short_open_tag = On/g' /usr/local/php/etc/php.ini
sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
sed -i 's/max_execution_time =.*/max_execution_time = 300/g' /usr/local/php/etc/php.ini
sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini

# 检测配置文件
pear config-set php_ini /usr/local/php/etc/php.ini
pecl config-set php_ini /usr/local/php/etc/php.ini
修改PHP-FPM配置
bash
# 复制模板文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf


vim /usr/local/php/etc/php-fpm.d/www.conf
[www]
;同主机尽量使用同用户,不能使用root用户
user = www
group = www
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 30
listen.owner = www
listen.group = www
listen.mode = 0666
pm.max_requests = 1024
pm.process_idle_timeout = 10s
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
启动php-fpm
bash
php-fpm
系统服务文件
bash
cat > /usr/lib/systemd/system/php-fpm.service << EOF
[Unit]
Description=PHP-FPM FastCGI Process
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID

[Install]
WantedBy=multi-user.target
EOF

# 重新加载systemd配置
sudo systemctl daemon-reload

# 启动服务(匹配图中的php-fpm进程)
sudo systemctl start php-fpm

# 设置开机自启
sudo systemctl enable php-fpm

# 检查状态(确认进程数匹配pm.max_children)
sudo systemctl status php-fpm -l

# 查看与Nginx的连接(匹配图中的fastcgi_pass)
ss -xlp | grep php-fpm

安装zabbix

创建zabbix程序用户

bash
useradd -Ms /sbin/nologin zabbix

创建初始数据库和数据库用户

sql
-- 创建数据库
create database zabbix character set utf8mb4 collate utf8mb4_bin;
-- 创建用户
create user zabbix@localhost identified by 'zabbix';
-- 为用户授权
grant all privileges on zabbix.* to zabbix@localhost;

安装依赖包

bash
yum install mailx postfix dos2unix -y
yum -y install openssl-devel libcurl-devel libevent-devel
yum -y install net-snmp-devel

# 设置开机启动
systemctl enable postfix --now

编译zabbix

bash
# 下载源码包
wget https://cdn.zabbix.com/zabbix/sources/stable/6.0/zabbix-6.0.28.tar.gz
tar -zxvf zabbix-6.0.28.tar.gz -C /usr/src
cd /usr/src/zabbix-6.0.28

# 预配置
./configure --prefix=/usr/local/zabbix/ \
  --enable-server \
  --enable-agent \
  --with-mysql \
  --with-net-snmp \
  --with-libcurl --with-libevent  
  
# 安装
make install

导入数据

bash
# sql文件在zabbix-6.0.28/database/mysql/
mysql -B zabbix < schema.sql
mysql -B zabbix < images.sql
mysql -B zabbix < data.sql

导入前端代码

bash
# 代码在zabbix-6.0.28/ui
rm -rf /usr/share/nginx/html/*
cp -rp ./* /usr/share/nginx/html/

设置语言包

将Windows的中文ttf字体 C:\Windows\Fonts目录下的字体上传至zabbix-server服务器,并且将字体改名为已经被套了几层软连接的默认字体名字DejaVuSans.ttf,不用重启服务器或应用,刷新页面即可。

bash
# 文件位置在zabbix-6.0.28/assets/fonts

cp DejaVuSans.ttf DejaVuSans.ttf.bak

# 比如华松中文字体
mv STZHONGS.TTF DejaVuSans.ttf

chown www:www DejaVuSans.ttf

配置service管理文件

bash
cat > /usr/local/zabbix/etc/zabbix_server.conf << "EOF"
LogFile=/var/log/zabbix/zabbix_server.log
PidFile=/tmp/zabbix_server.pid
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
Timeout=4
LogSlowQueries=3000
StatsAllowedIP=0.0.0.0
EOF

# 创建日志文件
mkdir /var/log/zabbix -p
chown -R zabbix:zabbix /var/log/zabbix/

启动zabbix

bash
cat > /usr/lib/systemd/system/zabbix.service << "EOF"
[Unit]
Description=Zabbix Server
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=/usr/local/zabbix/etc/zabbix_server.conf"
EnvironmentFile=-/usr/local/zabbix
Type=forking
PIDFile=/tmp/zabbix_server.pid
ExecStart=/usr/local/zabbix/sbin/zabbix_server -c
/usr/local/zabbix/etc/zabbix_server.conf
ExecStop=/usr/bin/kill $MAINPID
Restart=always
RestartSec=5
User=zabbix
Group=zabbix

[Install]
WantedBy=multi-user.target
EOF

# 启动服务
systemctl daemon-reload
systemctl enable zabbix --now


# 检测进程
ps aux|grep zabbix
ss -anplut|grep zabbix

安装zabbix get

bash
# 安装
dnf -y install zabbix-get

# 检查zabbix的联通性
zabbix_get -s 192.168.148.106 -k "agent.ping"

-s 指定客户端IP地址
执行结果是1,表示客户端配置正常

部署zabbix agent

安装服务

bash
# 设置zabbix源
rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/9/x86_64/zabbix-release-6.4-1.el9.noarch.rpm

# 安装
dnf -y install zabbix-agent2

修改agent配置文件

bash
cd /etc/zabbix/
cp zabbix_agent2.conf{,.bak}

# 配置文件
cat > /etc/zabbix/zabbix_agent2.conf << "EOF"
PidFile=/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=0
# 被动模式的服务端ip地址
Server=192.168.148.105
# 主动模式的服务端ip地址
ServerActive=192.168.148.105
# 当前主机 hostname
Hostname=zabbix-agent
Include=/etc/zabbix/zabbix_agent2.d/*.conf
PluginSocket=/run/zabbix/agent.plugin.sock
ControlSocket=/run/zabbix/agent.sock
Include=/etc/zabbix/zabbix_agent2.d/plugins.d/*.conf
EOF

# 启动服务
systemctl restart zabbix-agent2