Nginx optimization of the log split

Nginx optimization of the log split

Installation Configuration Nginx

Mount remote source package to a local

mount.cifs //192.168.100.10/LNMP-C7 /mnt        //挂载到/mnt目录下

Extract the source packet to the next directory / opt

[root@localhost ~]# cd /abc                                                       //切换到挂载点目录
[root@localhost abc]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz  php-7.1.10.tar.gz
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt        //解压Nginx源码包到/opt下
[root@localhost abc]# cd /opt/                                                  //切换到解压的目录下
[root@localhost opt]# ls
nginx-1.12.2  rh

Package installation environment required to compile

[root@localhost opt]# yum -y install \
gcc \                                              //c语言
gcc-c++ \                                      //c++语言
pcre-devel \                                  //pcre语言工具
zlib-devel                                     //数据压缩用的函式库

Create a program named users and compile Nginx nginx

[root@localhost opt]# useradd -M -s /sbin/nologin nginx     //创建程序用户,限定其
[root@localhost opt]# cd nginx-1.12.2/                                //切换到nginx目录下
[root@localhost nginx-1.12.2]# ./configure \                        //配置nginx
> --prefix=/usr/local/nginx \                                                  //安装路径
> --user=nginx \                                                                   //用户名
> --group=nginx \                                                                 //用户组
> --with-http_stub_status_module                                       //访问状态统计模块

Compiling and Installing

[root@localhost nginx-1.12.0]# make && make install                               //编译及安装

Nginx production management scripts, easy to use management

[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
                                                                                        //创建软连接                                                                 [root@nginx nginx-1.12.2]# vim /etc/init.d/nginx             //编辑启动脚本
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
    $PROG
    ;;
  stop)
    kill -s QUIT $(cat $PIDF)
    ;;  
  restart)
    $0 stop
    $0 start
    ;;  
  reload)
    kill -s HUP $(cat $PIDF)
    ;;  
  *)    
        echo "Usage: $0 {start|stop|restart|reload}"
        exit 1  
esac            
exit 0
[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx                  //给脚本执行权限
[root@nginx nginx-1.12.2]# chkconfig --add nginx                        //添加到service管理器中
[root@nginx nginx-1.12.2]# yum install elinks -y                           //
[root@nginx nginx-1.12.2]# service nginx start                             //启动Nginx服务
[root@nginx nginx-1.12.2]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80        0.0.0.0:*          LISTEN      42028/nginx: master 
[root@nginx nginx-1.12.2]# systemctl stop firewalld.service                   //关闭防火墙
[root@nginx nginx-1.12.2]# setenforce 0                                          //关闭增强型安全功能
[root@nginx nginx-1.12.2]# elinks http://192.168.131.133/                                                                                                           

Writing scripts for log splitting

[root@localhost ~]# vim fenge.sh            //编写脚本文件

#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d")                //显示一天前的时间
logs_path="/var/log/nginx"                              //分割日志的保存路径
pid_path="/usr/local/nginx/logs/nginx.pid"      //pid的路径
[ -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)                            //结束重新生成新的pid文件
find $logs_path -mtime +30 | xargs rm -rf      //删除30天前的日志文件
[root@localhost ~]# chmod +x fenge.sh        //给执行权限
[root@localhost ~]# ./fenge.sh                      //执行脚本文件

Guess you like

Origin blog.51cto.com/14449528/2451155