ELK 环境搭建 (二)

1.编译环境准备(root用户)

  1. 安装包:
    # yum -y install gcc openssl-devel pcre-devel
  2. 创建nginx用户和组:
    # groupadd -r nginx
    # useradd -r -M -g nginx -s /bin/nologin nginx

2.安装LuaJIT和相关Lua库

  1. LuaJIT安装操作:
    1. 解压软件包:
      tar -xzvf LuaJIT-2.0.4.tar.gz
    2. 在解压后的目录下执行:
      make
      make install
    3. LuaJIT安装验证:
      lua -v
      luajit -v
  2. lua-cjson安装操作:
    1. 解压软件包:
      tar -xzvf lua-cjson-2.1.0.tar.gz
    2. 解压后的目录下执行:
      修改 Makefile 文件中的变量 LUA_INCLUDE_DIR值为:LUA_INCLUDE_DIR = $(PREFIX)/include/luajit-2.0
      make
      cp cjson.so /usr/local/lib/lua/5.1

3.编译安装nginx

  1. 配置环境变量:
    LUAJIT_LIB=/usr/local/lib
    LUAJIT_INC=/usr/local/include/luajit-2.0
  2. 解压软件包:
    tar -xzvf nginx-1.11.10.tar.gz
    tar -xzvf lua-nginx-module-0.10.7.tar.gz
    tar -xzvf ngx_devel_kit-0.3.0.tar.gz
  3. 在nginx解压目录下执行
    1. nginx编译:

      ./configure --prefix=[nginx安装目录] \

      --error-log-path=[nginx安装目录]/log/error.log \

      --http-log-path=[nginx安装目录]/log/access.log \

      --pid-path=[nginx安装目录]/nginx.pid \

      --lock-path=[nginx安装目录]/nginx.lock \

      --user=nginx --group=nginx \

      --with-http_ssl_module \

      --with-http_flv_module \

      --with-http_gzip_static_module \

      --http-client-body-temp-path=[nginx安装目录]/client \

      --http-proxy-temp-path=[nginx安装目录]/proxy \

      --http-fastcgi-temp-path=[nginx安装目录]/fcgi \

      --http-uwsgi-temp-path=[nginx安装目录]/uwsgi \

      --http-scgi-temp-path=[nginx安装目录]/scgi \

      --with-pcre \

      --with-ld-opt="-Wl,-rpath,/usr/local/lib" \

      --add-dynamic-module=[ngx_devel_kit-0.3.0解压目录] \

      --add-dynamic-module=[lua-nginx-module-0.10.7解压目录]

    2. 构建与安装:
      make -j2
      make install
    3. nginx安装验证:
      [nginx安装目录]/sbin/nginx -v

4.修改配置文件

用以下代码覆盖nginx配置文件nginx.conf配置的内容:

user  nginx;
worker_processes  auto;
 
load_module modules/ndk_http_module.so;
load_module modules/ngx_http_lua_module.so;
 
error_log   log/error.log;
 
events {  
    use epoll;
    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"';
 
    log_format my_format '$remote_addr^A$msec^A$http_host^A$request_uri^A$request_body';
     
    access_log  log/access.log  main;
 
    client_header_buffer_size 1m;
     
    large_client_header_buffers 2 1m;
    sendfile        on;  
 
    keepalive_timeout 5;
 
    init_by_lua 'cjson = require "cjson"';
 
    server {  
        listen       80;
        server_name  WEB1;
        add_header X-upS  WEB1-$server_addr:$server_port;
 
    location = /i {
        lua_need_request_body on;                                                                                           
        content_by_lua_block {
        ngx.say(cjson.encode({result = "Success"}))
            }
            access_log log/access_ub.log my_format;
        default_type application/json;
        gzip on;
        gzip_types application/json;
            add_header 'Access-Control-Allow-Origin' '*';
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
    }
}

五、验证服务正常启动

  1. 启动nginx:[nginx安装目录]/sbin/nginx
  2. root下执行lsof -i:80 可以看见nginx进程

六、设置日志滚动

1、在 /etc/logrotate.d/ 下创建文件 touch nginx.conf

2、编辑文件内容:vi nginx.conf

/usr/local/zhangkun/nginx/log/access_ub.log{
    daily                # 日志轮询周期,weekly,monthly,yearly
    rotate 4           #保留四个文件
    copytruncate       #用于还在打开中的日志文件,把当前日志备份并截断
    size 200M          #文件超过200K才会被切割
    dateext            # 使用当前日期作为命名格式
    dateformat -%Y-%m-%d.%s  #文件名格式
    compress           #切割后压缩
    missingok          #没有日志文件也不报错
    notifempty         #日志为空则不切割
    create 777 nginx nginx
    sharedscripts
    postrotate
        [ -f /usr/local/zhangkun/nginx/nginx.pid ] || kill -USR1 `cat /usr/local/zhangkun/nginx/nginx.pid`
    endscript
}

3、添加一条计划任务,每天凌晨12点轮转

# crontab -e

0 0 * * * /usr/sbin/logrotate /etc/logrotate.d/nginx.conf

4、测试执行是否生效

access_ub.log 大于200M时执行

sudo logrotate -f /etc/logrotate.d/nginx

目录下会出现一个压缩文件,并且切割完之后access_ub.log 为0KB

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值