环境

  • 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,内容如下

    1
    2
    3
    4
    5
    6
    7
    
    [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,内容如下

    1
    2
    3
    4
    5
    6
    
    [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

    1
    2
    
    #这货依赖 epel-release,前面已经手动创建 epel 源,不鸟它!
    rpm -Uvh --nodeps https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  • 创建 /etc/yum.repos.d/mysql5.7.repo,内容如下

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    [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,内容如下

    1
    2
    3
    4
    5
    6
    
    [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 缓存

    1
    2
    
    yum clean all
    yum makecache fast

安装 nginx

  • 安装

    1
    
    yum install nginx
  • 修改 /etc/nginx/nginx.conf,内容如下

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    # 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,内容如下

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    # 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;
        }
    }
  • 开机自启

    1
    
    systemctl enable nginx
  • 启动 nginx

    1
    
    systemctl start nginx

安装 php

  • 安装

    1
    2
    3
    4
    5
    6
    7
    8
    
    yum install php72w \
        php72w-cli \
        php72w-fpm \
        php72w-gd \
        php72w-mbstring \
        php72w-xml \
        php72w-mysqlnd \
        php72w-bcmath
  • 修改 /etc/php.ini 中如下配置项

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    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 中如下配置

    1
    2
    
    listen = /var/lib/php/phpfpm.sock
    listen.mode = 0666
  • 开机自启

    1
    
    systemctl enable php-fpm
  • 启动 php-fpm

    1
    
    systemctl start php-fpm

安装 mysql 5.7

  • 安装,参考 CentOS7 yum 安装 MySQL5.7
  • 启动 mysqld 服务,创建 zabbix 数据库及其用户

    1
    2
    3
    
    create database zabbix default charset utf8mb4;
    grant all on zabbix.* to zbx@localhost identified by 'Zbx_123456';
    flush privileges;

安装 zabbix server

  • 安装

    1
    
    yum install zabbix-server-mysql zabbix-web-mysql
  • 导入 zabbix server 初始数据

    1
    
    zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzbx -p -Dzabbix
  • 修改 /etc/zabbix/zabbix_server.conf 中如下配置

    1
    2
    3
    4
    5
    
    DBName=zabbix
    DBUser=zbx
    DBPassword=Zbx_123456
    DBSocket=/var/lib/mysql/mysql.sock
    AllowRoot=1
  • 开机自启

    1
    
    systemctl enable zabbix-server
  • 启动 zabbix-server

    1
    
    systemctl start zabbix-server

web 界面设置

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

安装 zabbix agent

  • 可以直接 yum 安装,这里推荐先下载 rpm 包,方便复制到其他服务器中运行

    1
    
    yum install zabbix-agent --downloadonly --downloaddir=.
  • 安装

    1
    
    rpm -Uvh zabbix-agent-4.4.*.rpm
  • 修改 /etc/zabbix/zabbix_agentd.conf 中如下配置

    1
    2
    3
    4
    
    EnableRemoteCommands=1
    AllowRoot=1
    #UnsafeUserParameters=1
    #UserParameter=UP[*],/etc/zabbix/UP.sh $1 $2 $3 $4 $5 $6 $7 $8 $9
  • 开机自启

    1
    
    systemctl enable zabbix-agent
  • 启动 zabbix-agent

    1
    
    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

    1
    
    rpm -Uvh zabbix-agent-4.4.\*.rpm
  • 修改 /etc/zabbix/zabbix_agentd.conf 中如下配置

    1
    2
    3
    4
    5
    6
    7
    
    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
  • 开机自启

    1
    
    systemctl enable zabbix-agent
  • 启动 zabbix-agent

    1
    
    systemctl start zabbix-agent

Docker 启动 zabbix server

  • 下载镜像

    1
    
    docker pull ccr.ccs.tencentyun.com/colben/zabbix
  • 启动容器

    1
    2
    3
    4
    5
    
    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