AlamLinux8 LNMP 安装配置zabbix6

2024/08/06 zabbix 共 8552 字,约 25 分钟

基于AlamLinux8和 LNMP 安装部署zabbix6

一. zabbix说明

zabbix监控系统是c/s或c-proxy-s架构,通过agent(广义)从各监控端主动或被获取监控数据并存储下来,通过web GUI进行展示,通过定义的触发条件与报警系统进行事件的报警;

1. zabbix架构

zabbix6_00

2. zabbix逻辑组件

   主机组(host groups)
   主机(hosts)
   应用(applications)
   监控项(items)
   触发器(triggers)
   事件(events)
   动作(actions):条件(conditions)和操作(operations)
   媒介(media):发送通知通道;
   通知(notifications)
   远程命令(remote command)
   报警升级(escalation)
   模板(template)
   图形(graph)
   屏幕(screens)
   幻灯(side show)

3. 部署环境说明

    系统环境AlamLinux8.10_x64 ; yum安装数据库:mariadb 10.3.39  、 zabbix 6.4.17、nginx 1.14.1 、php7.4.33  。测试的机器ip: 172.21.161.234

关闭selinux

    防火墙添加好规则,tcp/10051(进出)tcp/10050(出)或为了便于测试直接关闭。

约定说明

    以下实践是本人整理基于以上提到的环境,不完全保证没有遗漏之处,如有错误请留言。nginx,php-fpm等参数

在本次测试中除了非必要没有做性能调优,如生产线上使用,勿必修改对应的参数,提高性能.

4. 安装相关包

# 安装扩展源
yum install epel-release -y
# 安装 mariadb
yum -y install mariadb mariadb-server mariadb-devel -y
# 御载系统自带的php7.2
rpm -qa |grep php
yum remove php-cli-7.2.24-1.module_el8.3.0+2010+7c76a223.x86_64
# 安装 php remi源
yum -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpm  --nogpgcheck
yum -y install php74 php74-php-opcache php74-php-mysqlnd php74-php-pecl-redis5 php74-php-pecl-yac php74-php-pecl-zip php74-php-gd php74-php-pecl-msgpack php74-php-xml php74-php-fpm

# 安装zabbix源
rpm -Uvh https://repo.zabbix.com/zabbix/6.4/rhel/8/x86_64/zabbix-release-6.4-2.el8.noarch.rpm
# 安装zabbix-server,mysql插件,zabbix-agent,nginx配置等
dnf module switch-to php:7.4 
dnf install zabbix-server-mysql zabbix-web-mysql zabbix-nginx-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent zabbix-get -y

5. /etc/hosts配置

# cat /etc/hosts (服务器和本地测试机)
172.21.161.234 zabbix.pkey.cn

二. 安装配置nginx

1. 编译安装nginx

# yum install nginx -y

2. 添加开机启动

# systemctl enable nginx
# systemctl start nginx

3. zabbix-server配置

# cat zabbix_server.conf |egrep -v '^$|^#'
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/run/zabbix/zabbix_server.pid
SocketDir=/run/zabbix
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123456.
AllowUnsupportedDBVersions=1
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1

# nginx配置
 cat zabbix.conf |egrep -v "^$|^#"
