Apache2 、nginx分割log日志

来源:互联网 发布:sap native sql 编辑:程序博客网 时间:2024/05/14 15:09

一、Apache2

1、下载cronolog

# cd /usr/local
# wget "http://download.chinaunix.net/down.php?id=3457&ResourceID=1851&site=1"

2、解压缩

# tar -xzvf cronolog-1.6.2.tar.gz

3、进入目录

# cd cronolog-1.6.2

4、运行安装

# ./configure# make# make install

5、配置apache2.conf

# vim /etc/apache2/apache2.conf

添加

# CustomLog "|/usr/local/sbin/cronolog /mnt/logs/apache2/other_vhosts_access.log_%Y%m%d" vhost_combined

重启httpd服务

二、Nginx
博主这里介绍logrotate来切割日志,logrotate是系统自带的服务,可以切割任何日志,不仅仅是nginx,这里推荐使用它。
步骤1

# vim /etc/logrotate.d/nginx

编辑内容:

/usr/local/nginx/logs/access.log {   daily rotate 7  missingok notifempty dateext sharedscripts postrotate     if [ -f /usr/local/nginx/logs/nginx.pid ]; then         kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`     fi endscript }

步骤2:测试是否可用

# /usr/sbin/logrotate -f /etc/logrotate.d/nginx

步骤3:配置定时任务

# vim /etc/crontable

添加:

00 00 * * *  /usr/sbin/logrotate -f /etc/logrotate.d/nginx
0 0