搭建nginx+mysql+loganalyzer+rsyslog日誌服務器

  1. 咱們事先規劃好兩臺主機,分別爲:php

  2. server-1:192.168.1.135html

  3. server-2:192.168.1.136前端

  4. 將server-1做爲客戶端進行日誌收集,導入到server-2的MySQL數據庫中,並經過LogAnalyzer進行前端的web展現。mysql

  5. 首先是對於server-2的配置linux

  6. 安裝LNMPnginx

  7.  yum install nginx php -php-fpm php-mysql mysql-server mysql -yweb

  8. //這裏爲啥要安裝php-fpm,由於php-fpm,是nginx和php的橋樑,php-fpm(快速進程管理),php-fpm默認進程爲127.0.0.1:9000,sql

  9. //一會php和php-fpm安裝完成後,要配置nginx的配置文件,讓其遇到客戶端php請求是,轉發給php-fpm(127.0.0.1:9000),php-fpm再讓php解析完成,最後又給nginx.數據庫

  10. 啓動數據庫vim

  11. systemctl start mysqld

  12. 加入開機自啓動

  13. systemctl enable mysqld

  14. 配置數據庫密碼

  15. [root@server-2 ~]# mysql
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 2
    Server version: 5.5.60-MariaDB MariaDB Server
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    MariaDB [(none)]> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    Database changed
    MariaDB [mysql]> update user set password=password('123456') where user='root';
    Query OK, 4 rows affected (0.00 sec)
    Rows matched: 4  Changed: 4  Warnings: 0
    MariaDB [mysql]> flush privileges;
    Query OK, 0 rows affected (0.01 sec)
    MariaDB [(none)]> grant all on *.* to 'rsyslog'@'%' identified by 'password123';//建立用戶並授予
    Query OK, 0 rows affected (0.02 sec)
    MariaDB [mysql]> exit;
    Bye
  16. 配置nginx

  17. [root@server-2 ~]# cd /etc/nginx/
    [root@server-2 nginx]# egrep -v "#|^$" nginx.conf.default >nginx.conf
    [root@server-2 nginx]# vim nginx.conf
    worker_processes  1;
    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;
            }
            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;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
  18. 檢查配置文件語法並啓動

  19. [root@server-2 nginx]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@server-2 nginx]# nginx
    [root@server-2 nginx]# service php-fpm start
    Redirecting to /bin/systemctl start php-fpm.service
  20. 檢查端口啓動狀況

  21. [root@server-2 nginx]# ss -lnt
    State      Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
    LISTEN     0      128                                    127.0.0.1:9000                                                       *:*                  
    LISTEN     0      50                                             *:3306                                                       *:*                  
    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                                                        :::*       
    在firewalld中容許端口80,3306,22經過
    [root@server-2 ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
    success
    [root@server-2 ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
    success
    [root@server-2 ~]# firewall-cmd --zone=public --add-port=22/tcp --permanent
    success
    [root@server-2 ~]# firewall-cmd --reload
    success
  22. 查看防火牆開放端口列表

  23. [root@server-2 ~]# firewall-cmd --zone=public --list-ports

  24. 80/tcp 3306/tcp 22/tcp

  25. 關閉selinux

  26. [root@server-2 ~]# setenforce 0

  27. [root@server-2 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 

  28. 建立PHP測試頁,檢查nginx和php的鏈接狀況;

  29. [root@server-2 html]# echo "<?php phpinfo(); ?>" >test.php

  30. 訪問動態網頁

  31. image.png

  32. 客戶端的配置

  33. 在客戶端主機server-1上192.168.1.135

  34. 檢查rsyslog是否安裝,默認是安裝過的

  35. [root@server-1 ~]# rpm -qa rsyslog

  36. rsyslog-8.24.0-34.el7.x86_64

  37. [root@server-1 ~]# yum install rsyslog-mysql mysql -y //rsyslog使用此模塊將數據傳入MySQL數據庫,必須安裝;最小化安裝,沒有mysql命令,此操做就是爲了有mysql命令,能夠鏈接數據庫

  38. 導入庫文件 ,使用rsyslog用戶

  39. [root@server-1 ~]# mysql -h192.168.1.136 -ursyslog -ppassword123 </usr/share/doc/rsyslog-8.24.0/mysql-createDB.sql

  40. 登陸數據庫查看庫是否存在

  41. 345.PNG

  42. vim /etc/rsyslog.conf
    .......
    #### MODULES ####
    # The imjournal module bellow is now used as a message source instead of imuxsock.
    $ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
    $ModLoad imjournal # provides access to the systemd journal
    $ModLoad ommysql
    #$ModLoad imklog # reads kernel messages (the same are read from journald)
    #$ModLoad immark  # provides --MARK-- message capability
    # Provides UDP syslog reception
    $ModLoad imudp
    $UDPServerRun 514
    # Provides TCP syslog reception
    #$ModLoad imtcp
    #$InputTCPServerRun 514
    ......
    #### RULES ####
    # Log all kernel messages to the console.
    # Logging much else clutters up the screen.
    #kern.*                                                 /dev/console
    # Log anything (except mail) of level info or higher.
    # Don't log private authentication messages!
    #*.info;mail.none;authpriv.none;cron.none                /var/log/messages
    *.info;mail.none;authpriv.none;cron.none :ommysql:192.168.1.136,Syslog,syslog,password123
    # The authpriv file has restricted access.
    .......

  43. 6789.PNG

  44. 添加mysql模塊ommysql

  45. 取消註釋使用udp協議的514端口通訊

  46. image.png

  47. 指明發送的主機,數據庫,用戶名,密碼

  48. 配置完畢,啓動rsyslog

  49. systemctl start rsyslog

  50. 在server-2端192.168.1.136安裝LogAnalyzer

  51. 下載LogAnalyzer v4.1.6    官網地址:http://loganalyzer.adiscon.com/downloads/

  52. [root@server-2 home]# wget http://download.adiscon.com/loganalyzer/loganalyzer-4.1.6.tar.gz    #下載安裝包。

  53. 安裝LogAnalyzer

  54. [root@server-2 home]# tar -xf loganalyzer-4.1.6.tar.gz 

  55. [root@server-2 home]# ll

  56. total 2788

  57. drwxrwxr-x. 5 root root      90 Nov  6  2017 loganalyzer-4.1.6

  58. -rw-r--r--. 1 root root 2852860 Nov  6  2017 loganalyzer-4.1.6.tar.gz

  59. 配置loganalyze 

  60. cp -r /home/loganalyzer-4.1.6/src/* /usr/share/nginx/html/
    cp -r /home/loganalyzer-4.1.6/contrib/* /usr/share/nginx/html/
    cd /usr/share/nginx/html/
    chmod +x *.sh
    ./configure.sh
    ./secure.sh
    chmod 666 config.php
  61. 訪問http://192.168.1.136/index.php

  62. image.png

  63. web1.png

  64. web2.png

  65. image.png

  66. image.pngimage.pngimage.png

相關文章
相關標籤/搜索