server {
        listen          80;
        server_name     zabbix.mobage.cn;
        root    /usr/share/zabbix;
        index   index.php;
        location = /favicon.ico {
                log_not_found   off;
        }
        location / {
                try_files       $uri $uri/ =404;
        }
        location /assets {
                access_log      off;
                expires         10d;
        }
        location ~ /\.ht {
                deny            all;
        }
        location ~ /(api\/|conf[^\.]|include|locale) {
                deny            all;
                return          404;
        }
        location /vendor {
                deny            all;
                return          404;
        }
        location ~ [^/]\.php(/|$) {
                fastcgi_pass    unix:/run/php-fpm/zabbix.sock;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_index   index.php;
                fastcgi_param   DOCUMENT_ROOT   /usr/share/zabbix;
                fastcgi_param   SCRIPT_FILENAME /usr/share/zabbix$fastcgi_script_name;
                fastcgi_param   PATH_TRANSLATED /usr/share/zabbix$fastcgi_script_name;
                include fastcgi_params;
                fastcgi_param   QUERY_STRING    $query_string;
                fastcgi_param   REQUEST_METHOD  $request_method;
                fastcgi_param   CONTENT_TYPE    $content_type;
                fastcgi_param   CONTENT_LENGTH  $content_length;
                fastcgi_intercept_errors        on;
                fastcgi_ignore_client_abort     off;
                fastcgi_connect_timeout         60;
                fastcgi_send_timeout            180;
                fastcgi_read_timeout            180;
                fastcgi_buffer_size             128k;
                fastcgi_buffers                 4 256k;
                fastcgi_busy_buffers_size       256k;
                fastcgi_temp_file_write_size    256k;
        }
}

测试访问http://172.21.161.234 出现”Welcome to nginx on AlmaLinux!”页面表示nginx配置成功。

dd

三. 配置Maridb数据库

1. 添加zabbix数据库及zabbix用户名密码

MariaDB [(none)]> create database zabbix character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by '123456.';
MariaDB [(none)]> set global log_bin_trust_function_creators = 1;

2. 设置root密码

# mysqladmin -u root password "redhat"    #设置数据的root密码(测试时非必要)

3. 初始化zabbix配置

# zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz |mysql --default-character-set=utf8mb4 -uzabbix -p zabbix 

## 登录数据库修改配置
MariaDB [(none)]> set global log_bin_trust_function_creators = 0;

四. 配置php

1. php-fpm配置

# rpm -ql php74-php-fpm-7.4.33-15.el8.remi.x86_64
/etc/httpd/conf.d/php74-php.conf
/etc/logrotate.d/php74-php-fpm
/etc/opt/remi/php74/php-fpm.conf
/etc/opt/remi/php74/php-fpm.d
/etc/opt/remi/php74/php-fpm.d/www.conf
/etc/systemd/system/httpd.service.d/php74-php-fpm.conf
/etc/systemd/system/nginx.service.d/php74-php-fpm.conf
/etc/systemd/system/php74-php-fpm.service.d
/opt/remi/php74/root/usr/sbin/php-fpm
/usr/lib/systemd/system/php74-php-fpm.service
…… 省略 ……
## www.conf配置
# cat /php-fpm.d/www.conf|egrep -v '^;|^$'
[www]
user = nginx 
group = nginx
listen = /run/php-fpm/www.sock
listen.acl_users = apache,nginx
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache

### php-fpm.service
cat /usr/lib/systemd/system/php-fpm.service
# It's not recommended to modify this file in-place, because it
# will be overwritten during upgrades.  If you want to customize,
# the best way is to use the "systemctl edit" command.

