LNMP+zabbix实战部署
一、服务端配置(提供监控服务)
'下载包'
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
'解压包'
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
'手动创建nginx yum安装源'
vim /etc/yum.repos.d/nginx.repo
#!/bin/bash
# nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
yum list
'yum安装nginx'
yum install nginx -y
'重启并设置开机自启动nginx'
systemctl start nginx
systemctl enable nginx
firewall-cmd --permanent --add-service=http --zone=public
firewall-cmd --reload
'安装mysql'
yum install mariadb-server mariadb -y
systemctl start mariadb.service
systemctl enable mariadb.service
mysql_secure_installation
空格
y
n
n
n
y
'安装php'
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
或
yum install epel-release
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql
'查看php版本'
php -v
'配置nginx支持php'
vim /etc/php-fpm.d/www.conf 把apache改成nginx
==第8行修改==
user = nginx
==第10行修改==
group = nginx
vim /etc/nginx/conf.d/default.conf
==第10行修改==
index index.php index.html index.htm
==第30-36行修改添加==
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;
}
'配置php'
vim /etc/php.ini
==第202行修改==
short_open_tag = On
支持php短标签
==第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
==第877行取消注释修改==
date.timezone = Asia/Shanghai
选择时区
systemctl start php-fpm
systemctl enable php-fpm
systemctl restart nginx
测试首页
vim /usr/share/nginx/html/info.php
<?php
phpinfo();
?>
http://192/168.90.10/info.php
测试连接数据库
vim /usr/share/nginx/html/info.php
<?php
$link=mysqli_connect('127.0.0.1','root','123123');
if($link)echo"连接成功!";
else echo"连接失败!";
?>
注意:mysql_connect扩展自PHP5.5.0起已废弃,改用mysqli或pdo_mysql
mysql -uroot -p
CREATE DATABASE zabbix character set utf8 collate utf8_bin;
GRANT all privileges ON *.* TO 'zabbix'@'%'IDENTIFIED BY 'admin123';
flush privileges;
collate的作用
对于mysql中那些字符类型典型的列,如VARCHAR,CHAR,TEXT类型的列,都需要有一个COLLATE类型来告知mysql如何对该列进行排序和比较,COLLATE会影响到
<?php
$link=mysqli_connect('127.0.0.1','zabbix','admin123');
if($link)echo"Zabbix数据库连接成功!";
else echo "Zabbix数据库连接失败";
?>
解决本地无法登录问题(连接成功可忽略)
select user,host from mysql.user;
drop user ''@localhost;
drop user ''@cacti;
flush privileges;
rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y
zcat /usr/share/doc/zabbix-server-mysql-4.0.30/create.sql.gz | mysql -uzabbix -p admin123 admin123
vim /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=admin123 #注释去掉,zabbix的密码
==第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
-------------------可选操作-------------------
优化字体 ,牵涉到图表中文乱码的问题
vim /usr/share/zabbix/include/defines.inc.php
将字体替换为kaiti
:%s /graphfont/kaiti/g也可使用全文替换
----------------------------------------------
mkdir /usr/share/zabbix/fonts
cp -r /usr/share/zabbix/ /usr/share/nginx/html/
chown -R zabbix:zabbix /etc/zabbix/
chown -R zabbix:zabbix /usr/share/nginx/
chown -R zabbix:zabbix /usr/lib/zabbix/
chmod -R 755 /etc/zabbix/web/
chmod -R 777 /var/lib/php/session/
systemctl start zabbix-server.service
systemctl enable zabbix-server.service
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service
systemctl restart php-fpm.service
systemctl restart nginx
-------------------安装过程会出现报错-------------------
解决方法:
根据提示点击下载配置文件zabbix.con.php保存到/etc/zabbix/web/
chown zabbix.zabbix /etc/zabbix/web/zabbix.conf.php
systemctl restart zabbix-server.service
--------------------------------------------------------
-------------------默认用户名密码-----------------------
用户名:Admin
密码:zabbix
--------------------------------------------------------
二、代理端配置(被监控服务)
清空防火墙规则,关闭核心防护
iptables -F
setenforce 0
关闭网络管理
systemctl stop NetworkManager
systemctl disable NetworkManager
下载源并安装zabbix-agent
rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum install -y zabbix-agent
修改配置文件
vim /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.11.25 #指向监控服务器
ServerActive=192.168.11.25 #指问监控服务器
Hostname=test1 #指定名称
Include=/etc/zabbix/zabbix_agentd.d/*.conf
systemctl enable zabbix-agent.service
systemctl restart zabbix-agent.service
=================监控报警机制====================
yum install mailx
vim /etc/mail.rc
末行添加
set from=lfp_0315@163.com
set smtp=smtp.163.com
set smtp-auth-user=lfp_0315@163.com
set smtp-auth-password=YEQQJTYIYIYPNAAO
set smtp-auth=login
测试发送
echo "hello world" | mail -s "Hosts-01" lfp_0315@163.com
cd /usr/lib/zabbix/alertscripts
vim mailx.sh
#!/bin/bash
#send mail
messages=`echo $3 | tr '\r\n' '\n'`
subject=`echo $2 | tr '\r\n' '\n'`
echo "${messages}" | mail -s "${subject}" $1 >> /tmp/mailx.log 2>&1
touch /tmp/mailx.log
chown -R zabbix.zabbix /tmp/mailx.log
chmod +x /usr/lib/zabbix/alertscripts/mailx.sh
chown -R zabbix.zabbix /usr/lib/zabbix/
测试脚本
./mailx.sh lfp_0315@163.com "HOSTS-01" "BOSE"
创建报警机制在WEB上创建
管理--》报警媒体类型--》创建媒体类型
名称:
类型:脚本
脚本名称:mailx.sh
脚本参数:(增加三个)
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
管理--》用户--》点击Admin --》报警媒介
类型:mailx 调用上面的脚本
收件人:lfp_0315@163.com
其他选项默认保存
配置--》动作--》创建动作--》删除默认标签,修改触发条件
名称:mailx
条件:A 主机群组=Linux servers
操作--》
默认操作步骤持续时间60
默认标题:{TRIGGER.STATUS}:{TRIGGER.NAME}
默认信息:
告警主机:{HOST.NAME}
告警IP:{HOST.IP}
告警时间:{EVENT.DATE}-{EVENT.TIME}
告警等级:{TRIGGER.SEVERITV}
告警信息:{TRIGGER.NAME}:{ITEM.VALUE}
事件ID:{EVENT.ID}
操作细节--》
操作类型:发送消息
发送到用户:Admin(Zabbix Administrator)
仅送到:Mail-Test
恢复操作:{TRIGGER.STATUS}:{TRIGGER.NAME}
恢复信息:
恢复主机:{HOST.NAME}
恢复IP:{HOST.IP}
恢复时间:{EVENT.DATE}-{EVENT.TIME}
恢复等级:{TRIGGER.SEVERITV}
恢复信息:{TRIGGER.NAME}:{ITEM.VALUE}
恢复ID:{EVENT.ID}
操作细节--》
同上
systemctl restart zabbix-server
systemctl restart zabbix-agent
测试警报
systemctl stop zabbix-agent