两台虚拟机搭建zabbix监控

1.安装

1.1 安装mysql

在192.168.220.50这台主机上源码安装mysql

//安装开发工具包
[[email protected] ~]# yum groups mark install 'Development Tools'
[[email protected] ~]# yum -y install wget bzip2 bzip2-devel gcc gcc-c++

//安装依赖包
[r[email protected] ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

//创建用户和组
[[email protected] ~]# groupadd -r -g 306 mysql
[[email protected] ~]# useradd -r -M -s /sbin/nologin -g mysql mysql

//下载二进制格式的mysql软件包
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz

//解压软件至/usr/local/
[[email protected] src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[[email protected] src]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.23-linux-glibc2.12-x86_64  share
[[email protected] src]# cd /usr/local/
[[email protected] local]# ln -s mysql-5.7.23-linux-glibc2.12-x86_64/ mysql

//修改目录/usr/local/mysql的属主属组,添加环境变量
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]#  echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[[email protected] local]# . /etc/profile.d/mysql.sh 

//建立数据存放目录
[[email protected] local]# mkdir /opt/data
[[email protected] local]# chown -R mysql.mysql /opt/data/


//初始化数据库
[[email protected] local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/


//配置mysql
[[email protected] local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[[email protected] local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[[email protected] local]# ldconfig -v

//生成配置文件
[[email protected] local]# cat > /etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF

//配置服务启动脚本
[[email protected] local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] local]#  sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[[email protected] local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

//启动
[[email protected] local]# service mysqld start

//修改密码
mysql> set password = password(123456');

1.2 安装php

在192.168.220.50这台主机上源码安装php

//安装开发工具包
[[email protected] ~]# yum groups mark install 'Development Tools'
[[email protected] ~]# yum -y install wget bzip2 bzip2-devel gcc gcc-c++

//安装依赖包
yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  libpcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php72w-mysqlnd

//下载php
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz

//编译安装php
[[email protected] src]# tar xf php-7.2.8.tar.xz 
[[email protected] src]# cd php-7.2.8
[[email protected] php-7.2.8]# ./configure --prefix=/usr/local/php7  \
> --with-config-file-path=/etc \
> --enable-fpm \
> --enable-inline-optimization \
> --disable-debug \
> --disable-rpath \
> --enable-shared \
> --enable-soap \
> --with-openssl \
> --enable-bcmath \
> --with-iconv \
> --with-bz2 \
> --enable-calendar \
> --with-curl \
> --enable-exif  \
> --enable-ftp \
> --with-gd \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib-dir \
> --with-freetype-dir \
> --with-gettext \
> --enable-json \
> --enable-mbstring \
> --enable-pdo \
> --with-mysqli=mysqlnd \
> --with-pdo-mysql=mysqlnd \
> --with-readline \
> --enable-shmop \
> --enable-simplexml \
> --enable-sockets \
> --enable-zip \
> --enable-mysqlnd-compression-support \
> --with-pear \
> --enable-pcntl \
> --enable-posix
 [[email protected] php-7.2.8]# make && make install


//安装后配置
[[email protected] php-7.2.8]#  echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[[email protected] php-7.2.8]# . /etc/profile.d/php7.sh 
[[email protected] php-7.2.8]# php -v
PHP 7.2.8 (cli) (built: Mar  1 2019 02:31:03) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

//配置php-fpm
[[email protected] php-7.2.8]# cp php.ini-production /etc/php.ini
[[email protected] php-7.2.8]#  cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[[email protected] php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[[email protected] php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[[email protected] php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
[[email protected] php-7.2.8]# vim /usr/local/php7/etc/php-fpm.conf
[[email protected] php-7.2.8]# tail -4 /usr/local/php7/etc/php-fpm.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8

//启动
[[email protected] php-7.2.8]# service php-fpm start


1.3 安装nginx

在192.168.220.60这台主机上源码安装ngninx

//安装开发工具包
[[email protected] ~]# yum groups mark install 'Development Tools'
[[email protected] ~]# yum -y install wget bzip2 bzip2-devel gcc gcc-c++

//创建系统用户ngin
[[email protected] ~]# groupadd -r nginx
[[email protected] ~]# useradd -r -s /sbin/nologin -M -g nginx nginx

//安装依赖环境
[[email protected] ~]#  yum -y install pcre-devel openssl openssl-devel gd-devel

//创建日志存放目录
[[email protected] ~]# mkdir -p /var/log/nginx
[[email protected] ~]# chown -R nginx.nginx /var/log/nginx/

//下载nginx
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz

//编译安装
[[email protected] src]# tar xf nginx-1.12.0.tar.gz 
[[email protected] src]# ls
debug  kernels  nginx-1.12.0  nginx-1.12.0.tar.gz
[[email protected] src]# cd nginx-1.12.0
[[email protected] nginx-1.12.0]#  ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
 [[email protected] nginx-1.12.0]# make && make install
 
//nginx安装后配置
[[email protected] nginx-1.12.0]#  echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[[email protected] nginx-1.12.0]# . /etc/profile.d/nginx.sh 

//启动
[[email protected] ~]# nginx

1.3 安装zabbix

在192.168.220.50这台虚拟机安装zabbix

//下载zabbix,解压
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.12/zabbix-3.4.12.tar.gz
[[email protected] src]# tar xf zabbix-3.4.12.tar.gz

//安装依赖包
[[email protected] src]# yum -y install net-snmp-devel libevent-devel

//创建zabbix用户和组
[[email protected] src]# groupadd -r zabbix
[[email protected] src]# useradd -r -g zabbix -M -s /sbin/nologin zabbix


//配置zabbix数据库
[[email protected] src]# mysql -uroot -p123456
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on zabbix.* to [email protected] identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[[email protected] src]# cd /usr/src/zabbix-3.4.12/database/mysql/
[[email protected] mysql]# ls
data.sql  images.sql  schema.sql
[[email protected] mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

//编译安装zabbix
[[email protected] mysql]# cd /usr/src/zabbix-3.4.12
[[email protected] zabbix-3.4.12]# [[email protected] zabbix-4.0.3]# ./configure --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
[[email protected] zabbix-3.4.12]# make install

//安装后配置
//下载zabbix,解压
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
[[email protected] src]# tar xf zabbix-3.4.12.tar.gz

//安装依赖包
[[email protected] src]# yum -y install net-snmp-devel libevent-devel

//创建zabbix用户和组
[[email protected] src]# groupadd -r zabbix
[[email protected] src]# useradd -r -g zabbix -M -s /sbin/nologin zabbix


//配置zabbix数据库
[[email protected] src]# mysql -uroot -p123456
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on zabbix.* to [email protected] identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[[email protected] src]# cd /usr/src/zabbix-3.4.12/database/mysql/
[[email protected] mysql]# ls
data.sql  images.sql  schema.sql
[[email protected] mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

//编译安装zabbix
[[email protected] mysql]# cd /usr/src/zabbix-3.4.12
[[email protected] zabbix-3.4.12]# [[email protected] zabbix-4.0.3]# ./configure --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
[[email protected] zabbix-3.4.12]# make install

//修改/etc/php.ini的配置并重启php-fpm
[[email protected] etc]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[[email protected] etc]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini[[email protected] etc]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[[email protected] etc]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[[email protected] etc]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done


2配置

2.1关闭防火墙

[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# setenforce 0

2.2修改php配置文件

[[email protected] ~]# vim  /usr/local/php7/etc/php-fpm.d/www.conf
 listen = 0.0.0.0:9000  //修改这一行

2.3 zabbix web界面安装配置

[[email protected] ~]# mkdir -p /www/zzg/zabbix
[[email protected] ~]# cd /usr/src/zabbix-4.0.4
[[email protected] ~]#  cp -a frontends/php/* /www/zzg/zabbix/
[[email protected] ~]# chmod -R 777 /www/zzg/zabbix/

2.5 安装nfs共享

[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]#  systemctl start rpcbind nfs-server
[[email protected] ~]# cat /etc/exports
/www/zzg/zabbix   192.168.220.60(insecure,rw)
[[email protected] ~]# exportfs -r

2.6修改nginx的配置文件


[[email protected] ~]# tail /usr/local/nginx/conf/nginx.conf

        location / {
            root   html;
            index index.php index.html index.htm;    //添加index.php
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root /www/zzg/zabbix;     //zabbix目录
            fastcgi_pass   192.168.220.50:9000;    //对方ip
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /www/zzg/zabbix$fastcgi_script_name;  
            include        fastcgi_params;
        }

2.7挂载共享zabbix

[[email protected] ~]# yum -y install nfs-utlis
[[email protected] ~]# mount -t nfs 192.168.220.50:/www/zzg/zabbix  /usr/local/nginx/html/

2.8两边重启服务

//220.50
[[email protected] ~]# service php-fpm restart

//220.60
[[email protected] ~]# nginx -s reload

验证
两台虚拟机搭建zabbix监控