Ubuntu Apache日志存储周期变更

文章目录

今天我们的环境是Ubuntu 22.04,更改apache的日志存储周期。

了解apache日志规则

apache的日志规则是在如下目录:

bash 复制代码
 sudo vi /etc/logrotate.d/apache2 

默认的规则

bash 复制代码
/var/log/apache2/*.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then
                        run-parts /etc/logrotate.d/httpd-prerotate
                fi
        endscript
        postrotate
                if pgrep -f ^/usr/sbin/apache2 > /dev/null; then
                        invoke-rc.d apache2 reload 2>&1 | logger -t apache2.logrotate
                fi
        endscript
}

它的主要逻辑是:

  • daily:每天轮转一次日志。
  • rotate 14:保留最近 14 天的旧日志。
  • compress & delaycompress:对旧日志进行压缩,但延迟一次压缩(即昨天的日志不压缩,前天的才压缩),方便查看最近的日志。
  • postrotate:在日志轮转后,如果 Apache2 进程正在运行,就优雅地重新加载(reload)它,以便 Apache 开始写入新的日志文件。

Apache日志规则调整

现在业务需求调整,需要将原来每天压缩日志,改为14天。改为:每周压缩一次,保留三个月,也就是12周。

需要修改的参数是:

  • daily ➡️ 改为 weekly:将日志轮转频率从每天一次调整为每周一次。
  • rotate 14 ➡️ 改为 rotate 12:因为每个月大约 4 周,3 个月就是 12 周。这样刚好可以保留最近 12 个星期的历史日志。

修改后的完整配置参考如下:

bash 复制代码
/var/log/apache2/*.log {
        weekly          # 修改为每周
        missingok
        rotate 12       # 修改为保留12份(即约3个月的时间)
        compress
        delaycompress
        notifempty
        create 640 root adm
        sharedscripts
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then
                        run-parts /etc/logrotate.d/httpd-prerotate
                fi
        endscript
        postrotate
                if pgrep -f ^/usr/sbin/apache2 > /dev/null; then
                        invoke-rc.d apache2 reload 2>&1 | logger -t apache2.logrotate
                fi
        endscript
}

日志规则调整重要提醒

因为我们用的是Ubuntu的os,不建议直接修改/etc/logrotate.d/apache2,可以新建一个自定义配置文件来覆盖它:

bash 复制代码
sudo vi /etc/logrotate.d/apache2-custom

Logrotate 会读取该目录下的所有文件,新文件的规则会生效。这样也可以避免apache 更新时,覆盖原来的默认文件,影响规则的生效。

规则调整生效时间

修改配置后,不会立即生效,需要等待下一次 logrotate 的自动执行周期。

因为我们将轮转频率改为了 weekly,所以你需要等到下一次每周的固定轮转日才会真正产生新的日志文件。

如果你是在周一凌晨刚轮转过之后修改的配置,那么最长可能需要等待 7天 才能看到第一次新规则的生效;

如果刚好在下次轮转前修改,可能几个小时内就会生效。

那么有没有什么方法可以让规则立即生效?可以手动强制运行一次 logrotate,让其立即生效。

bash 复制代码
sudo logrotate -d /etc/logrotate.d/apache2-custom

执行后,会输出类似如下:

bash 复制代码
warning: logrotate in debug mode does nothing except printing debug messages!  Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/apache2-custom
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state
Creating new state

Handling 1 logs

rotating pattern: /var/log/apache2/*.log  weekly (12 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/apache2/access.log
  Now: 2026-06-08 15:37
  Last rotated at 2026-06-07 00:00
  log does not need rotating (log has been rotated at 2026-06-07 00:00, which is less than a week ago)
considering log /var/log/apache2/error.log
  Now: 2026-06-08 15:37
  Last rotated at 2026-06-08 00:00
  log does not need rotating (log has been rotated at 2026-06-08 00:00, which is less than a week ago)
considering log /var/log/apache2/other_vhosts_access.log
  Now: 2026-06-08 15:37
  Last rotated at 2024-05-18 00:00
  log does not need rotating (log is empty)
not running prerotate script, since no logs will be rotated
not running postrotate script, since no logs were rotated

推荐阅读

相关推荐
慕伏白1 小时前
【慕伏白】Linux 系统如何根据端口号关闭进程
linux·运维·服务器
letisgo51 小时前
VMware Workstation + Ubuntu 26.04 LTS 小白运维部署手册
linux·运维·ubuntu
IT探索1 小时前
Linux 查找文件指令总结
linux·算法
晚风吹长发1 小时前
Docker使用——Docker容器及相关命令
linux·运维·服务器·docker·容器·架构
Gent_倪2 小时前
数据治理之元数据管理:Apache Atlas
apache
sulikey2 小时前
个人Linux操作系统学习笔记11 - 环境变量
linux·笔记·学习
Kina_C2 小时前
NFS与Autofs快速讲解-从原理到实战配置
linux·nfs·autofs
皮卡狮2 小时前
文件系统:磁盘硬件寻址和系统软件寻址
linux
是潮汕的灿灿展吖2 小时前
Centos离线部署nfs操作
linux·运维·centos
Kina_C2 小时前
Apache HTTP Server 安装、配置与高级功能详解
linux·http·apache