源码编译zabbix监控LNMP

源码编译zabbix监控LNMP

搭建lnmp,在使用源码编译zabbix,在本文档中,使用的包下载如下(包可以提前下载好,使用rz命令上传到服务器上)

nginx-1.16.1.tar.gz(http://nginx.org/download/nginx-1.16.1.tar.gz)

zabbix-3.4.3.tar.gz( https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.3/zabbix-3.4.3.tar.gz)

pcre-8.38.zip(https://ftp.pcre.org/pub/pcre/pcre-8.38.zip

该实验使用了一台服务器部署(看个人情况,可以使用多台部署) 192.168.184.159
1、检查基础环境
关闭selinux和防火墙,更新扩展源,安装lnmp的基础环境

[root@localhost ~]# systemctl stop firewalld   # 关闭防火墙
[root@localhost ~]# systemctl disable firewalld   # 开机自动关闭防火墙
[root@localhost ~]# setenforce 0               # 临时关闭selinux
[root@localhost ~]# vim /etc/selinux/config    #永久关闭selinux,但是要重启机器才会生效(reboot)
 SELINUX=disabled                              #将enforcing修改为disabled
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum clean all 
[root@localhost ~]# yum -y install make gcc gcc-c++ flex bison file libtool libtool-libs autoconf kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel unzip libcap vim lsof wget net-tools 

2、上传pcre-8.38.zip包(https://ftp.pcre.org/pub/pcre/pcre-8.38.zip)
将包下载到本地传到服务器上,并解压到相应目录

[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.16.1.tar.gz  pcre-8.38.zip
[root@localhost ~]# unzip pcre-8.38.zip
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.16.1.tar.gz  pcre-8.38   pcre-8.38.zip
[root@localhost ~]# mv pcre-8.38 /usr/local/src/ 

3、源码编译nginx
(1)下载nginx包,并编译安装

[root@localhost ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
--2019-09-17 09:44:57--  http://nginx.org/download/nginx-1.16.1.tar.gz
Resolving nginx.org (nginx.org)... 62.210.92.35, 95.211.80.227, 2001:1af8:4060:a004:21::e3
...
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.16.1.tar.gz
[root@localhost ~]# tar xf nginx-1.16.1.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.16.1  nginx-1.16.1.tar.gz
[root@localhost ~]# cd nginx-1.16.1 
[root@localhost nginx-1.16.1]#  ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.38 --user=nginx --group=nginx
[root@localhost nginx-1.16.1]# make && make install 

(2)修改nginx的配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
1     #user  nobody;
2     user  nginx;       #在第2行左右添加该行
      ...
44          location / {
45             root   html;
46             index  index.php index.html index.htm;    #在46行左右,添加index.php
47         }
	  ...
65         #   #  在66-72行去掉注释并作如下修改,如图:
66         location ~ \.php$ {
67             root           html;
68             fastcgi_pass   127.0.0.1:9000;
69             fastcgi_index  index.php;
70             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
71             include        fastcgi_params;
72         } 

1.png
(3)创建nginx用户并启动nginx

[root@localhost ~]# useradd -M -s /sbin/nologin nginx 
[root@localhost ~]# /usr/local/nginx/sbin/nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# netstat -nltp |grep nginx
tcp       0      0 0.0.0.0:80       0.0.0.0:*        LISTEN      10968/nginx: master

如果在生产环境下,需要设置开机自启,在此我们不在写入开机自启脚本,如有需要自行查阅编写。
(4)在浏览器访问 — 192.168.184.159
3.png
4、安装数据库(也可以使用源码编译安装,在此我们使用yum直接安装)
(1)安装数据库,并启动

[root@localhost ~]# yum -y install mariadb mariadb-server mysql-devel
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# netstat -nltp |grep mysqld   #若没有netstat,则yum -y install net-tools
tcp       0      0 0.0.0.0:3306        0.0.0.0:*            LISTEN      11262/mysqld

(2)对数据库配置

[root@localhost ~]# mysqladmin -u root password '123'   #使用root用户登录数据库的密码自行设置
[root@localhost ~]# mysql -uroot -p'123'
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin; #建zabbix库
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';                                        #创建zabbix用户,密码为zabbix
MariaDB [(none)]> show databases;       #查看所有库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| zabbix             |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> flush privileges;  
MariaDB [(none)]> \q 
Bye 
[root@localhost ~]# systemctl restart mariadb

6.配置php
(1)安装php中间件,并重启php-fpm

[root@localhost ~]# yum -y install php php-mysql gd php-gd php-fpm
[root@localhost ~]# systemctl start php-fpm          #重启php-fpm
[root@localhost ~]# systemctl enable php-fpm
[root@localhost ~]# netstat -nltp |grep php
tcp      0      0 127.0.0.1:9000       0.0.0.0:*        LISTEN      40847/php-fpm: mast 

(2)在nginx中写入测试页面

[root@localhost ~]# vim /usr/local/nginx/html/a.php
<?php
     phpinfo();
?>
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

(3)在浏览器测试 ---- http://192.168.184.159/a.php
出现以下界面说明php配置成功。
4.png
7、配置zabbix
(1)下载zabbix包,将数据导入到数据库中

[root@localhost ~]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.3/zabbix-3.4.3.tar.gz                                 #下载zabbix-3.4.3数据包
[root@localhost ~]# ls
nginx-1.16.1  nginx-1.16.1.tar.gz  pcre-8.38.zip  zabbix-3.4.3.tar.gz
[root@localhost ~]# tar xf zabbix-3.4.3.tar.gz 
[root@localhost ~]# ls
nginx-1.16.1  nginx-1.16.1.tar.gz  pcre-8.38.zip  zabbix-3.4.3  zabbix-3.4.3.tar.gz
[root@localhost ~]# cd zabbix-3.4.3/
[root@localhost zabbix-3.4.3]# mysql -uzabbix -pzabbix zabbix < database/mysql/schema.sql
[root@localhost zabbix-3.4.3]# mysql -uzabbix -pzabbix zabbix < database/mysql/images.sql
[root@localhost zabbix-3.4.3]# mysql -uzabbix -pzabbix zabbix < database/mysql/data.sql 

(2)创建zabbix的组和用户

[root@localhost zabbix-3.4.3]# groupadd zabbix
[root@localhost zabbix-3.4.3]# useradd -s /sbin/nologin -g zabbix zabbix

(3)安装依赖包,编译安装zabbix

[root@localhost zabbix-3.4.3]# yum install -y net-snmp-devel libevent libevent-devel
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
....
[root@localhost zabbix-3.4.3]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2
[root@localhost zabbix-3.4.3]# make && make install

(4)修改zabbix的配置文件,并启动zabbix

[root@localhost zabbix-3.4.3]# vim /usr/local/zabbix/etc/zabbix_server.conf 
 DBHost=localhost          #该行默认被注释掉,去掉注释(如果是多台服务器,应该写数据库的ip地址)
 DBName=zabbix             #数据库用户,授权的用户是zabbix
 DBUser=zabbix             #授权的用户是 zabbix
 DBPassword=zabbix         #该行默认被注释掉,去掉注释并修改,密码授权是 zabbix
[root@localhost zabbix-3.4.3]# vim /usr/local/zabbix/etc/zabbix_agentd.conf
 Server=127.0.0.1          #默认监控服务器自己,这三行不用修改
 ServerActive=127.0.0.1
 Hostname=Zabbix server
 UnsafeUserParameters=1    #该行默认被注释掉,去掉注释并修改(将0改为1)
[root@localhost zabbix-3.4.3]# /usr/local/zabbix/sbin/zabbix_server     #启动zabbix
[root@localhost zabbix-3.4.3]# netstat -nltp | grep zabbix
tcp        0      0 0.0.0.0:10051        0.0.0.0:*       LISTEN    40682/zabbix_server 

8、将zabbix的页面代码放到发布目录下

[root@localhost ~]# cp -r 、/root/zabbix-3.4.3/frontends/php/*   /usr/local/nginx/html/
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload 
[root@localhost ~]# systemctl restart php-fpm

9、在浏览器页面测试 ---- 192.168.184.159
(1)访问192.168.184.159,出来以下页面
5.png
(2)下一步,但在该页面会出现报错如图:
6.png

解决方法如下:

① php的配置文件修改

[root@localhost ~]# vim /etc/php.ini       #对内容作出如下修改
post_max_size = 16M              #修改(大概在672行)
max_execution_time = 300         #修改(大概在384行)
max_input_time = 300             #修改(大概在394行)
date.timezone = Asia/Shanghai    #此行有注释,去掉注释并修改内容(大概在878行)

② 缺少php的中间件

[root@localhost ~]# yum -y install php-bcmath php-mbstring  php-xml  php-ldap
[root@localhost ~]# systemctl restart php-fpm

(3)在浏览器访问
① 刷新访问的页面发现都变成ok了,并执行下一步
7.png
② 配置数据库(授权密码为zabbix),下一步
8.png
③ 默认,不进行修改
9.png10.png
④ 下一步后的页面会报错如图:(解决方法如图)

11.png

解决方法:(将下载的文件上传到服务器中,此处不列出,可以用rz命令,也可以用工具上传)
[root@localhost ~]# ls
nginx-1.16.1  pcre-8.38.zip  zabbix-3.4.3  zabbix-3.4.3.tar.gz  zabbix.conf.php
[root@localhost ~]# mv zabbix.conf.php /usr/local/nginx/html/conf/
[root@localhost ~]# ls /usr/local/nginx/html/conf/
maintenance.inc.php  zabbix.conf.php  zabbix.conf.php.example

⑤ 刷新刚才的页面,如图即成功,并进行下一步
12.png
⑥ 配置完成,进行登录
13.png
⑦ 登录即可进入zabbix的页面,即实验完成。
14.png
⑧ 修改系统的语言(改为简体中文)
15.png16.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章