LNMP+zabbix监控平台搭建

前言


由于某个项目需要新搭建zabbix监控平台。于是就抽时间总结了zabbix搭建的流程及排错的详细流程。由于每个人的生产环境的差异,本文适用于参考。

实际上,使用yum安装配置LAMP+zabbix更加的方便,快捷。但是,为了便于管理,此处使用的是源码安装。


系统:centos7.6
安装方式:源码安装
nginx版本:1.17.1
mysql版本:5.6.44
php版本:7.3.7
zabbix-server版本:4.2.4




Nginx源码安装



【1】安装依赖包


为了避免后期的问题,安装了大量的依赖包,其中可能有某些并不必要

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++   openssl-devel cmakepcre-develnanowget  gcc gcc-c++ ncurses-devel perl  pcre pcre-devel openssl openssl-devel fping


【2】下载并安装源码包



<1>创建nginx用户(为了安全,可禁止其登录)

useradd nginx -s /sbin/nologin


<2>从Nginx官网下载Nginx源码包
( /apk/package为创建的安装包统一存放位置)

wget http://nginx.org/download/nginx-1.17.1.tar.gz 
tar xf nginx-1.17.1.tar.gz  -C /apk/package
cd /apk/package/nginx-1.17.1


<3>编译安装
(/apk/install/nginx-1.17.1为安装目录)

注意编译参数(–prefix指定安装软件目录):

./configure --prefix=/apk/install/nginx-1.17.1 --with-http_stub_status_module --without-http_auth_basic_module --without-http_autoindex_module --without-http_browser_module --without-http_geo_module --without-http_limit_req_module --without-http_limit_conn_module --without-http_map_module --without-http_memcached_module --without-http_scgi_module --without-http_split_clients_module --without-http_userid_module --without-http_uwsgi_module --without-mail_imap_module --without-mail_pop3_module --without-mail_smtp_module --without-poll_module --without-select_module --with-stream --add-module=/apk/install/nginx-mod/mode --add-module=/apk/install/nginx-mod/nginx-module


下列为此次使用的编译参数:

./configure --prefix=/apk/install/nginx-1.17.1 --with-http_stub_status_module --without-http_auth_basic_module --without-http_autoindex_module --without-http_browser_module --without-http_geo_module --without-http_limit_req_module --without-http_limit_conn_module --without-http_map_module --without-http_memcached_module --without-http_scgi_module --without-http_split_clients_module --without-http_userid_module --without-http_uwsgi_module --without-mail_imap_module --without-mail_pop3_module --without-mail_smtp_module --without-poll_module --without-select_module --with-stream --with-http_ssl_module --with-pcre


make -j4

make install


–add-module为指定第三方模块的安装目录,如没安装第三方模块,可不配置,否则编译时会报如下错误:

在这里插入图片描述




Nginx配置zabbix


注意:/apk/install/nginx-1.17.1在此处为nignx编译时,指定的软件安装目录。修改配置文件要从自己指定的安装目录中找。


【1】配置启动方式



<1>做软链接或配置环境变量,使nginx命令可被直接使用

ln -s /apk/install/nginx-1.17.1/sbin/nginx /usr/sbin/nginx


<2>测试nginx配置文件是否有问题

nginx -t

在这里插入图片描述

<3>启动nginx

nginx 


【2】配置nginx的zabbix目录



<1>创建zabbix的web文件存放目录

mkdir /apk/data/web/zabbix -pv


<2>nginx配置文件如下:

cat /apk/install/nginx-1.17.1/conf/nginx.conf



user  nginx nginx;
worker_processes  auto;

error_log  logs/error.log info;

pid        logs/nginx.pid;
worker_rlimit_nofile    65536;

events
{
    use epoll;
    accept_mutex off;
    worker_connections  65536;
}

http
{
    include       mime.types;
    default_type  text/html;

    charset	UTF-8;
    server_names_hash_bucket_size	128;
    client_header_buffer_size		4k;
    large_client_header_buffers	 4	32k;
    client_max_body_size            20m;

    open_file_cache max=65536  inactive=60s;
    open_file_cache_valid      80s;
    open_file_cache_min_uses   1;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile    on;
    server_tokens off;

    keepalive_timeout  60;

    gzip  on;
    gzip_min_length	1k;
    gzip_buffers  4	64k;
    gzip_http_version	1.1;
    gzip_comp_level	2;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    fastcgi_temp_path /tmp/fastcgi_temp;
    fastcgi_cache_path /tmp/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:16m inactive=30m max_size=1g;
    fastcgi_cache_key $request_method://$host$request_uri;
    fastcgi_cache_valid 200 302 1h;
    fastcgi_cache_valid 301     1d;
    fastcgi_cache_valid any     1m;
    fastcgi_cache_min_uses 1;
    fastcgi_cache_use_stale error timeout http_500 http_503 invalid_header;

     server{
         listen 80;
         server_name zabbix.pro.cn;
         access_log logs/access.log main;
         index index.html index.htm index.php;
         root /apk/data/web/zabbix;

         location ~ [^/]\.php(/|$) {
         	fastcgi_pass 127.0.0.1:9000;
         	fastcgi_index index.php;
         	include fastcgi.conf;
         }

         }

}


