1. 程式人生 > >分散式lnmp架構上部署zabbix監控服務

分散式lnmp架構上部署zabbix監控服務

環境說明
系統 IP 服務
redhat7 192.168.225.128 nginx
redhat7 192.168.225.129 mysql
redhat7 192.168.225.130 php

lnmp部署流程

nginx --> mysql --> php
  • 在192.168.225.128上安裝nginx
//建立系統使用者nginx
[[email protected] ~]# groupadd -r nginx
[[email protected] ~]# useradd -r -M -s /sbin/nologin nginx -g nginx
//安裝依賴環境
[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[[email protected] ~]# yum -y groups install 'Development Tools'

//建立日誌存放目錄
[
[email protected]
~]# mkdir -p /var/log/nginx [[email protected] ~]# chown -R nginx.nginx /var/log/nginx/ //下載nginx [[email protected] ~]# cd /usr/src/ [[email protected] src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2018-08-20 11:19:09-- http://nginx.org/download/nginx-1.14.0.tar.gz [[email protected]
src]# ls debug kernels nginx-1.14.0.tar.gz //編譯安裝 [[email protected] src]# tar xf nginx-1.14.0.tar.gz [[email protected] nginx-1.14.0]# cd nginx-1.14.0/ [[email protected] nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log [[email protected] nginx-1.14.0]# make && make install //nginx安裝後配置 //設定環境變數 [r[email protected] ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh [[email protected] ~]# . /etc/profile.d/nginx.sh //啟動nginx [[email protected] ~]# nginx [[email protected] ~]# 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 :::22 :::* LISTEN 0 100 ::1:25 :::* //關閉防火牆 [[email protected] ~]# systemctl stop firewalld.service //關閉selinux [[email protected] ~]# setenforce 0

在192.168.225.129安裝mysql

