微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

LNMP方式部署ZABBIX服务器

一、zabbix部署架构

image


二、正文

2.1、部署环境

操作系统:RedHat 6.5 x86_64

Nginx:  1.15.7

PHP: 7.1.29

MysqL: 5.7.26

Zabbix: 4.2.1

openssl: 1.0.2r

测试系统配置: 1核1G20G

2.2、LNMP部署

2.2.1、Linux系统安装(略)

2.2.2、Nginx服务部署

[root@server1 Nginx-1.15.7]# yum –y install pcre-devel

[root@server1 Nginx-1.15.7]#  ./configure --prefix=/usr/local/Nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-pcre --with-openssl=/home/ipnet/Update_openssh_v8.0p1_rhel4567_20190425/openssl-1.0.2r --with-zlib=/home/ipnet/Update_openssh_v8.0p1_rhel4567_20190425/zlib-1.2.11

Configuration summary
   + using system PCRE library
   + using OpenSSL library: /home/ipnet/upload/Update_openssh_v7.9p1_rhel4567_20181228/openssl-1.0.2o
   + using zlib library: /home/ipnet/upload/Update_openssh_v7.9p1_rhel4567_20181228/zlib-1.2.11

  Nginx path prefix: "/usr/local/Nginx"
   Nginx binary file: "/usr/local/Nginx/sbin/Nginx"
   Nginx modules path: "/usr/local/Nginx/modules"
   Nginx configuration prefix: "/usr/local/Nginx/conf"
   Nginx configuration file: "/usr/local/Nginx/conf/Nginx.conf"
   Nginx pid file: "/usr/local/Nginx/logs/Nginx.pid"
   Nginx error log file: "/usr/local/Nginx/logs/error.log"
   Nginx http access log file: "/usr/local/Nginx/logs/access.log"
   Nginx http client request body temporary files: "client_body_temp"
   Nginx http proxy temporary files: "proxy_temp"
   Nginx http fastcgi temporary files: "fastcgi_temp"
   Nginx http uwsgi temporary files: "uwsgi_temp"
   Nginx http scgi temporary files: "scgi_temp"

[root@server1 Nginx-1.15.7]# make && make install

注:

--with-http_ssl_module 表示支持https方式

--with-http_v2_module 表示支持http2.0

--with-http_stub_module 表示支持Nginx状态查询

--with-pcre 表示支持rewrite功能

--with-openssl 指向openssl安装目录

--with-zlib  指向 zlib安装目录


2.2.3、MysqL5.7服务部署

2.2.3.1、卸载系统自带MysqL服务

[root@server1 upload]# rpm -qa|grep MysqL
MysqL-devel-5.1.73-7.el6.x86_64
MysqL-libs-5.1.73-7.el6.x86_64
MysqL-server-5.1.73-7.el6.x86_64
MysqL-5.1.73-7.el6.x86_64
[root@server1 upload]# rpm -e MysqL-devel
[root@server1 upload]# rpm -e MysqL-server
warning: /var/log/MysqLd.log saved as /var/log/MysqLd.log.rpmsave
[root@server1 upload]# rpm -e MysqL
[root@server1 MariaDB]# rpm  -e --nodeps MysqL-libs
[root@server1 upload]# rpm -qa|grep MysqL
[root@server1 upload]#

2.2.3.2、安装最新版MysqL

image

删除旧版本MysqL