<3>nginx配置文件测试及重新加载

nginx -t
nginx -s reload



mysql源码安装


可参考下列链接:
https://blog.csdn.net/GX_1_11_real/article/details/92987950



【1】安装依赖包


为了避免后期的问题,安装了大量的依赖包。即使提示没有安装包,接着往下执行即可

yum -y install gcc libxml2-dev curl screen libpng12-dev autoconf libpcre3-dev make bzip2 libevent-dev patch libjpeg62-dev libcurl4-openssl-dev \libfreetype6-dev g++ libtool libncurses5-dev psmisc lrzsz cmake bison libaio* boost


【2】下载并安装源码包



<1>创建mysql用户(为了安全,可禁止其登录)

groupadd mysql
useradd -g mysql mysql -s /bin/false


<2>从mysql官网下载安装包

mysql官网下载地址:https://dev.mysql.com/downloads/mysql/

wget   https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.44.tar.gz
tar xf mysql-5.6.44.tar.gz -C /apk/package


<3>创建相关的目录,用于后期配置和管理
(根据需求,也可跳过这步)

mkdir -p /apk/install/mysql-5.6.44
mkdir -p /apk/data/mysql_3306/data
mkdir -p /apk/conf/mysql/mysql_3306
mkdir -p /apk/log/mysql/mysql_3306
mkdir -p /apk/backup/mysql_3306

mkdir -pv /apk/log/mysql/mysql_3306/logs/relay-log
mkdir -pv  /apk/log/mysql/mysql_3306/logs/slow-log
mkdir -pv /apk/log/mysql/mysql_3306/logs/bin-log
mkdir -pv  /apk/log/mysql/mysql_3306/logs/error-log


chown -R mysql.mysql /apk/install/mysql-5.6.44
chown -R mysql.mysql  /apk/data/mysql_3306/data
chown -R mysql.mysql  /apk/conf/mysql/mysql_3306
chown -R mysql.mysql   /apk/log/mysql/mysql_3306
chown -R mysql.mysql  /apk/backup/mysql_3306


<4>使用cmake编译安装

cd /apk/package/mysql-5.6.44

cmake . -DCMAKE_INSTALL_PREFIX=/apk/install/mysql-5.6.44 -DMYSQL_DATADIR=/apk/data/mysql_3306/data  -DSYSCONFDIR=/apk/conf/mysql/mysql_3306 -DMYSQL_UNIX_ADDR=/tmp/mysql_3306.sock -DMYSQL_TCP_PORT=3306 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all  -DMYSQL_USER=mysql
    
make -j4 
make install



mysql配置



【1】mysql启动方式配置



<1>配置启动脚本

cd /apk/install/mysql-5.6.44/support-files
cp my-default.cnf /apk/conf/mysql/mysql_3306/my.cnf
cp mysql.server /etc/init.d/mysql_3306

vim /etc/init.d/mysql_3306

basedir 添加指定的安装根目录
datadir 添加指定的数据存放目录
cnf 添加指定的配置文件

在这里插入图片描述



<2>配置开机自启

chmod + x /etc/init.d/mysql_3306
chkconfig   --add mysql_3306
chkconfig mysql_3306 on
chkconfig --list | grep mysql_3306


<3>配置环境变量

vim /etc/profile
export PATH=/apk/install/mysql-5.6.44/bin/:$PATH

source /etc/profile


【2】mysql配置文件配置


根据需求,配置需要的参数,后期也可修改。


下方仅供参考


cat /apk/conf/mysql/mysql_3306/my.cnf

    
[client]
port = 3306
socket = /tmp/mysql.sock
user=账号
host=localhost
password=密码


