LNMP搭建的操作请参照LNMP分离搭建,只是都安装在一台服务器
环境说明
服务器 | ip地址 | 应用 | 操作系统 |
---|---|---|---|
LNMP zabbix服务端 |
192.168.199.179 | nginx mysql php |
centos7/redhat7 |
zabbix客户端 | 192.168.199.185 | zabbix | centos7/redhat7 |
检查所有接口开启
[root@lihuaixin ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
修改nginx配置文件
[root@lihuaixin ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log; #取消此行备注,开启错误日志
#error_log logs/error.log notice;
#error_log logs/error.log info;
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #取消以下几行备注
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
location / {
root html;
index index.php index.html index.htm; #此行加上index.php
}
#取消以下行的注释,并修改
location ~ \.php$ {
root 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网页信息
[root@lihuaixin ~]# cd /usr/local/nginx/html/
[root@lihuaixin html]# vim index.php
修改hosts文件
可在分离博客查看
接下来安装zabbix
参考zabbix部署
修改/etc/php.ini的配置并重启php-fpm
[root@lihuaixin ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@lihuaixin ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@lihuaixin ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@lihuaixin ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@lihuaixin ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm done
[root@lihuaixin html]# cd /usr/src/zabbix-4.0.3
[root@lihuaixin zabbix-4.0.3]# mkdir /usr/local/nginx/html/zabbix
[root@lihuaixin zabbix-4.0.3]# cp -a frontends/php/* /usr/local/nginx/html/zabbix/
[root@lihuaixin zabbix-4.0.3]# chown -R zabbix.zabbix /usr/local/nginx/html
修改nginx
[root@lihuaixin zabbix-4.0.3]# cd /usr/local/nginx/conf/
[root@lihuaixin conf]# vim nginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index zabbix index.php index.html index.htm; #加入zabbix
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index zabbix; #改为zabbix
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
设置zabbix/conf目录的权限,让zabbix有权限生成配置文件zabbix.conf.php
[root@lihuaixin html]# chmod 777 /usr/local/nginx/html/zabbix/conf
重启服务
[root@lihuaixin html]# nginx -s reload
[root@lihuaixin html]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 *:10051 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
web界面配置参照zabbix搭建
恢复权限
[root@lihuaixin html]# chmod 775 /usr/local/nginx/html/zabbix/conf
安装依赖包
[root@qiaobenhuannai ~]# yum -y install net-snmp-devel libevent-devel
下载zabbix包
[root@qiaobenhuannai src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
[root@qiaobenhuannai src]# ls
debug kernels zabbix-4.0.3.tar.gz
[root@qiaobenhuannai src]# tar xf zabbix-4.0.3.tar.gz
创建zabbix用户和组
[root@qiaobenhuannai ~]# groupadd -r zabbix
[root@qiaobenhuannai ~]# useradd -r -M -s /sbin/nologin -g zabbix zabbix
配置安装zabbix
[root@qiaobenhuannai zabbix-4.0.3]# ./configure --enable-agent
[root@qiaobenhuannai zabbix-4.0.3]# make install
[root@qiaobenhuannai ~]# ls /usr/local/etc/
zabbix_agentd.conf zabbix_agentd.conf.d
修改配置文件
修改以下配置:
[root@qiaobenhuannai ~]# vim /usr/local/etc/zabbix_agentd.conf
Server=192.168.199.179
#修改为服务端ip
ServerActive=192.168.199.179
#主动模式
Hostname=777
#此主机名不是客户端主机名
启动zabbix_agentd
[root@qiaobenhuannai ~]# zabbix_agentd
[root@qiaobenhuannai ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
这里监控nginx的状态的时候需要在配置文件里添加一下内容:
[root@lihuaixin ~]# vim /usr/local/nginx/conf/nginx.conf
location /status {
stub_status on;
}
[root@lihuaixin ~]# nginx -s reload
[root@qiaobenhuannai ~]# mkdir /script
[root@qiaobenhuannai ~]# cd /script/
[root@lihuaixin script]# vim nginx.sh
#!/bin/bash
case $1 in
accepts)
curl -s http://192.168.199.179/status | awk 'NR==3 {print $1}'
;;
handled)
curl -s http://192.168.199.179/status |awk 'NR==3 {print $2}'
;;
requests)
curl -s http://192.168.199.179/status |awk 'NR==3 {print $3}'
;;
*)
echo "Usage: $0 { accepts | handled | requests }"
esac
[root@lihuaixin script]# chmod +x nginx.sh
[root@lihuaixin script]# ll
总用量 4
-rwxr-xr-x 1 root root 389 8月 29 20:00 nginx.sh
[root@lihuaixin script]# chown -R zabbix.zabbix /script/
[root@qiaobenhuannai ~]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1 #此行改为等于1
UserParameter=check_nginx[*],/bin/bash /script/nginx.sh $1 #在最后加入此行
重启zabbix_agentd
[root@qiaobenhuannai ~]# pkill zabbix
[root@qiaobenhuannai ~]# zabbix_agentd
在服务端手动测试
[root@lihuaixin ~]# zabbix_get -s 192.168.199.185 -k check_nginx[requests]
717
配置——主机——监控项
配置以下配置其他保持默认
配置——主机——触发器
监控到值表示成功
[root@lihuaixin ~]# zabbix_server
zabbix_server: error while loading shared libraries: libmysqlclient.so.20: cannot open shared object file: No such file or directory
解决方法
[root@lihuaixin ~]# cd /usr/src/zabbix-4.0.3
[root@lihuaixin zabbix-4.0.3]# find / -name libmysqlclient.so.20
/usr/local/lib/libmysqlclient.so.20
/usr/local/mysql-5.7.22-linux-glibc2.12-x86_64/lib/libmysqlclient.so.20
[root@lihuaixin zabbix-4.0.3]# echo "/usr/local/mysql/lib" >> /etc/ld.so.conf
[root@lihuaixin zabbix-4.0.3]# ldconfig
然后在启动zabbix_server就行了