LNMP方式部署ZABBIX服務器

1、zabbix部署架構

image


2、正文

2.一、部署環境

操做系統: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.二、LNMP部署

2.2.一、Linux系統安裝(略)

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.三、Mysql5.7服務部署

2.2.3.一、卸載系統自帶的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.二、安裝最新版Mysql

image

刪除舊版本mysqlphp

[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.7html

[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.rpmwarning: mysql-community-libs-compat-5.7.26-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEYPreparing...                ########################################### [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.cnfjava

[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服務node

image

修改mysql初始密碼,由於我是拿來測試的,不想密碼設置太複雜,我這裏將密碼複雜度(默認須要有數字,大小寫字母,特殊字符)和密碼長度(默認是8位)都調低了。python

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

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基礎數據到數據庫中,注意按順序導入linux

[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.四、PHP服務部署

依賴安裝nginx

[root@server1 upload]# 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文件c++

image

PHP配置文件修改web

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 PHP和Nginx關聯

[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.php和mysql_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.三、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]#

而後重啓nginx和php-fpm便可

image

image

image

image

按照提示下載下來再傳到對應服務器位置刷新界面便可

image

image

zabbix的默認帳號和密碼是 Admin/zabbix

最終打開的界面以下:

image

相關文章
相關標籤/搜索