---
title: "CentOS7 yum 安装 Zabbix 4.4"
date: 2019-10-29T21:41:17+08:00
lastmod: 2020-02-12T05:50:00+08:00
keywords: []
tags: ["zabbix", "centos", "yum"]
categories: ["zabbix"]
---

## 环境
- centos 7
- nginx 1.16
- php 7.2
- zabbix 4.4
- mysql 5.7

## 虚拟机
- 192.168.1.100 安装 nginx, php, mysql, zabbix-server, zabbix-agent
- 192.168.1.101 安装 zabbix-agent

## 导入软件源
- 只在 192.168.1.100 上操作
- 创建 /etc/yum.repos.d/epel.repo，内容如下
    ```ini
    [epel]
    name=Extra Packages for Enterprise Linux 7 - $basearch
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch
    failovermethod=priority
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
    ```
- 创建 /etc/yum.repos.d/nginx.repo，内容如下
    ```ini
    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    enabled=1
    gpgcheck=0
    gpgkey=https://nginx.org/keys/nginx_signing.key
    ```
- 安装 webtatic 源，用于安装 php7.2
    ```bash
    #这货依赖 epel-release，前面已经手动创建 epel 源，不鸟它！
    rpm -Uvh --nodeps https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    ```
- 创建 /etc/yum.repos.d/mysql5.7.repo，内容如下
    ```ini
    [mysql-connectors-community]
    name=MySQL Connectors Community
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-connectors-community-el7/
    enabled=1
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

    [mysql-tools-community]
    name=MySQL Tools Community
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-tools-community-el7/
    enabled=1
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

    [mysql57-community]
    name=MySQL 5.7 Community Server
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/
    enabled=1
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    ```
- 创建 /etc/yum.repos.d/zabbix.repo，内容如下
    ```ini
    [zabbix]
    name=Zabbix Official Repository - $basearch
    baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/7/$basearch/
    enabled=1
    gpgcheck=0
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
    ```
- 重建 yum 缓存
    ```bash
    yum clean all
    yum makecache fast
    ```

## 安装 nginx
- 安装
    ```bash
    yum install nginx
    ```
- 修改 /etc/nginx/nginx.conf，内容如下
    ```nginx
    # Nginx main config
    user                 nginx;
    pid                  /run/nginx.pid;
    error_log            /var/log/nginx/error.log;
    worker_processes     auto;
    worker_rlimit_nofile 65535;
    include              /usr/share/nginx/modules/*.conf;
    events {
        use                epoll;
        multi_accept       on;
        worker_connections 10240;
    }
    http {
        log_format main     '$remote_addr - [$time_local] "$request_method $uri" "$args" '
                            '"-" $status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for"';
        access_log          /var/log/nginx/access.log main;
        gzip                on;
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         off;
        server_tokens       off;
        keepalive_timeout   65;
        types_hash_max_size 2048;
        default_type        application/octet-stream;
        include             /etc/nginx/mime.types;
        include             /etc/nginx/conf.d/*.conf;
    }
    ```
- 创建 /etc/nginx/conf.d/10080.conf，内容如下
    ```nginx
    # nginx listen 10080
    server {
        listen      10080;
        server_name _;
        location /zabbix/ {
            root       /usr/share;
            access_log /var/log/nginx/access-zabbix.log main;
            index      index.php index.html index.html;
        }
        location ~ ^/zabbix/.+\.php$ {
            root                    /usr/share;
            access_log              /var/log/nginx/access-zabbix.log main;
            index                   index.php index.html index.html;
            expires                 -1s;
            include                 fastcgi_params;
            try_files               $uri =404;
            fastcgi_pass            unix:/var/lib/php/phpfpm.sock;
            fastcgi_index           index.php;
            fastcgi_param           PATH_INFO $fastcgi_path_info;
            fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            break;
        }
        location / {
            access_log /var/log/nginx/access-illegal.log main;
            return     403;
        }
    }
    ```
- 开机自启
    ```bash
    systemctl enable nginx
    ```
- 启动 nginx
    ```bash
    systemctl start nginx
    ```

## 安装 php
- 安装
    ```bash
    yum install php72w \
        php72w-cli \
        php72w-fpm \
        php72w-gd \
        php72w-mbstring \
        php72w-xml \
        php72w-mysqlnd \
        php72w-bcmath
    ```
- 修改 /etc/php.ini 中如下配置项
    ```ini
    max_execution_time = 300
    memory_limit = 128M
    post_max_size = 16M
    upload_max_filesize = 2M
    max_input_time = 300
    max_input_vars = 10000
    date.timezone = PRC
    #php 与 mysql 同机可指定 mysql 本地套接字
    pdo_mysql.default_socket = /var/lib/mysql/mysql.sock
    mysqli.default_socket = /var/lib/mysql/mysql.sock
    ```
