两台虚拟机搭建zabbix监控(lnmp)

1.lnmp平台构建

环境说明:

服务器1 服务器2
ip 192.168.47.138 192.168.47.137
服务 mysql服务
php服务
nginx服务
系统平台 centos7
redhat7
centos7
redhat7

注意:实做验之前要关闭两个服务器的防火墙与selinux

systemctl stop firewalld
setenforce 0

1.1 在服务器1上安装mysql服务与php服务

//安装mysql
//安装依赖包
[root@peng ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
安装过程略...

//创建用户和组
[root@peng ~]# groupadd -r -g 306 mysql
[root@peng ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql


//下载二进制格式的mysql软件包
[root@peng ~]# cd /usr/src/
[root@peng src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
下载过程略...

//解压软件至/usr/local/
[root@peng src]# ls
debug  kernels  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@peng src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@peng ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.23-linux-glibc2.12-x86_64  share
[root@peng ~]# cd /usr/local/
[root@peng local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@peng local]# ll
总用量 0
drwxr-xr-x. 13 root root 152 2月  20 16:01 apache
drwxr-xr-x.  6 root root  58 2月  20 15:52 apr
drwxr-xr-x.  5 root root  43 2月  20 15:53 apr-util
drwxr-xr-x.  2 root root   6 3月  10 2016 bin
drwxr-xr-x.  2 root root   6 3月  10 2016 etc
drwxr-xr-x.  2 root root   6 3月  10 2016 games
drwxr-xr-x.  2 root root   6 3月  10 2016 include
drwxr-xr-x.  2 root root   6 3月  10 2016 lib
drwxr-xr-x.  2 root root   6 3月  10 2016 lib64
drwxr-xr-x.  2 root root   6 3月  10 2016 libexec
lrwxrwxrwx.  1 root root  36 2月  20 16:16 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 2月  20 16:15 mysql-5.7.23-linux-glibc2.12-x86_64
drwxr-xr-x.  2 root root   6 3月  10 2016 sbin
drwxr-xr-x.  5 root root  49 1月  15 18:31 share
drwxr-xr-x.  2 root root   6 3月  10 2016 src

//修改目录/usr/local/mysql的属主属组
[root@peng ~]# chown -R mysql.mysql /usr/local/mysql
[root@peng ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 2月  20 16:16 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/

//添加环境变量
[root@peng ~]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[root@peng ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@peng ~]# . /etc/profile.d/mysql.sh
[root@peng ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

//建立数据存放目录
[root@peng ~]# cd /usr/local/mysql/
[root@peng mysql]# mkdir /opt/data
[root@peng mysql]# chown -R mysql.mysql /opt/data/
[root@peng mysql]#  ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 2月  20 16:21 data

//初始化数据库
[root@peng ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2019-02-20T08:21:51.243481Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-02-20T08:21:51.843605Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-02-20T08:21:51.923869Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-02-20T08:21:52.023144Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 9352971c-34e8-11e9-9119-000c29ec8de1.
2019-02-20T08:21:52.024217Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-02-20T08:21:52.025010Z 1 [Note] A temporary password is generated for root@localhost: 6#?IsD2r1-ar
//请注意,这个命令的最后会生成一个临时密码,此处密码是6#?IsD2r1-ar
//再次注意,这个密码是随机的,你的不会跟我一样,一定要记住这个密码,因为一会登录时会用到

//配置mysql
[root@peng ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@peng ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@peng ~]# ldconfig -v
内容略...

//生成配置文件
[root@peng ~]# vim /etc/my.cnf
[root@peng ~]# cat /etc/my.cnf
[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



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

//启动mysql
[root@peng ~]# service mysqld start
Starting MySQL.. SUCCESS!  
[root@peng ~]# ps -ef|grep mysql
root      61067      1  0 16:35 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     61247  61067  0 16:35 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=peng.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      61277  12253  0 16:36 pts/1    00:00:00 grep --color=auto mysql
[root@peng ~]# ss -antl
State      Recv-Q Send-Q   Local Address:Port                  Peer Address:Port              
LISTEN     0      128                  *:22                               *:*                  
LISTEN     0      100          127.0.0.1:25                               *:*                  
LISTEN     0      128                 :::80                              :::*                  
LISTEN     0      128                 :::22                              :::*                  
LISTEN     0      100                ::1:25                              :::*                  
LISTEN     0      80                  :::3306                            :::*                  


//修改密码
//使用临时密码登录
[root@peng ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

//设置新密码
mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye


//安装php
//配置yum源
[root@peng ~]# cd /etc/yum.repos.d/
[root@peng yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@peng ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@peng ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@peng ~]# yum -y install epel-release
[root@peng ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装过程略。。。。


//安装依赖包
[root@peng ~]# 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
[root@peng ~]# cd /usr/src/
[root@peng src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
下载过程略....


//编译安装php
[root@peng src]# tar xf php-7.2.8.tar.xz
[root@peng src]# cd php-7.2.8
[root@peng 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

[root@peng php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
编译过程略
[root@peng php-7.2.8]# make install
安装过程略

//安装后配置
[root@peng ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@peng ~]# source /etc/profile.d/php7.sh
[root@peng php-7.2.8]# which php
/usr/local/php7/bin/php
[root@peng php-7.2.8]# php -v
PHP 7.2.8 (cli) (built: Aug 16 2018 13:27:30) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

//配置php-fpm
[root@peng php-7.2.8]# cp php.ini-production /etc/php.ini
[root@peng php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@peng php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@peng php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@peng php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

//编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf):
//配置fpm的相关选项为你所需要的值:
[root@peng ~]# vim /usr/local/php7/etc/php-fpm.conf
.....
.....
pm.max_children = 50    //最多同时提供50个进程提供50个并发服务
pm.start_servers = 5    //启动时启动5个进程
pm.min_spare_servers = 2    //最小空闲进程数
pm.max_spare_servers = 8    //最大空闲进程数

[root@peng ~]#  tail /usr/local/php7/etc/php-fpm.conf
;  - the global prefix if it's been set (-p argument)
;  - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf

pm.max_children = 50  
pm.start_servers = 5   
pm.min_spare_servers = 2
pm.max_spare_servers = 8


//启动php-fpm
[root@peng ~]# service php-fpm start
Starting php-fpm  done

//默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
[root@peng ~]# service php-fpm start
Starting php-fpm  done
[root@peng ~]# ss -antl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      100            127.0.0.1:25                                 *:*                  
LISTEN      0      128            127.0.0.1:9000                               *:*                  
LISTEN      0      128                   :::80                                :::*                  
LISTEN      0      128                   :::22                                :::*                  
LISTEN      0      100                  ::1:25                                :::*                  
LISTEN      0      80                    :::3306  
          
[root@peng ~]# ps -ef|grep php
root      81708      1  0 17:31 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody    81709  81708  0 17:31 ?        00:00:00 php-fpm: pool www
nobody    81710  81708  0 17:31 ?        00:00:00 php-fpm: pool www
nobody    81711  81708  0 17:31 ?        00:00:00 php-fpm: pool www
nobody    81712  81708  0 17:31 ?        00:00:00 php-fpm: pool www
nobody    81713  81708  0 17:31 ?        00:00:00 php-fpm: pool www
root      81722  61695  0 17:32 pts/2    00:00:00 grep --color=auto php


1.2 在服务器1上面生成php测试页面

[root@peng ~]# mkdir -p /aaa/bbb
[root@peng ~]# chmod 777 /aaa/bbb
[root@peng ~]# cd /aaa/bbb
[root@peng bbb]# vim index.php 
[root@peng bbb]# cat index.php 

1.3在服务器2上安装nginx服务

//创建nginx服务的用户和组
[root@ye ~]# groupadd -r nginx
[root@ye ~]# useradd -r -M -s /sbin/nologin -g nginx nginx

//安装依赖环境
[root@ye ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++

//安装开发工具包
[root@ye ~]# yum -y groups mark install 'Development Tools'

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

//下载nginx
[root@ye ~]# cd /usr/src/
[root@ye src]# wget http://nginx.org/download/nginx-1.14.2.tar.gz

//编译安装
[root@ye src]# tar xf nginx-1.14.2.tar.gz
root@ye src]# cd nginx-1.14.2
[root@ye nginx-1.14.2]# ./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
编译过程略...

[root@ye nginx-1.14.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
安装过程略....

//配置环境变量
[root@ye nginx-1.14.2]# cd
[root@ye ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@ye ~]# . /etc/profile.d/nginx.sh

//启动nginx
[root@ye ~]# nginx
[root@ye ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128               *:80                            *:*                  
LISTEN     0      128               *:22                            *:*                  
LISTEN     0      128               *:10050                         *:*                  
LISTEN     0      128               *:10051                         *:*                  
LISTEN     0      128       127.0.0.1:9000                          *:*                  
LISTEN     0      128              :::22                           :::*                  
LISTEN     0      80               :::3306                         :::*   

1.4 在服务器2上修改nginx配置文件

[root@ye ~]# vim /usr/local/nginx/conf/nginx.conf
//取消下面语句前面的注释,删除前面的#
#error_log  logs/error.log;

        #access_log  logs/host.access.log  main;

        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

//并修改
        location ~ \.php$ {
            root           /aaa/bbb;
            fastcgi_pass   192.168.47.138:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /aaa/bbb$fastcgi_script_name;
            include        fastcgi_params;
        }
        
//添加index.php
        location / {
            root   html;
            index index.php index.html index.htm;
        }


//重启服务
[root@ye ~]# nginx -s reload

1.5修改/etc/hosts文件,添加域名与IP的映射

两台虚拟机搭建zabbix监控(lnmp)_第1张图片

1.6 验证:

两台虚拟机搭建zabbix监控(lnmp)_第2张图片

2.搭建zabbix监控

2.1在服务器1上面安装zabbix

//安装依赖包
[root@peng ~]# yum -y install net-snmp-devel libevent-devel
安装过程略....

//下载zabbix
[root@peng ~]# cd /usr/src/
[root@peng src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
下载过程略....

//解压
[root@peng src]# tar xf zabbix-4.0.3.tar.gz 

//创建zabbix用户和组
[root@peng ~]# groupadd -r zabbix
[root@peng ~]# useradd -r -g zabbix -M -s /sbin/nologin zabbix

//配置zabbix数据库
[root@peng ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.23 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.03 sec)

mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.01 sec)

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

mysql> quit
Bye
[root@peng ~]# 

[root@peng ~]# cd /usr/src/zabbix-4.0.3/database/mysql/
[root@peng mysql]# ls
data.sql  images.sql  Makefile.am  Makefile.in  schema.sql
[root@peng mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@peng mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@peng mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.

//编译安装zabbix
[root@peng ~]# cd /usr/src/zabbix-4.0.3
[root@peng zabbix-4.0.3]# ./configure --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
编译过程略...

[root@peng zabbix-4.0.3]# make install


//zabbix服务端配置
[root@peng ~]# ls /usr/local/etc/
zabbix_agentd.conf  zabbix_agentd.conf.d  zabbix_server.conf  zabbix_server.conf.d

//修改服务端配置文件
//设置数据库信息
[root@peng ~]# vim /usr/local/etc/zabbix_server.conf
....
DBPassword=zabbix123!       //设置zabbix数据库连接密码


//启动zabbix_server和zabbix_agentd
[root@peng ~]# zabbix_server
[root@peng ~]# zabbix_agentd
[root@peng ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128               *:22                            *:*                  
LISTEN     0      100       127.0.0.1:25                            *:*                  
LISTEN     0      128               *:10050                         *:*                  
LISTEN     0      128               *:10051                         *:*                  
LISTEN     0      128       127.0.0.1:9000                          *:*                  
LISTEN     0      128              :::80                           :::*                  
LISTEN     0      128              :::22                           :::*                  
LISTEN     0      100             ::1:25                           :::*                  
LISTEN     0      80               :::3306                         :::*                  

2.2 在服务器1上配置zabbix web界面

[root@peng ~]# mkdir /aaa/bbb/zabbix
[root@peng ~]# cd /usr/src/zabbix-4.0.3
[root@peng zabbix-4.0.3]# cp -a frontends/php/* /aaa/bbb/zabbix

2.3 修改服务器1上的php配置文件

[root@peng ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000  			//修改为listen = 0.0.0.0:9000


2.4在两个服务器上安装nfs服务共享

//在服务器1上安装nfs
[root@peng ~]# yum -y install nfs-utils
[root@peng ~]# systemctl start rpcbind nfs-server
[root@peng ~]# vim /etc/exports
[root@peng ~]# cat /etc/exports
192.168.47.137   /aaa/bbb/zabbix (rw)
[root@peng ~]# exportfs -r


//在服务器2上安装nfs
[root@ye ~]# yum -y install nfs-utils
不用启动

2.5 修改服务器2上的nginx配置文件

[root@ye ~]# vim /usr/local/nginx/conf/nginx.conf
        
        location / {
            root   html;
            index index.php index.html index.htm;		//添加index.php
        }


        location ~ \.php$ {
            root           /aaa/bbb/zabbix;		
            fastcgi_pass   192.168.47.138:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /aaa/bbb/zabbix$fastcgi_script_name;
            include        fastcgi_params;
        }

//重启服务
[root@ye ~]# nginx -s reload

2.6在服务器2上挂载共享zabbix

[root@ye ~]# mount -t nfs 192.168.47.138:/aaa/bbb/zabbix /usr/local/nginx/html/

2.7验证:

两台虚拟机搭建zabbix监控(lnmp)_第3张图片

你可能感兴趣的:(两台虚拟机搭建zabbix监控(lnmp))