//安裝依賴包
[[email protected] ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//建立mysql使用者和組
[[email protected] ~]# groupadd -r -g 306 mysql
[[email protected] ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

//下載二進位制格式的mysql軟體包

[[email protected] local]# ls
apache    bin    include  libexec                                     share
apr       etc    lib      mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz  src
apr-util  games  lib64    sbin

[[email protected] local]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[[email protected] local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"

//修改目錄/usr/local/mysql的屬主屬組
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 9月  27 10:28 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/

//新增環境變數
[[email protected] local]# cd
[[email protected] ~]# ls /usr/local/mysql
bin  COPYING  docs  include  lib  man  README  share  support-files
[[email protected] ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[[email protected] ~]# . /etc/profile.d/mysql.sh 
[[email protected] ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

//建立資料存放目錄
[[email protected] ~]# mkdir /opt/data
[[email protected] ~]# chown -R mysql.mysql /opt/data/
[[email protected] ~]# ll /opt/
總用量 0
drwxr-xr-x. 2 mysql mysql 6 9月  27 10:36 data

//初始化資料庫
[[email protected] ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-10-18T10:06:27.093840Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-18T10:06:27.742780Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-18T10:06:27.821871Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-18T10:06:29.215545Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7b2f63b5-d2bd-11e8-9110-000c294b7cb8.
2018-10-18T10:06:29.216758Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-18T10:06:29.580008Z 1 [Note] A temporary password is generated for [email protected]: ur1),jhW!Isp

//配置mysql
[[email protected] ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[[email protected] ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[[email protected] ~]# ldconfig -v
[[email protected] ~]# ldconfig -p |grep mysql
	libmysqlclient.so.20 (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so.20
	libmysqlclient.so.18 (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so.18
	libmysqlclient.so (libc6,x86-64) => /usr/lib64/mysql/libmysqlclient.so
	libmysqlclient.so (libc6,x86-64) => /usr/local/mysql/lib/libmysqlclient.so

//生成配置檔案
[[email protected] ~]# vim /etc/my.cnf
[[email protected] ~]# cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
socket=/tmp/mysql.sock
port=3306
pid-file=/opt/data/mysql.pid
user=mysql
skip-name-resolve

//配置服務啟動指令碼
[[email protected] ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[[email protected] ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld 
[[email protected] ~]# sed -ri 's#^(datadir=).*#\1/opt/data/#g' /etc/init.d/mysqld 

//啟動mysql
[[email protected] ~]# service mysqld start 
Starting MySQL.Logging to '/opt/data/hxdserver.err'.
 SUCCESS! 
[[email protected] ~]# ps -ef |grep mysql
root      57089      1  0 11:09 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     57267  57089  3 11:09 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=hxdserver.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      57297  12647  0 11:10 pts/1    00:00:00 grep --color=auto mysql
[[email protected] ~]# 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             :::22                          :::*                  
LISTEN     0      100            ::1:25                          :::*                  
LISTEN     0      80              :::3306                        :::* 
//修改密碼
//使用初始密碼登入
[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23

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> set password = password('hxd123456.');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye


在192.168.225.130上安裝php

//配置yum源
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]#  wget http://mirrors.163.com/.help/CentOS7-Base-163.repo

[[email protected] yum.repos.d]#  sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[[email protected] yum.repos.d]#  sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[[email protected] yum.repos.d]# yum clean all 

[[email protected] yum.repos.d]# vim xx.repo 
//這裡將本地源關閉了
[[email protected] yum.repos.d]# yum install -y  libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel 

//下載php
[[email protected] src]# wget  http://cn.php.net/distributions/php-7.2.8.tar.xz
[[email protected] src]# tar xf php-7.2.8.tar.xz
[[email protected] src]# cd php-7.2.8/
[[email protected] php-7.2.8]# ./configure --prefix=/usr/local/php7 --with-curl  --with-freetype-dir  --with-gd  --with-gettext  --with-iconv-dir  --with-kerberos  --with-libdir=lib64  --with-libxml-dir=/usr  --with-openssl  --with-pcre-regex  --with-pdo-mysql  --with-pdo-sqlite  --with-pear  --with-jpeg-dir  --with-png-dir  --with-xmlrpc  --with-xsl  --with-zlib  --with-config-file-path=/etc  --with-config-file-scan-dir=/etc/php.d  --with-bz2  --enable-fpm  --enable-bcmath  --enable-libxml  --enable-inline-optimization  --enable-mbregex  --enable-mbstring  --enable-opcache  --enable-pcntl  --enable-shmop  --enable-soap  --enable-sockets  --enable-sysvsem --enable-xml  --enable-zip --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

[ro[email protected] php-7.2.8]#  make 
當配置PHP時出現  make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1 時,是因為伺服器記憶體不足1G。
只需要在配置命令中新增 --disable-fileinfo即可 。
[[email protected] php-7.2.8]# make install
//安裝後配置
[[email protected] ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[[email protected] ~]# source /etc/profile.d/php7.sh 
[[email protected] ~]# which php 
/usr/local/php7/bin/php
[[email protected] ~]# php -v 
PHP 7.2.8 (cli) (built: Sep 27 2018 12:01:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

//配置php-fpm
[[email protected] php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 
[[email protected] php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm 
[[email protected] php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[[email protected] php-7.2.8]#  cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

//編輯php-fpm的配置檔案/usr/local/php7/etc/php-fpm.conf 
//配置fpm的相關選項改為所需要的值
[[email protected] php-7.2.8]# vim /usr/local/php7/etc/php-fpm.conf
[[email protected] php-7.2.8]#  tail /usr/local/php7/etc/php-fpm.conf 
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it's been set (-p argument)
;  - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5 
pm.min_spare_servers = 2 
pm.max_spare_servers = 8

//啟動php-fpm
[[email protected] php-7.2.8]# service php-fpm start 
Starting php-fpm  done
[[email protected] php-7.2.8]#  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                        :::*

//fpm預設監聽在127.0.0.1的9000埠

  • 192.168.225.129
[[email protected] ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 MySQL Community Server (GPL)

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> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.06 sec)

mysql> grant all privileges on zabbix.* to [email protected] identified by 'zabbix123!';  //給nginx伺服器登入zabbix的許可權
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

  • 192.168.225.128
//下載依賴包
[[email protected] ~]# yum -y install net-snmp-devel libevent-devel mariadb mysql-devel libcurl-devel libxml2-devel
[[email protected] src]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.12/zabbix-3.4.12.tar.gz
[[email protected] src]# tar xf zabbix-3.4.12.tar.gz
//建立zabbix使用者和組
[[email protected] ~]# groupadd -r zabbix 
[[email protected] ~]# useradd -r -g zabbix -M -s /sbin/nologin zabbix
[[email protected] ~]# cd zabbix-3.4.12/database/mysql/
[[email protected] mysql]# mysql -h 192.168.225.129 -uzabbix -pzabbix123! zabbix < schema.sql
[r[email protected] mysql]# mysql -h 192.168.225.129 -uzabbix -pzabbix123! zabbix < images.sql
[[email protected] mysql]# mysql -h 192.168.225.129 -uzabbix -pzabbix123! zabbix < data.sql
[[email protected] zabbix-3.4.12]# ./configure --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2
[[email protected] zabbix-3.4.12]# make install
[[email protected] ~]# vim /usr/local/etc/zabbix_server.conf
DBPassword=zabbix123!
[[email protected] ~]# zabbix_server
[[email protected] ~]# zabbix_agentd
 
//修改nginx的配置檔案
[[email protected] ~]# 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;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
 location / {
            root   /www/aaa;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
        location ~ \.php$ {
            fastcgi_pass   192.168.225.130:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /www/aaa$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
  #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}



  • 192.168.225.130修改php配置檔案
[[email protected] ~]# sed -ri 's/(post_max_size =).*/\1 16M/g' /etc/php.ini
[[email protected] ~]# sed -ri 's/(max_execution_time =).*/\1 300/g' /etc/php.ini
[[email protected] ~]# sed -ri 's/(max_input_time =).*/\1 300/g' /etc/php.ini
[[email protected] ~]# sed -i '/;date.timezone/a date.timezone = Asia/Shanghai' /etc/php.ini
[[email protected] ~]# mkdir /www/aaa/ -p
[[email protected] ~]# groupadd -r nginx 
[[email protected] ~]# chown -R nginx.nginx /www/

[[email protected] ~]# yum -y install php-mysqld
[[email protected] ~]# cd /root/php-7.2.8/ext/mysqli/
[[email protected] mysqli]# phpize
[[email protected] mysqli]# ./configure --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
[[email protected] mysqli]# make && make install
[[email protected] mysqli]# vim /etc/php.ini 
extension=mysqli.so        //新增此行
[roo[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# systemctl start nfs-server
[[email protected] ~]# vim /etc/exports
/www/aaa 192.168.225.128(rw,no_root_squash)


  • 192.168.225.128
[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# mount -t nfs 192.168.225.130:/www/aaa/ /www/aaa/
[[email protected] ~]# cp -r /root/zabbix-3.4.12/frontends/php/* /www/aaa/
[[email protected] ~]# chmod 777 /www/aaa/conf

測試
在這裡插入圖片描述