Nginx Optimization - split logs

Log split

First, the enterprise server log files over the General Assembly to bring the following questions:
1, the query log file is too large for the development and operation and maintenance is very inconvenient, especially in the case of overtime people tired even worse;
2, a long time ago the log file is almost no value, but are too tedious manual cleaning.
This time you need a solution that can automatically do the log split logs divided not only "clean", but also facilitate regular cleaning log.

experiment

Second, compile and install Nginx service
1, remote access source packages on Windows, Linux and mount onto the

[root@localhost ~]# smbclient -L //192.168.235.1
Enter SAMBA\root's password: 
Sharename       Type      Comment
---------       ----      -------
LNMP            Disk  

[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.235.1/LNMP /abc
Password for root@//192.168.235.1/LNMP:  
[root@localhost ~]# ls /abc
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.0.tar.gz  php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz

2, the packet decompressor

[root@localhost ~]# cd /abc
[root@localhost abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@localhost abc]# ls /opt
nginx-1.12.0  rh

3, the mounting assembly package compiled

[root@localhost abc]# cd /opt
[root@localhost opt]# yum install -y \
> gcc \             //C语言
> gcc-c++ \         //c++语言
> pcre-devel \      //pcre语言工具
> zlib-devel        //压缩函数库

4. Create a user program and configure Nginx services related components

[root@localhost opt]# useradd -M -s /sbin/nologin nginx
//创建程序用户nginx,并限定其不可登录终端
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \            
//配置nginx
> --prefix=//usr/local/nginx \      
//指定安装路径                        
> --user=nginx \
//指定用户名
> --group=nginx \
//指定用户所属组
> --with-http_stub_status_module
//安装状态统计模块

5, compile and install

[root@localhost nginx-1.12.0]# make && make install

6, Nginx optimization service startup script, and the establishment of command soft links

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
//创建nginx服务命令软链接到系统命令
[root@localhost nginx-1.12.0]# systemctl stop firewalld.service 
//关闭防火墙
[root@localhost nginx-1.12.0]# setenforce 0
//关闭增强型安全功能
[root@localhost nginx-1.12.0]# nginx 
//输入nginx 开启服务
[root@localhost nginx-1.12.0]# netstat -ntap | grep 80      //查看服务的80 端口,显示已开启
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7520/nginx: master  

7, systemctl management script nginx

[root@localhost ~]# vim /lib/systemd/system/nginx.service      ##创建配置文件

[Unit]
Description=nginx                                            ##描述
After=network.target                                        ##描述服务类型
[Service]
Type=forking                                                    ##后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid            ##PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx              ##启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID    ##根据PID重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID       ##根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service     ##设置执行权限
[root@localhost ~]# systemctl stop nginx.service       ##关闭nginx 
[root@localhost ~]# systemctl start nginx.service       ##开启

8, write scripts split logs

[root@localhost nginx-1.12.0]# vim fenge.sh

#!/bin/bash
#Filename:fengge.sh                  ##描述信息

d=$(date -d "-1 day" "+%Y%m%d" )           ##显示系统一天前的时间,并生成一个日期字符串,如"2019.11.11"
logs_path="/var/log/nginx"                          ##日志分割后的存放路径
pid_path="/usr/local/nginx/logs/nginx.pid"       ##Nginx的进程号文件
[ -d $logs_path ] || mkdir -p $logs_path         ##判断是否存在日志分割存放路径,如不存在则创建该路径
mv      /usr/local/nginx/logs/access.log     ${logs_path}/test.com-access.log-$d
##移出原有路径下的访问日志生成到创建的路径下,并以日期命名生成一个日志文件
kill   -USR1   $(cat $pid_path)                       ##结束此前的进程号,用以生成新的进程号
find  $logs_path  -mtime  +30  |  xargs  rm  -rf      ##寻找路径下30天前的文件并删除,xargs用于将前面命令的处理结果作为管道符号后的命令的参数
[root@localhost nginx-1.12.0]# chmod  +x  fenge.sh              ##授予脚本执行权限
[root@localhost nginx-1.12.0]# ./ fenge.sh
[root@localhost nginx-1.12.0]#  ls  /var/log/nginx
test.com-access.log-20191112                    ##查看指定路径下生成的的日志分割文件
[root@localhost nginx-1.12.0]# ls  /usr/local/nginx/logs
access.log  error.log  nginx.pid             ##查看Nginx日志目录,可见又自动生成了一个access.log日志

This approach greatly strengthened the enterprise server log management, improve efficiency! !

Guess you like

Origin blog.51cto.com/14475593/2450135