LNMP架构基础上zabbix监控平台部署!
主机 | IP地址 | 部署服务 |
---|---|---|
server | 192.168.233.136 | LNMP、zabbix |
client | 192.168.233.137 | zabbix代理 |
关闭防火墙、网络管理、主机名修改(server、client),仅展示server操作
[root@192 ~]# hostnamectl set-hostname server
[root@192 ~]# su
[root@server ~]#
[root@server ~]# systemctl stop firewalld && systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@server ~]# setenforce 0 && sed -i "s/SELINUX=*/SELINUX=disabled/g" /etc/selinux/config
[root@server ~]# systemctl stop NetworkManager && systemctl disable NetworkManager
Removed symlink /etc/systemd/system/multi-user.target.wants/NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.NetworkManager.service.
Removed symlink /etc/systemd/system/dbus-org.freedesktop.nm-dispatcher.service.
Removed symlink /etc/systemd/system/network-online.target.wants/NetworkManager-wait-online.service.
nagios是一款比较老的监控工具,但是因为插件太多,不是主流
cacti是一款图形化的监控分析工具
zabbix是基于web界面的提供分布式功能的监控工具,监控上线是300-500台
zabbix可以监控网络设备(例如思科、华为等)、服务器(例如IBM、联想、戴尔等)、应用监控(Apache、MQ、中间件、Tomcat等)和服务(HTTP、https、nginx、ssl等)等监控
zabbix使用的最稳定的版本是4.0,LTS是指测试,5.0版本在官网下载的时候比4.0版本多了一个nginx服务器的选择
ajax:异步刷新
zcat:不解压直接查看压缩包
1、安装nginx
[root@server ~]# wget
http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm '//下载nginx软件包'
[root@server ~]# vim /etc/yum.repos.d/nginx.repo '//编辑yum源'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@server ~]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: base extras nginx-stable updates
Cleaning up list of fastest mirrors
[root@server ~]# yum makecache '//建立yum缓存'
[root@server ~]# systemctl start nginx
[root@server ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
网页测试:发现nginx搭建成功
2、安装mysql(mariadb)
[root@server ~]# yum -y install mariadb-server mariadb
[root@server ~]# systemctl start mariadb.service
[root@server ~]# systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@server ~]# netstat -ntap |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 95170/mysqld
'//接下来执行mysql安全配置向导'
[root@server ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): '//初次运行直接回车'
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y '//y。设置密码'
New password: '//输入密码'
Re-enter new password: '//重复输入一次'
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y '//是否删除匿名用户,建议y删除'
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y '//是否禁止root远程登录,建议y禁止'
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] n '//是否删除test数据库,根据实际情况'
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y '//是否重新加载权限表,建议y'
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@server ~]# mysql -uroot -p '//测试登陆mysql数据库'
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> exit
Bye
3、安装PHP
[root@server ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm '//升级rpm包'
获取https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
警告:/var/tmp/rpm-tmp.xsEbUC: 头V3 RSA/SHA256 Signature, 密钥 ID 352c64e5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:epel-release-7-12 ################################# [100%]
[root@server ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
获取https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
警告:/var/tmp/rpm-tmp.YidqBL: 头V4 RSA/SHA1 Signature, 密钥 ID 62e74ca5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:webtatic-release-7-3 ################################# [100%]
[root@server ~]# cd /etc/yum.repos.d/
[root@server yum.repos.d]# yum -y install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql '//安装PHP环境工具包'
[root@server yum.repos.d]# php -v '//查看版本'
PHP 7.2.27 (cli) (built: Jan 26 2020 15:49:49) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
4、配置php-fpm.d模块的配置文件与nginx配置文件并关联
[root@server yum.repos.d]# vim /etc/php-fpm.d/www.conf
user = nginx '//8行将apache修改为nginx'
; RPM: Keep a group allowed to write in log dir.
group = nginx '//10行将apache修改为nginx'
[root@server yum.repos.d]# vim /etc/nginx/conf.d/default.conf '//修改nginx配置文件与php相关联'
index index.php index.html index.htm; '//第10行添加index.php文件'
...省略内容 '//下方是30-36行的字段,为配置php请求被传送到后端的php-fpm模块的配置'
location ~ \.php$ {
root /usr/share/nginx/html; '//修改站点目录'
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php; 34 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; '//将fastcgi_param中的/scripts改为$document_root,root是配置php程序纺织的根目录'
include fastcgi_params;
}
5、优化PHP配置文件
[root@server yum.repos.d]# vim /etc/php.ini
short_open_tag = On '//202行改为On,支持php短标签'
expose_php = Off '//359修改为Off,隐藏php版本'
'//以下都是zabbix的配置要求'
max_execution_time = 300 '//执行时间,在一个程序执行的过程中能够等待的执行时间,执行时间过程中如果没有执行完会结束该程序,以防出现卡死,默认30秒'
max_input_time = 300 '//378行,接受数据的等待时间'
memory_limit = 128M '//389行,每个脚本的占用内存限制'
post_max_size = 16M '//656行,post数据的最大限制'
upload_max_filesize = 2M '//799,下载文件的大小限制'
always_populate_raw_post_data = -1 '//800行添加此句,可以用$HTTP_RAW_POST_DATA接受post raw data(原始未处理数据)'
date.timezone = Asia/Shanghai '//878行,修改时区为上海'
[root@server yum.repos.d]# systemctl start php-fpm
[root@server yum.repos.d]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@server yum.repos.d]# netstat -ntap |grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 96130/php-fpm: mast
[root@server yum.repos.d]# systemctl restart nginx
6、编写PHP测试首页并测试网页
[root@server yum.repos.d]# vim /usr/share/nginx/html/info.php
测试连接数据库
[root@server yum.repos.d]# vim /usr/share/nginx/html/info.php '//重新修改首页文件'
'//注:mysql_connetct扩展从PHP 5.5.0起开始废弃,改用mysqli或pdo_mysql'
7、mysql数据库创建zabbix用户
[root@server yum.repos.d]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> GRANT all privileges ON *.* TO 'zabbix'@'%' IDENTIFIED BY 'admin123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit
Bye
'//collate的作用:对于mysql中的那些字符类型的列,如varchar,char,text类型的列,都需要有一个collate类型来告知mysql如何对该列进行排序和比较'
[root@server yum.repos.d]# mysql -uzabbix -p '//先测试数据库是否能登陆'
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select user,host from mysql.user; '//如果报1045的error,是因为此处有空用户占用导致本地无法登陆,解决办法是删除相应的空用户:drop user ''@localhost;然后flush privileges
+--------+-----------+
| user | host |
+--------+-----------+
| zabbix | % |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+--------+-----------+
4 rows in set (0.00 sec)
重新修改PHP首页文件并测试
[root@server yum.repos.d]# vim /usr/share/nginx/html/info.php
1、安装数据库、Zabbix server,Web前端,agent
[root@server yum.repos.d]# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
获取https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.i4nVQE: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:zabbix-release-4.0-2.el7 ################################# [100%]
[root@server yum.repos.d]# yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent '//因为网络问题,需要多次重试才可下载完成'
2、导入初始架构和数据,系统将提示您输入新创建的密码。
[root@server yum.repos.d]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
Enter password:
[root@server yum.repos.d]# mysql -uzabbix -p '//登陆数据库查看导入的数据'
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 5.5.65-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| zabbix |
+--------------------+
5 rows in set (0.01 sec)
MariaDB [(none)]> use zabbix;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [zabbix]> show tables; '//会发现导入的数据'
'//慢日志:记录了执行速度特别慢的SQL语句'
3、优化zabbix配置文件并修改图表中文乱码
[root@server yum.repos.d]# vim /etc/zabbix/zabbix_server.conf
DBHost=localhost '//91行取消注释'
DBPassword=admin123 '//124行设置密码'
[root@server yum.repos.d]# vim /usr/share/zabbix/include/defines.inc.php
'//:% s /graphfont/kaiti/g末行模式修改'
[root@server yum.repos.d]# cd /usr/share/zabbix
[root@server zabbix]# mkdir fonts
[root@server zabbix]# cd fonts
[root@server fonts]# rz -E
rz waiting to receive.
[root@server fonts]# ls
STKAITI.TTF '//上传相应字体到指定目录'
4、给权限,开启相关zabbix服务
[root@server fonts]# cp -r /usr/share/zabbix/ /usr/share/nginx/html/
[root@server fonts]# chown -R zabbix:zabbix /etc/zabbix
[root@server fonts]# chown -R zabbix:zabbix /usr/share/nginx/
[root@server fonts]# chown -R zabbix:zabbix /usr/lib/zabbix/
[root@server fonts]# chmod -R 755 /etc/zabbix/web/
[root@server fonts]# chmod -R 777 /var/lib/php/session/
[root@server fonts]# systemctl start zabbix-server.service
[root@server fonts]# systemctl enable zabbix-server.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.
[root@server fonts]# systemctl start zabbix-agent.service
[root@server fonts]# systemctl enable zabbix-agent.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@server fonts]# netstat -ntap |grep 10051
tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 97671/zabbix_server
tcp 0 0 127.0.0.1:10051 127.0.0.1:51692 TIME_WAIT -
tcp6 0 0 :::10051 :::* LISTEN 97671/zabbix_server
[root@server fonts]# systemctl restart php-fpm.service
[root@server fonts]# systemctl restart nginx
登陆网页操作:192.168.233.136/zabbix,用户Admin,密码zabbix
[root@server fonts]# cd /etc/zabbix/web/
[root@server web]# ls
maintenance.inc.php
[root@server web]# rz -E
rz waiting to receive.
[root@server web]# ls
maintenance.inc.php zabbix.conf.php
[root@server web]# chown zabbix.zabbix /etc/zabbix/web/zabbix.conf.php [root@server web]# systemctl restart zabbix-server.service
密码是zabbix
1、安装yum源与zabbix代理
[root@client ~]# rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
获取https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm
警告:/var/tmp/rpm-tmp.Qwfdol: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:zabbix-release-4.0-2.el7 ################################# [100%]
[root@client ~]# yum -y install zabbix-agent
2、修改zabbix代理配置文件并开启服务
[root@client ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.233.136 '//98行指向监控服务器地址'
ServerActive=192.168.233.136 '//139行指向监控服务器地址'
Hostname=Zabbix-test '//150行修改名称'
[root@client ~]# systemctl start zabbix-agent.service
[root@client ~]# systemctl enable zabbix-agent.service
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
[root@client ~]# netstat -ntap |grep zabbix
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 82119/zabbix_agentd
tcp6 0 0 :::10050 :::* LISTEN 82119/zabbix_agentd