1.lnmp搭建
参考上一篇lnmp分离部署,将lnmp搭建在一台主机上
1.1 安装后配置
# 修改nginx的主配置文件
[root@zabbix ~]# vim /usr/local/nginx/conf/nginx.conf
.....
error_log logs/error.log;
#error_log logs/error.log notice; ##开启错误日志存放目录
#error_log logs/error.log info;
.....
# 开启以匹配php结尾的网站模板
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ##此处修改/scripts为$document_root
include fastcgi_params;
}
......
# 修改server段内容为
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm; ##此处添加index.php即可
}
# 在网页目录站点创建index.php的文件,写入php网页信息
[root@zabbix ~]# cat > /usr/local/nginx/html/index.php <<EOF
> <?php
> phpinfo();
> ?>
> EOF
# 检查配置文件语法是否有误,并重新加载配置文件
[root@zabbix ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zabbix ~]# nginx -s reload
1.2 验证php页面
2. Zabbix部署
环境说明:
环境 | IP | 应用 |
---|---|---|
服务器 | 192.168.40.139 | lnmp架构 zabbix serverzabbix agent |
客户端 | 192.168.40.140 | zabbix agent |
2.1 zabbix服务端安装
# 安装依赖包
[root@zabbix ~]# yum -y install net-snmp-devel libevent-devel
安装过程略....
# 下载zabbix
[root@zabbix ~]# cd /usr/src/
[root@zabbix ~]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
下载过程略....
# 解压
[root@zabbix src]# tar xf zabbix-4.0.3.tar.gz
# 创建zabbix用户和组
[root@zabbix src]# groupadd -r zabbix
[root@zabbix src]# useradd -r -M -s /sbin/nologin -g zabbix zabbix
# 配置zabbix数据库
[root@zabbix ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> alter user 'root'@'localhost' identified by 'scl666'; #重置密码
Query OK, 0 rows affected (0.00 sec)
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix123!';
Query OK, 0 rows affected, 2 warnings (0.07 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@zabbix ~]# cd /usr/src/zabbix-4.0.3/database/mysql/
[root@zabbix mysql]# ls
data.sql images.sql Makefile.am Makefile.in schema.sql
[root@zabbix mysql]# mysql -uzabbix -pzabbix123! zabbix < schema.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@zabbix mysql]# mysql -uzabbix -pzabbix123! zabbix < images.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@zabbix mysql]# mysql -uzabbix -pzabbix123! zabbix < data.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
# 编译安装zabbix
[root@zabbix mysql]# cd /usr/src/zabbix-4.0.3
[root@zabbix zabbix-4.0.3]# ./configure --enable-server \
> --enable-agent \
> --with-mysql \
> --with-net-snmp \
> --with-libcurl \
> --with-libxml2
[root@zabbix zabbix-4.0.3]# make install
2.2 zabbix服务端配置
# 修改服务端配置文件
# 设置数据库信息
[root@zabbix ~]# ls /usr/local/etc/
zabbix_agentd.conf zabbix_agentd.conf.d zabbix_server.conf zabbix_server.conf.d
[root@zabbix ~]# vim /usr/local/etc/zabbix_server.conf
....
DBPassword=zabbix123! //设置zabbix数据库连接密码
# 启动zabbix_server和zabbix_agentd
[root@zabbix ~]# zabbix_server
[root@zabbix ~]# zabbix_agentd
[root@zabbix ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
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 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
2.3 zabbix web界面安装前配置
# 修改/etc/php.ini的配置并重启php-fpm
[root@zabbix ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[root@zabbix ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[root@zabbix ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[root@zabbix ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[root@zabbix ~]# service php-fpm restart
[root@zabbix ~]# cd /usr/src/zabbix-4.0.3
[root@zabbix zabbix-4.0.3]# ls
aclocal.m4 ChangeLog config.log configure.ac frontends m4 man README
AUTHORS compile config.status COPYING include Makefile misc sass
bin conf config.sub database INSTALL Makefile.am missing src
build config.guess configure depcomp install-sh Makefile.in NEWS
[root@zabbix zabbix-4.0.3]# mkdir /usr/local/nginx/html/zabbix
[root@zabbix zabbix-4.0.3]# cp -a frontends/php/* /usr/local/nginx/html/zabbix/
[root@zabbix zabbix-4.0.3]# chown -R nginx.nginx /usr/local/nginx/html
# 修改nginx配置文件
#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;
}
# 启动nginx status,在nginx.conf配置文件中添加以下内容
[root@zabbix zabbix-4.0.3]# vim /usr/local/nginx/conf/nginx.conf #
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.40.140; ## 定义仅客户端能访问
deny all;
}
[root@zabbix zabbix-4.0.3]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zabbix zabbix-4.0.3]# nginx -s reload
# 客户端使用curl命令获取nginx status,并了解其参数值
[root@client ~]# curl http://192.168.40.139/nginx_status
Active connections: 1
server accepts handled requests
8 8 4
Reading: 0 Writing: 1 Waiting: 0
## Active connections – 活跃的连接数量
## server accepts handled requests — 总共处理了1个连接 , 成功创建1次握手, 总共处理了1个请求
## Reading — 读取客户端的连接数
## Writing — 响应数据到客户端的数量
## Waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接.
# 设置zabbix/conf目录的权限,让zabbix有权限生成配置文件zabbix.conf.php
[root@zabbix ~]# chmod 777 /usr/local/nginx/html/zabbix/conf
[root@zabbix ~]# ll -d /usr/local/nginx/html/zabbix/conf
drwxrwxrwx 2 nginx nginx 81 12月 20 2018 /usr/local/nginx/html/zabbix/conf
# 重启nginx
[root@zabbix ~]# nginx -s stop
[root@zabbix ~]# nginx
[root@zabbix ~]# 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 :::*
登录进去zabbix网页
# 恢复zabbix/conf目录的权限为755
[root@zabbix ~]# chmod 755 /usr/local/nginx/html/zabbix/conf
2.4 zabbix 客户端配置
# 安装依赖包
[root@client ~]# yum -y install net-snmp-devel libevent-devel libxml2-devel curl-devel pcre*
# 下载zabbix并解压
[root@client ~]# cd /usr/src/
[root@clientsrc]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/4.0.3/zabbix-4.0.3.tar.gz
[root@client src]# tar xf zabbix-4.0.3.tar.gz
# 创建zabbix用户和组
[root@client src]# groupadd -r zabbix
[root@client src]# useradd -r -M -s /sbin/nologin -g zabbix zabbix
# 编译安装zabbix
[root@client src]# cd zabbix-4.0.3
[root@client zabbix-4.0.3]# ./configure --enable-agent
编译过程略。。。
[root@client zabbix-4.0.3]# make install
[root@client zabbix-4.0.3]# ls /usr/local/etc/
zabbix_agentd.conf zabbix_agentd.conf.d
# 修改服务端配置文件
# 设置数据库信息
[root@client zabbix-4.0.3]# vim /usr/local/etc/zabbix_agentd.conf #找到以下三行并修改为:
Server=192.168.100.100
ServerActive=192.168.100.100
Hostname=runtime # 可自定义
# 启动zabbix_agentd
[root@client zabbix-4.0.3]# zabbix_agentd
[root@client zabbix-4.0.3]# ss -antl
[root@client ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::22 :::*
# 在客户端创建脚本存放目录
[root@client ~]# mkdir /scripts
# 写脚本
[root@client ~]# vim /scripts/ngx_status.sh
#!/bin/bash
case $1 in
accepts)
curl -s http://192.168.40.139/status 2>/dev/null | awk 'NR==3 {print $1}'
;;
handled)
curl -s http://192.168.40.139status 2>/dev/null |awk 'NR==3 {print $2}'
;;
requests)
curl -s http://192.168.40.139/status 2>/dev/null |awk 'NR==3 {print $3}'
;;
*)
echo "Usage: $0 { accepts | handled | requests }"
esac
# 给脚本执行权限
[root@client ~]# chmod +x /scripts/ngx_status.sh
[root@client ~]# ll /scripts/ngx_status.sh
-rwxr-xr-x 1 root root 441 8月 29 00:04 /scripts/ngx_status.s
# 改脚本存放目录的属主
[root@client ~]# chown -R zabbix.zabbix /scripts/
[root@client ~]# ll -d /scripts/
drwxr-xr-x 2 zabbix zabbix 27 8月 29 00:04 /scripts/
进行访问测试检测:
[root@client ~]# curl http://192.168.40.139/nginx_status
Active connections: 2
server accepts handled requests
39 39 312
Reading: 0 Writing: 1 Waiting: 1
[root@client ~]# bash /scripts/ngx_status.sh accepts
40 ## 这里因为每次数字都会变化
# 改客户端配置文件agentd.conf
[root@client ~]# vim /usr/local/etc/zabbix_agentd.conf
修改:UnsafeUserParameters 等于 1 :UnsafeUserParameters=1
UserParameter=nginx[*],/bin/bash /scripts/ngx_status.sh $1 ##末行加入此行
# 重启zabbix_agentd
[root@client ~]# pkill zabbix
[root@client ~]# zabbix_agentd
# 在服务端手动测试
[root@zabbix ~]# zabbix_get -s 192.168.100.96 -k nginx[requests]
358