- 修改 /etc/php-fpm.d/www.conf 中如下配置
    ```ini
    listen = /var/lib/php/phpfpm.sock
    listen.mode = 0666
    ```
- 开机自启
    ```bash
    systemctl enable php-fpm
    ```
- 启动 php-fpm
    ```bash
    systemctl start php-fpm
    ```

## 安装 mysql 5.7
- 安装，参考 [CentOS7 yum 安装 MySQL5.7](/post/mysql5.7-install/)
- 启动 mysqld 服务，创建 zabbix 数据库及其用户
    ```sql
    create database zabbix default charset utf8mb4;
    grant all on zabbix.* to zbx@localhost identified by 'Zbx_123456';
    flush privileges;
    ```

## 安装 zabbix server
- 安装
    ```bash
    yum install zabbix-server-mysql zabbix-web-mysql
    ```
- 导入 zabbix server 初始数据
    ```bash
    zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzbx -p -Dzabbix
    ```
- 修改 /etc/zabbix/zabbix_server.conf 中如下配置
    ```ini
    DBName=zabbix
    DBUser=zbx
    DBPassword=Zbx_123456
    DBSocket=/var/lib/mysql/mysql.sock
    AllowRoot=1
    ```
- 开机自启
    ```bash
    systemctl enable zabbix-server
    ```
- 启动 zabbix-server
    ```bash
    systemctl start zabbix-server
    ```

## web 界面设置
- 使用常用浏览器访问: http://192.168.1.100:10080/zabbix/
- 根据界面提示提供数据库等信息
- zabbix 安装完成，进入系统登陆界面，默认 admin/zabbix

## 安装 zabbix agent
- 可以直接 yum 安装，这里推荐先下载 rpm 包，方便复制到其他服务器中运行
    ```bash
    yum install zabbix-agent --downloadonly --downloaddir=.
    ```
- 安装
    ```bash
    rpm -Uvh zabbix-agent-4.4.*.rpm
    ```
- 修改 /etc/zabbix/zabbix_agentd.conf 中如下配置
    ```ini
    EnableRemoteCommands=1
    AllowRoot=1
    #UnsafeUserParameters=1
    #UserParameter=UP[*],/etc/zabbix/UP.sh $1 $2 $3 $4 $5 $6 $7 $8 $9
    ```
- 开机自启
    ```bash
    systemctl enable zabbix-agent
    ```
- 启动 zabbix-agent
    ```bash
    systemctl start zabbix-agent
    ```

## 在其它服务器上安装 zabbix agent
- 从 192.168.1.100 复制 zabbix-agent-4.4.\*.rpm 到 192.168.1.101 下
- 登陆 192.168.1.101，安装 zabbix-agent
    ```bash
    rpm -Uvh zabbix-agent-4.4.\*.rpm
    ```
- 修改 /etc/zabbix/zabbix_agentd.conf 中如下配置
    ```ini
    EnableRemoteCommands=1
    Server=192.168.1.100
    ServerActive=192.168.1.100:10051
    Hostname=app101 #自定义，别和其他 agent 重名即可
    AllowRoot=1
    #UnsafeUserParameters=1
    #UserParameter=UP[*],/etc/zabbix/UP.sh $1 $2 $3 $4 $5 $6 $7 $8 $9
    ```
- 开机自启
    ```bash
    systemctl enable zabbix-agent
    ```
- 启动 zabbix-agent
    ```bash
    systemctl start zabbix-agent
    ```

## Docker 启动 zabbix server
- 下载镜像
    ```bash
    docker pull ccr.ccs.tencentyun.com/colben/zabbix
    ```
- 启动容器
    ```bash
    docker run -d \
        --name zabbix-server \
        -p 80:80 \
        -p 10051:10051 \
        ccr.ccs.tencentyun.com/colben/zabbix
    ```
- 浏览器访问 http://{ip}/zabbix/ ，打开 zabbix 页面向导，一直 "下一步" 即可
- 配置文件
    - /etc/my.cnf
    - /etc/my.cnf.d/\*.cnf
    - /etc/nginx/nginx.conf
    - /etc/nginx/conf.d
    - /etc/zabbix/zabbix_server.conf
- 数据目录
    - /var/lib/mysql
    - /var/lib/mysql-bin
- 日志目录
    - /var/log/mysql
    - /var/log/nginx
    - /var/log/php7
    - /var/log/zabbix