[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=notify
ExecStart=/opt/remi/php74/root/usr/sbin/php-fpm --nodaemonize
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
LimitNOFILE=102400

[Install]
WantedBy=multi-user.target

2. zabbix配置


# cat /etc/php-fpm.d/zabbix.conf |egrep -v '^;|^$'
[zabbix]
user = nginx 
group = nginx
listen = /run/php-fpm/zabbix.sock
listen.acl_users = apache,nginx
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 200
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[max_execution_time] = 300
php_value[memory_limit] = 128M
php_value[post_max_size] = 16M
php_value[upload_max_filesize] = 2M
php_value[max_input_time] = 300
php_value[max_input_vars] = 10000

4. 启动php-fpm

systemctl enable php-fpm
systemctl start php-fpm

5. nginx之php-fpm配置

## php-fpm配置 /etc/php-fpm.d/目录下
# sed -i "s/apache/nginx" www.conf
# sed -i "s/apache/nginx" zabbix.conf
## 修改apache为nginx后 需要将以下目录修改为nginx属主属组,否则web安装会报没有权限;
# chown  -R  nginx.nginx /etc/zabbix/web/ 
# cat php-fpm.conf |egrep -v "^$|^#"
upstream php-fpm {
        server unix:/run/php-fpm/zabbix.sock;
}

6. 全局变量配置

echo "PATH=$PATH:/opt/remi/php74/root/bin " >>/etc/bashrc
echo "export PATH " >> /etc/bashrc

五. 安装zabbix

更多版本下载参考

1. 配置zabbix_server.conf

# egrep -v '(^$|^#)' /etc/zabbix/zabbix_server.conf  
LogFile=/var/log/zabbix/zabbix_server.log
LogFileSize=0
PidFile=/run/zabbix/zabbix_server.pid
SocketDir=/run/zabbix
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123456.
AllowUnsupportedDBVersions=1
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1

2. 修改/etc/zabbix/web目录权限

# cd /etc/zabbix
chown zabbix.nginx web -R
chmod 775 web -R* 1.

注:此目录没有权限,后面web安装配置zabbix时提示没有权限信息:

3. Zabbix-agent配置

# cat /etc/zabbix/zabbix_agentd.conf |egrep -v '^$|^#'
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf

4. 配置zabbix-server及相关服务

systemctl restart zabbix-server zabbix-agent nginx php-fpm
systemctl enable zabbix-server zabbix-agent nginx php-fpm

#检查服务状态
# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      1789/zabbix_agentd  
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      21263/zabbix_server 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21742/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      924/sshd            
tcp6       0      0 :::10050                :::*                    LISTEN      1789/zabbix_agentd  
tcp6       0      0 :::10051                :::*                    LISTEN      21263/zabbix_server 
tcp6       0      0 :::3306                 :::*                    LISTEN      1036/mysqld         
tcp6       0      0 :::80                   :::*                    LISTEN      21742/nginx: master 
tcp6       0      0 :::22                   :::*                    LISTEN      924/sshd            
udp        0      0 127.0.0.1:323           0.0.0.0:*                           911/chronyd         
udp6       0      0 ::1:323      

1. 创建zabbix web目录并复制内容

登录后复制

六、web配置安装zabbix

1. 配置zabbix

http://zabbix.pkey.cn

默认的登录用户名密码:Admin/zabbix

zabbix6_01

zabbix6_02

zabbix6_03

zabbix6_04

zabbix6_05

zabbix6_06

安装了正常的包后故障排除,zabbix-server服务正常启动侦听10051端口。

2. 定期清理历史数据(超过30天):

登录后复制

#!/bin/bash
#!/bin/bash
User="root"
Passwd="PASSWORD"
Date=`date -d $(date -d "-30 day" +%Y%m%d) +%s`
$(which mysql) -u${User} -p${Passwd} -e "
    use zabbix;
    DELETE FROM history WHERE 'clock' < $Date;
    optimize table history;
    DELETE FROM history_str WHERE 'clock' < $Date;
    optimize table history_str;
    DELETE FROM history_uint WHERE 'clock' < $Date;
    optimize table history_uint;
    DELETE FROM  trends WHERE 'clock' < $Date;
    optimize table  trends;
    DELETE FROM trends_uint WHERE 'clock' < $Date;
    optimize table trends_uint;
    DELETE FROM events WHERE 'clock' < $Date;
    optimize table events;

3.修复图形乱码问题

zabbix默认配置字体,图形显示汉字是乱码,如下:

zabbix6_07

所以我们要下载配置正确的字体并配置;

下载simkai.ttf字体

1)、将simkai.ttf 放到 zabbix程序所在目录fonts目录下(默认:/usr/share/zabbix/assets/fonts) 如果不知道在哪个目录可以find 查找

2)、修改/usr/share/zabbix/include/defines.inc.php (zabbix6.x 7.x) 将define(‘ZBX_GRAPH_FONT_NAME’, ‘graphfont’) –> 修改为 define(‘ZBX_GRAPH_FONT_NAME’, ‘simkai’) 其他版本类似,可能默认字体不是graphfont,最终重启下zabbix-server服务,如图:

zabbix6_08

文档信息

Search

    Table of Contents