温馨提示×

Ubuntu下Apache日志分割方法

小樊
45
2025-08-06 23:02:31
栏目: 智能运维

Ubuntu下Apache日志分割主要有以下两种方法:

  • 使用rotatelogs模块:编辑Apache配置文件(如/etc/apache2/apache2.conf/etc/httpd/conf/httpd.conf),在ErrorLogCustomLog指令中使用管道符|调用rotatelogs,指定日志路径和分割间隔(如86400秒为每天分割)。例如:
    ErrorLog "|/usr/sbin/rotatelogs /var/log/apache2/error_%Y%m%d.log 86400"
    CustomLog "|/usr/sbin/rotatelogs /var/log/apache2/access_%Y%m%d.log 86400" combined
    
    重启Apache服务生效:sudo systemctl restart apache2
  • 使用logrotate工具:创建配置文件(如/etc/logrotate.d/apache2),定义日志轮转规则,包括轮转频率(如daily)、保留天数(如rotate 7)、压缩选项(如compress)等。例如:
    /var/log/apache2/*.log {
        daily
        missingok
        rotate 7
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        postrotate
            /usr/sbin/service apache2 reload > /dev/null
        endscript
    }
    
    该工具会自动按规则分割并管理日志文件。

0