Centos7搭建zabbix4.4監控-LNMP

2020-09-23 12:01:15

一、搭建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服務已經完成,要想監控到其他伺服器還需要在瀏覽器的監控頁面上手動新增