[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
log_bin =  /apk/log/mysql/mysql_3306/logs/bin-log/bin.log
log-error = /apk/log/mysql/mysql_3306/logs/error-log/error.log
pid-file = mysql.pid


long_query_time = 1
slow_query_log
slow_query_log_file = /apk/log/mysql/mysql_3306/logs/slow-log/slow.log
max_binlog_cache_size = 16M
max_binlog_size = 1G
expire_logs_days = 15
ft_min_word_len = 4



relay-log =  /apk/log/mysql/mysql_3306/logs/relay-log/relay.log
log-slave-updates
replicate-ignore-db=mysql


# These are commonly set, remove the # and set as required.
basedir = /apk/install/mysql-5.6.44
datadir = /apk/data/mysql_3306/data
port = 3306
server_id = 1
socket = /tmp/mysql_3306.sock


back_log = 3000
max_allowed_packet = 128M
max_connections = 6000
max_connect_errors = 100


join_buffer_size = 8M
read_buffer_size = 32M
read_rnd_buffer_size = 32M
sort_buffer_size = 12M

query_cache_size = 64M
table_open_cache = 1024
thread_cache_size = 120
max_heap_table_size = 64M
tmp_table_size = 64M

key_buffer_size = 512M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 32M
myisam_max_sort_file_size = 1G
myisam_repair_threads = 1
myisam_recover

delay_key_write = ON 
delayed_insert_limit = 4000
delayed_insert_timeout = 600
delayed_queue_size = 4000

lower_case_table_names = 1
query_cache_type = 1 
performance_schema = 0
net_read_timeout = 3600
net_write_timeout = 3600


connect_timeout = 600
interactive_timeout = 600
wait_timeout = 600

local-infile = 0
skip-show-database
skip-name-resolve
skip-external-locking


# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 


#*** INNODB
#innodb_buffer_pool_size = 32G
#innodb_additional_mem_pool_size = 20M
#innodb_data_file_path = ibdata:32M:autoextend
#innodb_read_io_threads = 8
#innodb_write_io_threads = 12
#innodb_file_per_table = 1
#innodb_flush_log_at_trx_commit = 2
#innodb_log_buffer_size = 8M
#innodb_log_file_size = 256M
#innodb_log_files_in_group = 3
#innodb_max_dirty_pages_pct = 10
#innodb_force_recovery = 4

#innodb_file_format = barracuda
#innodb_file_format_max = barracuda
#innodb_buffer_pool_instances = 4
#innodb_fast_shutdown = 0
#innodb_lock_wait_timeout = 3
#innodb_flush_log_at_trx_commit = 2
#innodb_flush_method = O_DIRECT
#transaction-isolation = READ-COMMITTED
#innodb_thread_concurrency = 0
#innodb_io_capacity = 200
#innodb_purge_threads = 1
#innodb_autoinc_lock_mode = 2
#innodb_stats_on_metadata = 0

##使用线程池处理连接
#thread_handling = pool-of-threads
#thread_pool_oversubscribe = 30
#thread_pool_size = 64
#thread_pool_idle_timeout = 7200
#thread_pool_max_threads = 2000



[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout


[mysqld_safe]
open-files-limit = 60000


【3】mysql初始化



<1>mysql初始化
(根据需求选择合适的命令。如报错,需修改参数后,再次初始化)

/apk/install/mysql-5.6.44/script/mysql_install_db   --user=mysql --basedir=/apk/install/mysql/mysql-5.6.44 --datadir=/apk/data/mysql_3306/data --pid-file=/apk/data/mysql_3306/data/mysql.pid --socket=/tmp/mysql_3306.sock --port=3306 

or

/apk/install/mysql/mysql-5.6.44/script/mysql_install_db  --defaults-file=/etc/my.cnf --user=mysql

如初始化有报错,请先检查一下配置文件中的参数。修改后仍报错,可在上方的参数后添加 --no-defaults



<2>启动mysql

/etc/init.d/mysql_3306 start

如报pid相关错误,可先检查mysql的配置文件参数后,查看下列链接尝试解决:
https://blog.csdn.net/GX_1_11_real/article/details/93010284



<3>设置账号和密码

mysqladmin -u root -p password  你的密码



PHP源码安装



【1】安装依赖包


为了避免后期的问题,安装了大量可能用到的包。即使提示没有安装包,接着往下执行即可

yum install -y gcc gcc-c++  make zlib zlib-devel pcre pcre-devel  libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers  libmcrypt-devel mhash mcrypt openldap openldap-devel  zlib-devel libxml2-devel libjpeg-devel libiconv-devel freetype-devel libpng-devel gd-devel curl-devel libxslt-devel


【2】libiconv安装


<1>源码安装

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv


make  -j4
make install


<2>报错解决

make时,通常会遇到如下报错:
./stdio.h:1010:1: error: ‘gets’ undeclared here (not in a function)

在这里插入图片描述

解决方法:

vim  /root/libiconv-1.14/srclib/stdio.h

注释掉:_GL_WARN_ON_USE (gets, "gets is a security hole - use fgets instead");

LNMP+zabbix监控平台搭建_第1张图片





【3】php编译安装


<1>下载php并安装

php官网链接:https://www.php.net/downloads.php

wget https://www.php.net/distributions/php-7.3.7.tar.gz
tar -xf  php-7.3.7.tar.gz -C  /apk/package
cd /apk/package/php-7.3.7

./configure --prefix=/apk/install/php-7.3.7 --with-config-file-path=/apk/install/php-7.3.7/etc --with-pdo-mysql=/apk/install/mysql/mysql-5.6.44/ --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --with-gettext --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring  --with-gd  --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --enable-short-tags --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp --enable-opcache=no  --with-ldap  --with-ldap-sasl


<2>如配置ldap模块,需要在make前,对ldap做链接,否则编译会报错

ln -s /usr/lib64/libldap* /usr/lib/

make -j4 
make install


<3>如使用make后出现以下类似报错:

/usr/bin/ld: ext/ldap/.libs/ldap.o: undefined reference to symbol ‘ber_strdup’

解决方法如下:

vim Makefile      
在以EXTRA_LIBS开头的一行结尾添加-llber

在这里插入图片描述



<4>编译安装

make -j4 
make install


如编译php时,报错warning:configure:WARNING:unrecognizedoptions:–with-mysql
原因是php7版本不支持mysql模块,而是使用–with-pdo-mysql连接数据库,所以需要更换参数为–with-pdo-mysql

 --with-config-file-path  指定配置文件位置
 --with-pdo-mysq          指定数据库位置
 --with-iconv-dir         指定libiconv目录



【4】php启动方式配置



<1>配置文件设置
(注意php.ini文件的存放位置)

ln -s /apk/install/php-7.3.7/bin/php /usr/bin/php
cp /apk/package/php-7.3.7/php.ini-production /apk/install/php-7.3.7/etc/php.ini

cd /apk/install/php-7.3.7/etc
cp  php-fpm.conf.default  php-fpm.conf

cd /apk/install/php-7.3.7/etc/php-fpm.d
cp www.conf.default www.conf


<2>启动脚本配置

cp /apk/package/php-7.3.7/sapi/fpm/init.d.php-fpm   /etc/init.d/php-fpm

chmod +x /etc/init.d/php-fpm 

/etc/init.d/php-fpm start


<3>配置php与zabbix相关参数

cd /apk/install/php-7.3.7/etc/

vim php.ini

post_max_size 16M
max_execution_time 300
max_input_time 300
date.timezone Asia/Shanghai


<4>php-fpm启动

/etc/init.d/php-fpm start


<5>查看支持的模块

php -m |grep ldap



LNMP验证



【1】示例1


此处123.123.123.123代表zabbix_server的IP


cat test.php

    

http://123.123.123.123/test.php

LNMP+zabbix监控平台搭建_第2张图片

访问到如上页面即为成功



补充:
Configuration File(php.ini) Path为php.ini应存放的位置,php.ini放其他位置并不会生效

在这里插入图片描述



【2】示例2



cat mysql.php (php7以下版本)

 


cat mysqli.php (php7)


在这里插入图片描述

http://123.123.123.123/mysqli.php

访问页面得到OK即为成功(此php文件功能不完全,可能报错忽略即可)




Zabbix安装



zabbix官网链接:
https://www.zabbix.com/download?zabbix=3.4&os_distribution=centos&os_version=6&db=MySQL

zabbix安装可参考链接:
https://blog.csdn.net/GX_1_11_real/article/details/80872582



【1】安装依赖包


yum install  net-snmp-devel curl curl-devel libevent-devel libssh2-devel  -y


【2】zabbix安装



<1>zabbix用户创建

groupadd zabbix
useradd -g zabbix -s /sbin/nologin  -M zabbix 


<2>从官网下载

tar xf zabbix-4.2.4.tar.gz -C /apk/package
cd /apk/package/zabbix-4.2.4


<3>进入数据库,创建zabbix库

mysql -uroot -p'密码'
  
create database zabbix character set utf8;
grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by 'zabbix';
flush privileges;


<4>导入数据

cd /apk/package/zabbix-4.2.4/database/mysql

mysql -uroot -p '密码' zabbix < schema.sql
mysql -uroot -p '密码' zabbix < images.sql 
mysql -uroot -p '密码' zabbix < data.sql


或

mysql -uzabbix -pzabbix zabbix < schema.sql 
mysql -uzabbix -pzabbix zabbix < images.sql 
mysql -uzabbix -pzabbix zabbix < data.sql 


<5>编译安装

 cd /apk/package/zabbix-4.2.4
 ./configure --prefix=/apk/install/zabbix-4.2.4 --enable-server --enable-agent --with-mysql --enable-ipv6 --with-net-snmp --with-libcurl --with-libxml

–prefix 指定安装目录
–enable-ipv6如报错,可去掉




Zabbix配置



【1】zabbix的前端文件配置


(将前端文件发送到nginx配置的web目录)


cp -r /apk/package/zabbix-4.2.4/frontends/php/* /apk/data/web/zabbix


【2】zabbix-server配置文件配置


cd /apk/install/zabbix-4.2.4/etc
egrep -v "^$|^#" zabbix_server.conf

LogFile=/var/log/zabbix/zabbix_server.log
DBHost=123.123.123.123
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
DBSocket=/tmp/mysql.sock
DBPort=3306
StartPollers=30
StartDiscoverers=5
CacheSize=8G
Timeout=4
#AlertScriptsPath=/apk/install/zabbix-4.2.4/altscripts
LogSlowQueries=3000


【3】zabbix-server启动配置


<1>cp启动文件到/etc/init.d/下

cp /apk/package/zabbix-4.2.4/misc/init.d/fedora/core/zabbix_server  /etc/rc.d/init.d/zabbix_server


<2>修改BASEDIR为zabbix-server的安装目录

vim /etc/init.d/zabbix_server

LNMP+zabbix监控平台搭建_第3张图片



<3>启动zabbix-server

chmod + x /etc/init.d/zabbix_server
chkconfig   --add zabbix_server
chkconfig zabbix_server on
chkconfig --list | grep zabbix_server

service zabbix_server start


【4】zabbix-server的web配置



<1>访问zabbix-server

http://123.123.123.123/setup.php

LNMP+zabbix监控平台搭建_第4张图片



<2>根据提示,修改zabbix的php.ini的配置文件
LNMP+zabbix监控平台搭建_第5张图片



<3>填入相关信息

LNMP+zabbix监控平台搭建_第6张图片



<4>确认信息后,点击next

LNMP+zabbix监控平台搭建_第7张图片

LNMP+zabbix监控平台搭建_第8张图片



<5>配置zabbix关联数据库的php文件

LNMP+zabbix监控平台搭建_第9张图片

cp /apk/data/web/zabbix/conf/zabbix.conf.php.example /apk/data/web/zabbix/conf/zabbix.conf.php


cat /App/data/web/zabbix/conf/zabbix.conf.php
(填入数据库账号及密码等)

     'http://localhost:9200',
    //		'text' => 'http://localhost:9200'
    //];
    // Value types stored in Elasticsearch.
    //$HISTORY['types'] = ['uint', 'text'];


<6>重新加载nginx,点击Login进入zabbix界面

nginx -s reload

LNMP+zabbix监控平台搭建_第10张图片

LNMP+zabbix监控平台搭建_第11张图片



<7>输入默认的用户名Admin,及密码zabbix,点击Sign in ,进入zabbix正式的页面(初始为英文版面)

LNMP+zabbix监控平台搭建_第12张图片



<8>可切换语言及修改密码

在这里插入图片描述
LNMP+zabbix监控平台搭建_第13张图片




Zabbix4.2图形乱码问题解决


zabbix部分问题解决,可查看下列链接:
https://blog.csdn.net/GX_1_11_real/article/details/89149722



<1>配置字体库目录及字体

vim /apk/data/web/zabbix/include/defines.inc.php

在这里插入图片描述



<2>从windows中导入字体(可使用rz或其他方式上传至服务器)

也可根据需求导入其他字体,并配置

字体位置:c:\windows\Fonts

LNMP+zabbix监控平台搭建_第14张图片



<3>将上传的文件放到/apk/data/web/zabbix/assets/fonts下

cd /apk/data/web/zabbix/assets/fonts/

在这里插入图片描述



<4>刷新页面即可(如无变化,重启nginx及zabbix_server)



你可能感兴趣的:(Linux各类服务搭建,服务监控)