[root@server1 upload]# rpm -qa|grep MysqL
MysqL-devel-5.1.73-7.el6.x86_64
MysqL-libs-5.1.73-7.el6.x86_64
PHP-MysqL-5.3.3-47.el6.x86_64
MysqL-server-5.1.73-7.el6.x86_64
MysqL-5.1.73-7.el6.x86_64
[root@server1 upload]#
[root@server1 upload]#
[root@server1 upload]# rpm -e MysqL-devel
[root@server1 upload]# rpm -e MysqL-libs
error: Failed dependencies:
     libMysqLclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
     libMysqLclient.so.16()(64bit) is needed by (installed) perl-dbd-mysql-4.013-3.el6.x86_64
     libMysqLclient.so.16()(64bit) is needed by (installed) MysqL-5.1.73-7.el6.x86_64
     libMysqLclient.so.16()(64bit) is needed by (installed) MysqL-server-5.1.73-7.el6.x86_64
     libMysqLclient.so.16()(64bit) is needed by (installed) PHP-MysqL-5.3.3-47.el6.x86_64
     libMysqLclient.so.16(libMysqLclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
     libMysqLclient.so.16(libMysqLclient_16)(64bit) is needed by (installed) perl-dbd-mysql-4.013-3.el6.x86_64
     libMysqLclient.so.16(libMysqLclient_16)(64bit) is needed by (installed) MysqL-5.1.73-7.el6.x86_64
     libMysqLclient.so.16(libMysqLclient_16)(64bit) is needed by (installed) MysqL-server-5.1.73-7.el6.x86_64
     libMysqLclient.so.16(libMysqLclient_16)(64bit) is needed by (installed) PHP-MysqL-5.3.3-47.el6.x86_64
     libMysqLclient_r.so.16()(64bit) is needed by (installed) MysqL-python-1.2.3-0.3.c1.1.el6.x86_64
     libMysqLclient_r.so.16()(64bit) is needed by (installed) MysqL-5.1.73-7.el6.x86_64
     libMysqLclient_r.so.16()(64bit) is needed by (installed) MysqL-server-5.1.73-7.el6.x86_64
     libMysqLclient_r.so.16(libMysqLclient_16)(64bit) is needed by (installed) MysqL-python-1.2.3-0.3.c1.1.el6.x86_64
     libMysqLclient_r.so.16(libMysqLclient_16)(64bit) is needed by (installed) MysqL-5.1.73-7.el6.x86_64
     libMysqLclient_r.so.16(libMysqLclient_16)(64bit) is needed by (installed) MysqL-server-5.1.73-7.el6.x86_64
     MysqL-libs is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
     MysqL-libs = 5.1.73-7.el6 is needed by (installed) MysqL-5.1.73-7.el6.x86_64
[root@server1 upload]# rpm -e PHP-MysqL
[root@server1 upload]# rpm -e MysqL-server
warning: /var/log/MysqLd.log saved as /var/log/MysqLd.log.rpmsave
[root@server1 upload]# rpm -e MysqL-5.1.73
[root@server1 upload]# rpm -qa|grep MysqL
MysqL-libs-5.1.73-7.el6.x86_64
[root@server1 upload]#

安装MysqL5.7

[root@server1 upload]# rpm -Uvh MysqL-community-libs-compat-5.7.26-1.el6.x86_64.rpm MysqL-community-libs-5.7.26-1.el6.x86_64.rpm MysqL-community-server-5.7.26-1.el6.x86_64.rpm MysqL-community-client-5.7.26-1.el6.x86_64.rpm MysqL-community-common-5.7.26-1.el6.x86_64.rpm MysqL-community-devel-5.7.26-1.el6.x86_64.rpm
warning: MysqL-community-libs-compat-5.7.26-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                ########################################### [100%]
    1:MysqL-community-common ########################################### [ 20%]
    2:MysqL-community-libs   ########################################### [ 40%]
    3:MysqL-community-client ########################################### [ 60%]
    4:MysqL-community-server ########################################### [ 80%]
    5:MysqL-community-libs-co########################################### [100%]

    6:MysqL-community-devel  ########################################### [100%]

修改my.cnf

[root@server1 upload]# cat /etc/my.cnf
[client]
default-character-set = utf8
socket=/var/lib/MysqL/MysqL.sock

[MysqLd]
max_connections = 2000
open_files_limit = 30000
table_open_cache = 20000
explicit_defaults_for_timestamp = true
transaction-isolation = READ-COMMITTED
max_allowed_packet = 128M
innodb_buffer_pool_size = 20G  这个一般是主机内存的80%
innodb_log_file_size = 512M
innodb_file_per_table = 1
innodb_log_buffer_size=4M
innodb_thread_concurrency=64
innodb_flush_log_at_trx_commit=0
innodb_flush_method=O_DIRECT
lower_case_table_names = 1
#collation-server = gbk_bin       
# character-set-server = gbk
default-storage-engine = INNODB
join_buffer_size = 512M
sort_buffer_size = 20M
read_rnd_buffer_size = 20M
 
log_timestamps = system
collation-server = utf8_bin
character-set-server = utf8
#validate_password_policy = 0
#validate_password_number_count = 0
#validate_password_length = 4
#validate_password_special_char_count = 0
default_password_lifetime = 0
innodb_read_io_threads = 16
innodb_write_io_threads = 16

datadir=/var/lib/MysqL
socket=/var/lib/MysqL/MysqL.sock
 
skip-host-cache
skip-name-resolve
 
# disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MysqL setup
#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
sql_mode='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
 
[MysqLd_safe]
log-error=/var/log/MysqLd.log
pid-file=/var/run/MysqLd/MysqLd.pid

[root@server1 upload]#


启动MysqL服务

image

修改MysqL初始密码,因为我是拿来测试的,不想密码设置太复杂,我这里将密码复杂度(认需要有数字,大小写字母,特殊字符)和密码长度(认是8位)都调低了。

MysqL> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

MysqL> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

MysqL> alter user 'root'@'localhost' identified by 'redhat';
Query OK, 0 rows affected (0.00 sec)

MysqL>

创建zabbix数据库并设置zabbix用户访问权限

MysqL> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

MysqL> grant all on zabbix.* to zabbix@localhost identified by 'redhat';
Query OK, 0 rows affected, 2 warnings (0.00 sec)

MysqL> grant all on *.* to root@'127.0.0.1' identified by 'redhat';
Query OK, 0 rows affected, 1 warning (0.44 sec)

MysqL> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MysqL>


导入zabbix基础数据到数据库中,注意按顺序导入

[root@server1 zabbix-4.2.1]# cd /home/ipnet/zabbix-4.2.1/zabbix-4.2.1
[root@server1 zabbix-4.2.1]# ls
aclocal.m4  build      conf          config.status  configure.ac  depcomp    INSTALL     Makefile     man      NEWS    src
AUTHORS     ChangeLog  config.guess  config.sub     copYING       frontends  install-sh  Makefile.am  misc     README
bin         compile    config.log    configure      database      include    m4          Makefile.in  missing  sass
[root@server1 zabbix-4.2.1]# cd database/
[root@server1 database]# ls
elasticsearch  ibm_db2  Makefile  Makefile.am  Makefile.in  MysqL  oracle  postgresql  sqlite3
[root@server1 database]# cd MysqL/
[root@server1 MysqL]# ls
data.sql  images.sql  Makefile  Makefile.am  Makefile.in  schema.sql
[root@server1 MysqL]# MysqL -uzabbix -predhat zabbix < schema.sql
MysqL: [Warning] Using a password on the command line interface can be insecure.
[root@server1 MysqL]# MysqL -uzabbix -predhat zabbix < images.sql
MysqL: [Warning] Using a password on the command line interface can be insecure.
[root@server1 MysqL]# MysqL -uzabbix -predhat zabbix < data.sql
MysqL: [Warning] Using a password on the command line interface can be insecure.
[root@server1 MysqL]#



2.2.4、PHP服务部署

依赖安装

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2  glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap  openldap-devel openldap.i686 openldap-devel.i686 nss_ldap openldap-clients openldap-servers net-snmp net-snmp-devel libxslt-devel

[root@server1 upload]# rpm -ivh libmcrypt-devel-2.5.8-9.el6.x86_64.rpm libmcrypt-2.5.8-9.el6.x86_64.rpm

Preparing...                ########################################### [100%]
    1:libmcrypt              ########################################### [ 50%]
    2:libmcrypt-devel        ########################################### [100%]

[root@server1 PHP-7.1.29]# './configure' '--prefix=/usr/local/PHP-7.1.29' '--with-config-file-path=/usr/local/PHP-7.1.29/etc' '--with-MysqLi=MysqLnd' '--with-pdo-MysqL=MysqLnd' '--enable-mbstring' '--with-freetype-dir' '--with-jpeg-dir' '--with-png-dir' '--with-iconv-dir' '--with-zlib' '--with-libxml-dir' '--enable-pdo' '--with-snmp' '--with-xsl' '--enable-xml' '--disable-rpath' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--enable-mbregex' '--enable-fpm' '--with-mcrypt' '--with-gd' '--enable-gd-native-ttf' '--with-openssl' '--with-mhash' '--enable-sockets' '--with-bz2' '--enable-pcntl' '--with-xmlrpc' '--enable-zip' '--enable-soap' '--disable-debug' '--disable-ipv6' '--enable-opcache' '--without-pear'

[root@server1 upload]# make && make install

生成PHP-fpm.conf 和 PHP.ini文件

image

PHP配置文件修改

max_execution_time = 300
memory_limit = 128M
post_max_size = 16M
upload_max_filesize = 2M
max_input_time = 300
date.timezone = Asia/Shanghai

PHP-fpm引擎的配置文件

image

[root@server1 PHP-fpm.d]# egrep -v '^$|^;' www.conf
[www]
user = nobody
group = nobody
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 30
pm.max_requests = 500
slowlog = /var/log/PHP-fpm/$pool.log.slow
request_slowlog_timeout = 10s
[root@server1 PHP-fpm.d]#

[root@server1 PHP-fpm.d]# mkdir –p /var/log/PHP-fpm

[root@server1 PHP-fpm.d]# /usr/local/PHP-7.1.29/sbin/PHP-fpm

[root@server1 PHP-fpm.d]# netstat -tnlp|grep 9000
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      91358/PHP-fpm      
[root@server1 PHP-fpm.d]#


2.2.5 PHPNginx关联

[root@server1 PHP-fpm.d]# vim /usr/local/Nginx/conf/Nginx.conf

[root@server1 PHP-fpm.d]# egrep -v '^\s+#.*|^$|^#' /usr/local/Nginx/conf/Nginx.conf
user  nobody;
worker_processes  1;
error_log  logs/error.log;
pid        logs/Nginx.pid;
events {
     worker_connections  1024;
}
http {
     include       mime.types;
     default_type  application/octet-stream;
     sendfile        on;
     keepalive_timeout  65;
     server {
         listen       80;
         server_name  localhost;
         location / {
             root   html;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
         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;
         }
     }
}
[root@server1 PHP-fpm.d]#

[root@server1 PHP-fpm.d]# /usr/local/Nginx/sbin/Nginx
[root@server1 PHP-fpm.d]# netstat -tnlp|grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      91399/Nginx        
[root@server1 PHP-fpm.d]#

重点

   Nginx配置文件 fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name; 这一行需要改成fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

否则界面访问PHP文件会报File not Found的错误Nginx后台日志也会报Primary script unkNown" while reading response header from upstream的报错,$document_root 代表当前请求在root指令中指定的值,更改后重启Nginx服务即可恢复正常访问。

测试Nginx打开PHP页面是否正常

直接在/usr/local/Nginx/html 目录中新建index.PHPMysqL_connect.PHP

[root@server1 html]# cat index.PHP
<?PHP
PHPinfo();
?>
[root@server1 html]#

[root@server1 html]# cat test.PHP
<?PHP
$link = new MysqLi("127.0.0.1","root","redhat");
if(!$link) {
     echo "数据库连接失败";
}
else {
      echo "连接成功";
}
$link->close();
?>
[root@server1 html]#

image

image

2.3、zabbix部署

必要依赖环境安装

[root@server1 zabbix-4.2.1]# rpm -ivh /home/ipnet/zabbix-4.2.1/libevent-devel-1.4.13-4.el6.x86_64.rpm /home/ipnet/zabbix-4.2.1/libevent-headers-1.4.13-4.el6.noarch.rpm /home/ipnet/zabbix-4.2.1/libevent-doc-1.4.13-4.el6.noarch.rpm
warning: /home/ipnet/zabbix-4.2.1/libevent-devel-1.4.13-4.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY
Preparing...                ########################################### [100%]
    1:libevent-headers       ########################################### [ 33%]
    2:libevent-doc           ########################################### [ 67%]
    3:libevent-devel         ########################################### [100%]
[root@server1 zabbix-4.2.1]#

如果要zabbix服务器支持监控JAVA应用程序的性能需要事先在服务端安装java编译环境然后启用—enable-java 参数

[root@server1 zabbix-4.2.1]# rpm -ivh /home/ipnet/jdk-7u80-linux-x64.rpm

如果需要监控服务器硬件可以加上 –with-openipmi 参数,这个需要事先安装好OpenIPMI和OpenIPMI-devel RPM包,也可以用SNMP来监控服务器硬件,我因为是用的SNMP来获取服务器硬件信息,这里我没有将这个功能编译进去。

编译zabbix服务端应用

[root@server1 zabbix-4.2.1]# ./configure --prefix=/usr/local/zabbix-3.4.1 --enable-server --enable-agent --with-MysqL --with-net-snmp --with-libcurl --with-libxml2 --with-openssl --enable-java --enable-ipv6

[root@server1 zabbix-4.2.1]# make && make install

[root@server1 conf]# egrep -v '^$|^#' /usr/local/zabbix-3.4.1/etc/zabbix_server.conf
ListenPort=10051
LogFile=/tmp/zabbix_server.log
Timeout=4
LogSlowQueries=3000
StatsAllowedIP=127.0.0.1
DBHost=localhost
dbname=zabbix
DBUser=zabbix
DBPassword=redhat
ListenIP=0.0.0.0
[root@server1 conf]

[root@server1 conf]# useradd -M zabbix -s /sbin/nologin

[root@server1 conf]# /usr/local/zabbix-3.4.1/sbin/zabbix_server

[root@server1 conf]# /usr/local/zabbix-3.4.1/sbin/zabbix_agentd
[root@server1 conf]# ps -ef|grep zabbix
zabbix     3973      1  0 16:27 ?        00:00:00 /usr/local/zabbix-3.4.1/sbin/zabbix_server
zabbix     3978      1  0 16:27 ?        00:00:00 /usr/local/zabbix-3.4.1/sbin/zabbix_agentd
zabbix     3979   3978  0 16:27 ?        00:00:00 /usr/local/zabbix-3.4.1/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix     3980   3978  0 16:27 ?        00:00:00 /usr/local/zabbix-3.4.1/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix     3981   3978  0 16:27 ?        00:00:00 /usr/local/zabbix-3.4.1/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix     3982   3978  0 16:27 ?        00:00:00 /usr/local/zabbix-3.4.1/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix     3983   3978  0 16:27 ?        00:00:00 /usr/local/zabbix-3.4.1/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root       3985   2470  0 16:27 pts/0    00:00:00 grep zabbix
[root@server1 conf]#

拷贝zabbix web 文件到html目录下并改名

[root@server1 zabbix-4.2.1]# cp -rf /home/ipnet/zabbix-4.2.1/zabbix-4.2.1/frontends/PHP /usr/local/Nginx/html/
[root@server1 zabbix-4.2.1]# cd /usr/local/Nginx/html/ && mv PHP zabbix
[root@server1 html]# ls -l /usr/local/Nginx/html/
总用量 20
-rw-r--r--.  1 root root  494 5月  23 13:07 50x.html
-rw-r--r--.  1 root root  612 5月  23 13:07 index.html
-rw-r--r--.  1 root root   20 5月  23 14:01 index.PHP
-rw-r--r--.  1 root root  157 5月  23 14:48 test.PHP
drwxr-xr-x. 13 root root 4096 5月  23 16:07 zabbix

浏览器打开zabbix配置界面

image

下面的要求都需要是OK状态再继续。

image

配置数据库连接,当主机填写为localhost时MysqL会采用 unix domain socket连接,当主机填写为127.0.0.1时MysqL会采用TCP/IP的方式连接,如果页面报的错误是No such file or directory,也就是说PHP找不到这个socket文件,这个时候需要修改PHP.ini指定MysqL的socket文件位置

具体可以看https://segmentfault.com/q/1010000000328531 这个博文

首先my.cnf看下socket文件的位置

[root@server1 no-debug-non-zts-20160303]# cat /etc/my.cnf|grep socket
socket=/var/lib/MysqL/MysqL.sock

[root@server1 no-debug-non-zts-20160303]# egrep 'MysqLi.default_socket' /usr/local/PHP-7.1.29/etc/PHP.ini
MysqLi.default_socket = /var/lib/MysqL/MysqL.sock
[root@server1 no-debug-non-zts-20160303]#

然后重启NginxPHP-fpm即可

image


image

image

image

按照提示下载下来再传到对应服务器位置刷新界面即可

image

image

zabbix的认账号和密码是 Admin/zabbix

最终打开的界面如下:

image


三、总结

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