Centos7搭建zabbix4.4监控-LNMP

一、搭建LNMP

[root@zzxt ~]# systemctl stop firewalld.service 
[root@zzxt ~]# setenforce 0

1.1 配置yum源

#阿里yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#Epel(Extra Packages for Enterprise Linux)镜像
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#zabbix镜像
vi /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/$basearch/debuginfo/
enabled=0
gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=1

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX
gpgcheck=1
#清除缓存
yum clean all
#生成缓存
yum makecache

1.2安装并配置nginx

安装nginx

[root@zzxt ~]# yum -y install nginx 
[root@zzxt ~]# systemctl start nginx
[root@zzxt ~]# systemctl enable nginx
[root@zzxt ~]# netstat -ntpl | grep 80

配置PHP请求被传送到后端的php-fpm模块,默认php配置模块是在default文件中,nginx.conf中添加以下配置:

[root@zzxt ~]# vim /etc/nginx/nginx.conf
        index  index.php index.html index.htm;
     location ~ \.php$ {
    
    
         root          /usr/share/nginx/html;
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php; 
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; 
         include        fastcgi_params; 
     } 

在浏览器上访问http://ip/,进行测试

1.3安装并配置mariadb

安装并初始化

[root@zzxt ~]# yum install -y mariadb-server mariadb 
[root@zzxt ~]# systemctl start mariadb.service
[root@zzxt ~]# systemctl enable mariadb.service 
[root@zzxt ~]# mysql_secure_installation       #配置mariadb参数
Enter current password for root (enter for none):  # 输入数据库管理员的密码,第一次进入直接回车
Set root password? [Y/n] y
New password:
Re-enter new password:
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

创建zabbix数据库与zabbix用户:

[root@zzxt 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;
MariaDB [(none)]> quit;

设置MariaDB字符集为utf-8

vi /etc/my.cnf
#在[mysqld]标签下添加
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

vi /etc/my.cnf.d/client.cnf
#在[client]标签下添加
default-character-set=utf8

vi /etc/my.cnf.d/mysql-clients.cnf
#在[mysql]标签下添加
default-character-set=utf8

#查看MariaDB字符集
MariaDB [(none)]> show variables like "%character%";show variables like "%collation%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_unicode_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

解决本地无法登陆问题:

[root@zzxt ~]# mysql -u zabbix -p    #有空用户在进行占用使zabbix用户是无法登陆数据库
Enter password: 
ERROR 1045 (28000): Access denied for user 'zabbix'@'localhost' (using password: YES)
[root@zzxt ~]# mysql -uroot -p
#省略密码输入部分
MariaDB [(none)]>  select user,host from mysql.user;
+--------+-----------+
| user   | host      |
+--------+-----------+
| zabbix | %         |
| root   | 127.0.0.1 |
| root   | ::1       |
|        | localhost |
| root   | localhost |
|        | zzxt      |
| root   | zzxt      |
+--------+-----------+
7 rows in set (0.00 sec)


MariaDB [(none)]> drop user ''@'localhost';    #删除库中的空用户
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>drop user ''@'zzxt';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit;
[root@zzxt ~]# mysql -u zabbix -p  #测试

1.4 安装配置PHP7.2

[root@zzxt~]#rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@zzxt ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm    #下载PHPyum源
[root@zzxt ~]# yum install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql -y    #安装PHP等关联包
[root@zzxt ~]# php -v     #查看版本
PHP 7.2.32 (cli) (built: Aug 23 2020 18:46:58) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

配置nginx支持PHP:

[root@zzxt ~]# vim /etc/php-fpm.d/www.conf
 user = nginx     #将Apache改为nginx
 group = nginx   #将Apache改为nginx

配置PHP:

[root@zzxt ~]# vim /etc/php.ini
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    #下载文件大小
877 date.timezone = Asia/Shanghai    #将时区设为上海时区

开启服务

[root@zzxt ~]# systemctl start php-fpm.service 
[root@zzxt ~]# systemctl enable php-fpm.service 
[root@zzxt ~]# systemctl restart nginx
[root@zzxt ~]# vim /usr/share/nginx/html/index.php    #创建PHP测试首页
<?php
  phpinfo();
?>

在浏览器上访问http://ip/index.php

测试PHP能否连接数据库:

[root@zzxt html]# vim index.php 
<?php
$link=mysqli_connect('127.0.0.1','root','qqq');
 if ($link) echo "root connect to db successfully!!";
 else echo "false";
?>

在浏览器上访问http://ip/index.php
测试zabbix用户能否连接PHP:

[root@zzxt ~]# vim /usr/share/nginx/html/index.php 
<?php
$link=mysqli_connect('127.0.0.1','zabbix','qqq');
 if ($link) echo "zabbix connect to db successfully!!";
 else echo "false";
?>

在浏览器上访问http://ip/index.php

到此为止LNMP架构就搭建完成了,下面开始部署zabbix服务

二、部署zabbix server:

安装zabbix

[root@zzxt ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y

导入初始表

[root@zzxt ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix     #将zabbix脚本文件导入到zabbix数据库中
[root@zzxt ~]# mysql -u zabbix -p
MariaDB [(none)]> use zabbix
MariaDB [zabbix]> show tables;    #查看zabbix数据库中的表,如果脚本导入成功,就应该会看到很多表

修改zabbix配置文件

[root@zzxt ~]# 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         #去掉注释
100:DBName=zabbix
116:DBUser=zabbix
124:DBPassword=qqq     #修改密码
356:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
473:Timeout=4
516:AlertScriptsPath=/usr/lib/zabbix/alertscripts
527:ExternalScripts=/usr/lib/zabbix/externalscripts
563:LogSlowQueries=3000

修正图表中文乱码:

1)从window机器上C:\Windows\Fonts\下选择字体文件如:msyh.ttf #微软雅黑
上传到zabbix服务器,路径为/usr/share/zabbix/assets/fonts, 注意字体文件名为小写
2)修改 /usr/share/zabbix/include/defines.inc.php,替换graphfont为msyh

[root@zzxt ~]# vi /usr/share/zabbix/include/defines.inc.php
:%s /graphfont/msyh/g   //全局替换模式
#替换内容为:
define('ZBX_GRAPH_FONT_NAME',           'msyh'); // font file name
define('ZBX_FONT_NAME', 'msyh');

赋予文件权限:

[root@zzxt ~]# cp -r /usr/share/zabbix/ /usr/share/nginx/html/
[root@zzxt ~]# chown -R zabbix.zabbix /etc/zabbix/
[root@zzxt ~]# chown -R zabbix.zabbix /usr/share/nginx/
[root@zzxt ~]# chown -R zabbix.zabbix /usr/lib/zabbix/
[root@zzxt ~]# chmod -R 755 /etc/zabbix/web/
[root@zzxt ~]# chmod -R 777 /var/lib/php/session/

启动服务:

[root@zzxt ~]# systemctl start zabbix-server.service 
[root@zzxt ~]# systemctl enable zabbix-server.service 
[root@zzxt ~]# systemctl start zabbix-agent.service 
[root@zzxt ~]# systemctl enable zabbix-agent.service 
[root@zzxt ~]# systemctl restart php-fpm.service 
[root@zzxt ~]# systemctl restart nginx       #这两个服务一定要重启,不然在登陆zabbix监控页面时会出错

在浏览器上登录zabbix界面:
http://ip/zabbix/setup.php
下一步
这里必须每项都显示OK才可以,如果有报错请重新启动PHP,nginx和zabbix服务,如果还有问题,请检查三个服务的配置文件
下一步
填写密码即可,密码就是zabbix登录数据库密码,端口保持默认即可
下一步
填写zabbix的服务器名称
下一步
显示zabbix的完整信息
下一步
看到提示信息,是说没办法创建配置文件,这时候点击图中的下载链接下载这个文件,接着将这个文件挂载到zabbix服务器上/etc/zabbix/web下,然后点击完成就会发现报错消失了
下一步
使用账户密码登录zabbix,默认账号密码为‘Admin’和‘zabbix’
下一步
接着就会看到zabbix的监控页面

三、被监控服务器:

[root@zzxt ~]# systemctl stop firewalld.service 
[root@zzxt ~]# setenforce 0

配置yum源(上边有)
安装agent

[root@zzxt ~]# yum install zabbix-agent -y

更改zabbix配置文件,需要更改以下内容:

vi /etc/zabbix/zabbix_agentd.conf
Server=zabbix服务器ip
ServerActive=zabbix服务器ip
Hostname=本机的主机名称

开启服务:

[root@zzxt ~]# systemctl start zabbix-agent.service 
[root@zzxt ~]# systemctl enable zabbix-agent.service 
[root@zzxt ~]# netstat -ntlp | grep 10050 
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      45620/zabbix_agentd 
tcp6       0      0 :::10050                :::*                    LISTEN      45620/zabbix_agentd 

到此为止,在LNMP上搭建zabbix服务已经完成,要想监控到其他服务器还需要在浏览器的监控页面上手动添加

猜你喜欢

转载自blog.csdn.net/losersnake/article/details/108711136