ZBX:源码部署Zabbix 7
- Zabbix概述
- 安装前准备工作
- 部署LNMP环境
- 部署Zabbix 7
Zabbix7源码部署-第4集-Zabbix Server部署
一、Zabbix概述
1. 什么是zabbix
- Zabbix是一个开源的、免费的、易于理解的、容易应用的监控软件。 是在企业中比较流行的、运行稳定的监控解决方案。
2. 为什么学习zabbix
- Zabbix适应场景:面向虚拟机、服务器、网络设备较为传统的环境。
- Prometheus适应场景:面向容器、k8s、微服务、云原生应用领域。
企业中多为二者结合、在相互擅长的领域相互配合使用。
3. 逻辑架构
- 典型的C/S架构
Client: Zabbix agent/agent2 , SNMP/IPMI , JMX , Zabbix API/Sender
Server: Zabbix Server , Zabbix Web , Zabbix DB , Zabbix Proxy
3. 实验环境
操作系统 | 版本 | CPU | 内存 | 硬盘 |
---|---|---|---|---|
CentOS | 9 | 2C | 4G | 70G |
应用 | 版本 | 安装路径 | 用户 |
---|---|---|---|
JDK | 21.0.5 | /opt/jdk | root |
Nginx | 1.27.2 | /opt/nginx | nginx |
PHP | 8.3.13 | /opt/php | nginx |
Server | 7.0.5 | /opt/zbx | zabbix |
MySQL | 8.4.3 | /opt/mysql | mysql |
4. 软件下载:
JDK:
https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
Nginx:
https://nginx.org/en/download.html
PHP:
https://www.php.net/downloads.php
MySQL:
https://dev.mysql.com/downloads/
如何选择下载的mysql版本?
[root@localhost php-8.3.13]# ldd --version
ldd (GNU libc) 2.34
[root@localhost ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 6
二、安装前的系统准备工作
1. 配置主机名
[root@localhost ~]# hostnamectl set-hostname zbx1
[root@localhost ~]# bash
bash
[root@zbx1 ~]#
2. 关闭防火墙
[root@zbx1 ~]# systemctl stop firewalld
[root@zbx1 ~]# systemctl disable firewalld
3. 关闭selinux
[root@zbx1 ~]# vim /etc/selinux/config
SELINUX=disabled
4. 配置yum源
[root@zbx1 ~]# rm -rf /etc/yum.repos.d/*
[root@zbx1 ~]# vim /etc/yum.repos.d/CentOS-Base.repo
[BaseOS]
name = BaseOS
baseurl = https://mirrors.bfsu.edu.cn/centos-stream/9-stream/BaseOS/x86_64/os/
enable = 1
gpgcheck = 0
[AppStream]
name = AppStream
baseurl = https://mirrors.bfsu.edu.cn/centos-stream/9-stream/AppStream/x86_64/os/
enable = 1
gpgcheck = 0
[CRB]
name = CRB
baseurl = https://mirrors.bfsu.edu.cn/centos-stream/9-stream/CRB/x86_64/os/
enable = 1
gpgcheck = 0
[epel]
name = epel
baseurl = https://mirrors.bfsu.edu.cn/epel/9/Everything/x86_64/
enable = 1
gpgcheck = 0
5. 配置时钟同步
[root@zbx1 ~]# vim /etc/chrony.conf
pool ntp1.aliyun.com iburst
pool ntp2.aliyun.com iburst
pool ntp3.aliyun.com iburst
[root@zbx1 ~]# systemctl restart chronyd
[root@zbx1 ~]# systemctl enable chronyd
[root@zbx1 ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? 120.25.115.20 2 7 2 16 +1470us[+1470us] +/- 27ms
^* 203.107.6.88 2 6 17 21 -3300us[-2942us] +/- 24ms
[root@zbx1 ~]# time
time timedatectl timeout times
[root@zbx1 ~]# timedatectl
Local time: 日 2024-11-17 08:59:04 CST
Universal time: 日 2024-11-17 00:59:04 UTC
RTC time: 日 2024-11-17 00:59:04
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
6. 优化系统限制
[root@zbx1 ~]# vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
* soft memlock unlimited
* hard memlock unlimited
7. 安装JDK
[root@zbx1 ~]# cd /opt/soft/
[root@zbx1 soft]# tar zxf jdk-21_linux-x64_bin.tar.gz
[root@zbx1 soft]# cp -r jdk-21.0.5 /opt/jdk
[root@zbx1 ~]# vim /etc/profile
export JAVA_HOME=/opt/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
[root@zbx1 ~]# source /etc/profile
[root@zbx1 ~]# java -version
java 21.0.5 2024-10-15 LTS
Java(TM) SE Runtime Environment (build 21.0.5+9-LTS-239)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.5+9-LTS-239, mixed mode, sharing)
三、部署LNMP环境
1. 部署Nginx
- 安装依赖包
[root@zbx1 ~]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
- 创建应用用户
[root@zbx1 ~]# groupadd nginx && useradd nginx -g nginx
- 编译安装
[root@zbx1 ~]# mkdir -p /opt/nginx
[root@zbx1 ~]# cd /opt/soft/
[root@zbx1 soft]# tar zxf nginx-1.27.2.tar.gz
[root@zbx1 soft]# cd nginx-1.27.2/
[root@zbx1 nginx-1.27.2]# ./configure --user=nginx --group=nginx --prefix=/opt/nginx --conf-path=/opt/nginx/conf/nginx.conf \
--with-http_gzip_static_module --with-http_ssl_module --with-http_realip_module --with-http_random_index_module \
--with-http_addition_module --with-http_stub_status_module --with-pcre --with-threads --with-stream --with-debug \
--with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module \
>> /tmp/install_nginx.log 2>&1 && make >> /tmp/install_nginx.log 2>&1 && make install >> /tmp/install_nginx.log 2>&1
- 添加为系统服务
[root@zbx1 ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/opt/nginx/sbin/nginx
ExecReload=/opt/nginx/sbin/nginx reload
ExecStop=/opt/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@zbx1 ~]# systemctl daemon-reload
[root@zbx1 ~]# systemctl start nginx.service
[root@zbx1 ~]# systemctl enable nginx.service
2. 部署Mysql
- 选择下载的版本
cat /etc/redhat-release
[root@zbx1 ~]# ldd --version
ldd (GNU libc) 2.34
[root@zbx1 ~]# lscpu | grep Architecture
Architecture: x86_64
- 创建用户解压软件
[root@zbx1 ~]# groupadd mysql && useradd mysql -g mysql
[root@zbx1 ~]# mkdir -p /data/mysql/{data,logs}
[root@zbx1 ~]# cd /opt/soft/
[root@zbx1 soft]# tar zxf mysql-8.4.3-linux-glibc2.17-x86_64.tar.xz
[root@zbx1 soft]# cp -r mysql-8.4.3-linux-glibc2.28-x86_64 /opt/mysql
[root@zbx1 ~]# chown -R mysql:mysql /data/mysql/ /opt/mysql/
- 编辑配置文件
[root@zbx1 ~]# vim /opt/mysql/my.cnf
[mysqld]
port = 3306 #配置监听端口
server-id = 1
user = mysql #指定运行用户
basedir = /opt/mysql #指定mysql家目录
socket = /data/mysql/my.sock #指定mysql的sock文件地址
datadir = /data/mysql/data #指定mysql数据目录
max_connections = 1500 #设置mysql最大连接数
character-set-server = utf8mb4 #设置数据库默认字符集
log-error = /data/mysql/logs/error.log #开启错误日志并配置日志地址
default-time-zone = '+08:00' #配置时区
mysql_native_password = ON
[client]
socket = /data/mysql/my.sock
[root@zbx1 ~]# ln -s /opt/mysql/my.cnf /etc/my.cnf
[root@zbx1 ~]# ln -s /opt/mysql/lib/libmysqlclient.so.24.0.3 /usr/lib64/libmysqlclient.so.24
- 初始化数据库
[root@zbx1 ~]# /opt/mysql/bin/mysqld --defaults-file=/opt/mysql/my.cnf --initialize
- 添加为系统服务
[root@zbx1 ~]# vim /lib/systemd/system/mysqld.service
[Unit]
Description=MySql
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=/opt/mysql/bin/mysqld --defaults-file=/opt/mysql/my.cnf
LimitNOFILE=5000
[Install]
WantedBy=multi-user.target
[root@zbx1 ~]# systemctl daemon-reload
[root@zbx1 ~]# systemctl start mysqld.service
[root@zbx1 ~]# systemctl enable mysqld.service
- 重置数据库密码
[root@zbx1 logs]# mysql -u root -p
Enter password:
mysql> alter user user() identified by '1qaz3edc';
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '1qaz3edc';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
3. 部署php
- 安装依赖包
[root@zbx1 ~]# yum install -y libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel \
libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses curl gdbm-devel \
libXpm-devel libX11-devel gd-devel gmp-devel expat-devel xmlrpc-c \
libicu-devel libmcrypt-devel libzip gcc-c++ libsqlite3x-devel sqlite-devel oniguruma oniguruma-devel
- 编译安装
[root@zbx1 ~]# mkdir -p /opt/php
[root@zbx1 ~]# cd /opt/soft/
[root@zbx1 soft]# tar zxf php-8.3.13.tar.gz
[root@zbx1 soft]# cd php-8.3.13/
[root@zbx1 php-8.3.13]# ./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc \
--enable-ctype --enable-mysqlnd --with-mysql-sock=/data/mysql/my.sock \
--with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib --enable-xml \
--disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem \
--with-curl --enable-mbregex --enable-mbstring --enable-gd --with-openssl \
--with-mhash --enable-pcntl --enable-sockets --with-ldap-sasl --enable-soap \
--with-gettext --enable-fpm --with-freetype --with-jpeg >> /tmp/install_php.log 2>&1 \
&& make >> /tmp/install_php.log 2>&1 && make install >> /tmp/install_php.log 2>&1
- 编辑配置文件和环境变量
[root@zbx1 php-8.3.13]# cp php.ini-production /opt/php/etc/php.ini
[root@zbx1 php-8.3.13]# vim /opt/php/etc/php.ini
post_max_size = 64M
max_execution_time = 300
max_input_time = 300
date.timezone = Asia/Shanghai
always_populate_raw_post_data = -1
[root@zbx1 php-8.3.13]# vim /opt/php/etc/php-fpm.conf
[global]
[www]
user = nginx
group = nginx
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
[root@zbx1 ~]# vim /etc/profile
export PATH=$PATH:/opt/php/bin
- 添加为系统服务
[root@zbx1 ~]# vim /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=/opt/php/sbin/php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@zbx1 ~]# systemctl daemon-reload
[root@zbx1 ~]# systemctl start php-fpm.service
[root@zbx1 ~]# systemctl enable php-fpm.service
- 修改nginx配置文件
[root@zbx1 ~]# vim /opt/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/zabbix.access.log;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ .*\.(php|php5)?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
[root@zbx1 ~]# systemctl restart nginx.service
- 功能性验证
[root@zbx1 ~]# vim /opt/nginx/html/index.php
<?php
phpinfo();
?>
root@zbx1 ~]# vim /opt/nginx/html/test.php
<?php
$link=mysqli_connect("127.0.0.1","root","1qaz3edc");
if(!$link) echo "Error !";
else echo "Ok!";
mysqli_close();
?>
四、部署Zabbix Server
1. 安装依赖软件
[root@zbx1 ~]# yum -y install libevent-devel wget tar gcc gcc-c++ \
make net-snmp-devel libxml2-devel libcurl-devel \
unixODBC-devel fping OpenIPMI OpenIPMI-devel libyaml-devel go
2. 创建用户
[root@zbx1 ~]# groupadd zabbix && useradd zabbix -g zabbix
3. 编译安装zbx服务
[root@zbx1 soft]# tar zxf zabbix-7.0.5.tar.gz
[root@zbx1 soft]# cd zabbix-7.0.5
[root@zbx1 zabbix-7.0.5]# ./configure --prefix=/opt/zabbix --enable-server --enable-java \
--with-mysql=/opt/mysql/bin/mysql_config \
--with-net-snmp --with-libcurl --with-libxm2 --with-openipmi --with-unixodbc --enable-yaml
[root@zbx1 ~]# make install
4. 创建数据库用户
[root@zbx1 ~]# /opt/mysql/bin/mysql -u root -p
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> CREATE USER 'zabbix'@'%' IDENTIFIED BY '1qaz3edc';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'zabbix'@'%' WITH GRANT OPTION;
5. 初始化数据库
[root@zbx1 ~]# cd /opt/soft/zabbix-7.0.5/database/mysql
[root@zbx1 mysql]# /opt/mysql/bin/mysql -u zabbix -p zabbix < schema.sql
[root@zbx1 mysql]# /opt/mysql/bin/mysql -u zabbix -p zabbix < images.sql
[root@zbx1 mysql]# /opt/mysql/bin/mysql -u zabbix -p zabbix < data.sql
6. 复制前端文件
[root@zbx1 ~]# cp -r /opt/soft/zabbix-7.0.5/ui /opt/nginx/html/zabbix
[root@zbx1 ~]# chown -R nginx:nginx /opt/nginx/
[root@zbx1 ~]# echo 'PATH=$PATH:/opt/zabbix/bin:/opt/zabbix/sbin' >> /etc/profile
7. 添加系统服务
vim /usr/lib/systemd/system/zabbix_server.service
[Unit]
Description=Zabbix Server
After=network.target
After=mysqld.service
After=nginx.service
[Service]
User=zabbix
Group=zabbix
Environment="CONFFILE=/opt/zabbix/etc/zabbix_server.conf"
EnvironmentFile=-/etc/sysconfig/zabbix_server
Type=forking
Restart=on-failure
PIDFile=/opt/zabbix/zabbix_server.pid
KillMode=control-group
ExecStart=/opt/zabbix/sbin/zabbix_server -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
TimeoutSec=0
[Install]
WantedBy=multi-user.target
8.编辑zabbix配置文件
[root@zbx1 ~]# grep -v "^$|^*" /opt/zabbix/etc/zabbix_server.conf
LogFile=/opt/zabbix/zabbix_server.log
PidFile=/opt/zabbix/zabbix_server.pid
SocketDir=/opt/zabbix
DBHost=192.168.31.160
DBName=zabbix
DBUser=zabbix
DBPassword=1qaz3edc
DBPort=3306
StartPollers=50
StartPreprocessors=10
StartTrappers=10
StartPingers=5
StartDiscoverers=5
CacheSize=128M
TrendCacheSize=32M
ValueCacheSize=32M
Timeout=4
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
9. 启动服务
[root@zbx1 ~]# chown -R zabbix:zabbix /opt/zabbix/
[root@zbx1 ~]# systemctl enable zabbix_server.service
[root@zbx1 ~]# systemctl start zabbix_server.service
10. 初始化服务
用户名:Adimin,密码:zabbix
http://192.168.31.160/zabbix/setup.php
11. 编译安装zabbix_agent2
[root@zbx1 soft]# cd zabbix-7.0.5
[root@zbx1 zabbix-7.0.5]# ./configure --prefix=/opt/zabbix_agent2 --enable-agent2
[root@zbx1 zabbix-7.0.5]# make install
12. 编辑zabbix_agent2配置文件
[root@zbx1 ~]# vim /opt/zabbix_agent2/etc/zabbix_agent2.conf
PidFile=/opt/zabbix_agent2/zabbix_agent2.pid
LogFile=/opt/zabbix_agent2/zabbix_agent2.log
LogFileSize=0
SourceIP=192.168.31.160
Server=192.168.31.160
ServerActive=192.168.31.160
HostnameItem=system.hostname
Include=/opt/zabbix_agent2/etc/zabbix_agent2.d/*.conf
ControlSocket=/tmp/agent.sock
13. 启动zabbix_agent2
[root@zbx1 ~]# /opt/zabbix_agent/sbin/zabbix_agent2 -c /opt/zabbix_agent/etc/zabbix_agent2.conf &