在上文中我具体操作了在LAMP架构中搭建Zabbix监控服务,在本篇中我将在LNMP中搭建一个Zabbix服务,操作步骤相比于之前要稍微复杂一些,但也只是在细节上LNMP稍多一些,大体过程还是类似的。
实验环境:
STKAITI下载链接见 http://www.info110.com/Linux/2018-11/155200.htm
操作步骤
一、搭建LNMP
1 安装nginx1.4
[root@promote~]#wget http://nginx.org/packages/CentOS/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@promote ~]# rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm #安装nginx官方源
[root@promote ~]# yum install nginx -y
[root@promote ~]# systemctl start nginx
[root@promote ~]# systemctl enable nginx
[root@promote ~]# netstat -ntap | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3024/nginx: master
在浏览器上访问http://192.168.199.129/
2 安装mysql5.7(这里为了安装方便我也mariadb来代替mysql,功能都是一样的):
[root@promote ~]# yum install mariadb-server mariadb -y #下载mariadb安装包
[root@promote ~]# systemctl start mariadb.service
[root@promote ~]# systemctl enable mariadb.service
[root@promote ~]# mysql_secure_installation #配置mariadb相关参数
Set root password? [Y/n] y
Remove anonymous users? [Y/n] n
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] n
Reload privilege tables now? [Y/n] y
3 安装PHP7.2
[root@promote~]#rpm -Uvh https://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@promote ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm #下载PHPyum源
[root@promote ~]# yum install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql -y #安装PHP相关服务
[root@promote ~]# php -v #查看版本
PHP 7.2.10 (cli) (built: Sep 15 2018 07:10:58) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
4 配置nginx支持PHP:
[root@promote ~]# vim /etc/php-fpm.d/www.conf
8 user = nginx #将Apache改为nginx
10 group = nginx #将Apache改为nginx
5 配置PHP:
[root@promote ~]# vim /etc/php.ini
359 expose_php = Off #隐藏PHP版本
368 max_execution_time = 300 #监控执行时间
378 max_input_time = 300 #接收数据等待时间
389 memory_limit = 128M #每个脚本占用内存
656 post_max_size = 16M #POST数据大小
799 upload_max_filesize = 2M #下载文件大小
800 always_populate_raw_post_data = -1 #可以用$HTTP_RAW_POST_DATA接收post raw data
date.timezone = Asia/Shanghai #将时区设为上海时区
6 配置PHP请求被传送到后端的php-fpm模块,默认情况下php配置模块是被注释掉的:
[root@promote ~]# vim /etc/nginx/conf.d/default.conf
10 index index.php index.html index.htm;
30 location ~ .php$ {
31 root /usr/share/nginx/html;
32 fastcgi_pass 127.0.0.1:9000;
33 fastcgi_index index.php; 34 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_scr ipt_name; 35 include fastcgi_params; 36 } #把fastcgi_param中的/scripts改为$document_root,root是配置php程序用户
7 开启服务
[root@promote ~]# systemctl start php-fpm.service
[root@promote ~]# systemctl enable php-fpm.service
[root@promote ~]# systemctl restart nginx
[root@promote ~]# vim /usr/share/nginx/html/index.php #创建PHP测试首页
<?php
phpinfo();
?>
在浏览器上访问http://192.168.199.129/index.php
测试PHP能否连接数据库:
[root@promote html]# vim index.php
<?php
$link=mysqli_connect(‘127.0.0.1′,’root’,’123123′);
if ($link) echo “true”;
else echo “false”;
?>
8 创建zabbix数据库与zabbix用户:
[root@promote html]# mysql -u root -p
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
MariaDB [(none)]> grant all privileges on *.* to ‘zabbix’@’%’ identified by ‘123123’;
MariaDB [(none)]> flush privileges;
解决本地无法登陆问题:
[root@promote ~]# mysql -u zabbix -p #现在用zabbix用户是无法登陆数据库的,因为有空用户在进行占用
Enter password:
ERROR 1045 (28000): Access denied for user ‘zabbix’@’localhost’ (using password: YES)
MariaDB [(none)]> select user,host from mysql.user;
+——–+————————-+
| user | host |
+——–+————————-+
| zabbix | % |
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | promote.cache-dns.local |
| root | promote.cache-dns.local |
+——–+————————-+
7 rows in set (0.00 sec)
MariaDB [(none)]> drop user ”@’localhost’; #删除库中的空用户
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> drop user ”@’promote.cache-dns.local’; #删除空用户
Query OK, 0 rows affected (0.00 sec)
测试zabbix用户能否连接PHP:
[root@promote ~]# vim /usr/share/nginx/html/index.php
<?php
$link=mysqli_connect(‘127.0.0.1′,’zabbix’,’123123′);
if ($link) echo “true”;
else echo “false”;
?>
在浏览器上访问http://192.168.199.129/index.php
到此为止LNMP架构就搭建完成了,下面开始部署zabbix服务
二、部署zabbix server:
[root@promote~]#rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[root@promote ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y
[root@promote ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix #将zabbix脚本文件导入到zabbix数据库中
[root@promote ~]# mysql -u zabbix -p
MariaDB [(none)]> use zabbix
MariaDB [zabbix]> show tables; #查看zabbix数据库中的表,如果脚本导入成功,就应该会看到很多表
修改zabbix配置文件,需要修改以下内容:
[root@promote ~]# grep -n ‘^'[a-Z] /etc/zabbix/zabbix_server.conf
38:LogFile=/var/log/zabbix/zabbix_server.log
49:LogFileSize=0
72:PidFile=/var/run/zabbix/zabbix_server.pid
82:SocketDir=/var/run/zabbix
91:DBHost=localhost #去掉注释
101:DBName=zabbix
117:DBUser=zabbix
125:DBPassword=123123 #修改密码
357:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
475:Timeout=4
518:AlertScriptsPath=/usr/lib/zabbix/alertscripts
529:ExternalScripts=/usr/lib/zabbix/externalscripts
565:LogSlowQueries=3000
修正图表中文乱码:
[root@promote ~]# vim /usr/share/zabbix/include/defines.inc.php
[root@promote zabbix]# cp STKAITI.TTF /usr/share/zabbix/fonts/ #从微软系统下复制相应的字体文件到/usr/share/zabbix/fonts/
赋予文件权限:
[root@promote ~]# cp -r /usr/share/zabbix/ /usr/share/nginx/html/
[root@promote ~]# chown -R zabbix.zabbix /etc/zabbix/
[root@promote ~]# chown -R zabbix.zabbix /usr/share/nginx/
[root@promote ~]# chown -R zabbix.zabbix /usr/lib/zabbix/
[root@promote ~]# chmod -R 755 /etc/zabbix/web/
[root@promote ~]# chmod -R 777 /var/lib/php/session/
启动服务:
[root@promote ~]# systemctl start zabbix-server.service
[root@promote ~]# systemctl enable zabbix-server.service
[root@promote ~]# systemctl start zabbix-agent.service
[root@promote ~]# systemctl enable zabbix-agent.service
[root@promote ~]# systemctl restart php-fpm.service
[root@promote ~]# systemctl restart nginx #这两个服务一定要重启,不然在登陆zabbix监控页面时会出错
在浏览器上登录zabbix界面:
版本显示为4.0
这里必须每项都显示OK才可以,如果有报错请重新启动PHP,nginx和zabbix服务,如果还有问题,请检查三个服务的配置文件
接着填写密码即可,密码就是zabbix登录数据库密码,端口保持默认即可
接着填写zabbix的服务器名称,这里我就定义为zabbix
接着点击下一步就会显示zabbix的完整信息
接着点击下一步就会看到这个信息,是说没办法创建配置文件,这时候点击图中的下载链接下载这个文件,接着将这个文件挂载到zabbix服务器上,具体操作如下:
点击完成就会发现报错消失了
使用账户密码登录zabbix,默认账号密码为‘Admin’和‘zabbix’
接着就会看到zabbix的监控页面
三、配置被监控服务器:
[root@promote ~]# systemctl stop firewalld.service
[root@promote ~]# setenforce 0
下载yum源:
[root@promote~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[root@promote ~]# yum install zabbix-agent -y
更改zabbix配置文件,需要更改以下内容:
[root@promote ~]# grep -n ‘^'[a-Z] /etc/zabbix/zabbix_agentd.conf
13:PidFile=/var/run/zabbix/zabbix_agentd.pid
32:LogFile=/var/log/zabbix/zabbix_agentd.log
43:LogFileSize=0
98:Server=192.168.199.129 #地址指向zabbix监控端
139:ServerActive=192.168.199.129 #地址指向zabbix监控端
150:Hostname=test #定义服务器名称
268:Include=/etc/zabbix/zabbix_agentd.d/*.conf
开启服务:
[root@promote ~]# systemctl start zabbix-agent.service
[root@promote ~]# systemctl enable zabbix-agent.service
[root@promote ~]# netstat -ntap | grep 10050 #查看端口,注意被监控端端口是10050
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 38128/zabbix_agentd
tcp6 0 0 :::10050 :::* LISTEN 38128/zabbix_agentd
到此为止,在LNMP上搭建zabbix服务已经完成,要想监控到其他服务器还需要在浏览器的监控页面上手动添加,添加过程我在上篇博客“在LAMP架构上搭建Zabbix监控服务”已经详细讲过,这里就不再操作。