Apache和logrotate配置

sis*_*ssy 30 apache ubuntu apache2 logrotate

上周我在我的服务器上发现了一个问题,因为磁盘使用率是100%,我发现apache创建了一个60GB的巨大error.log文件.然后我将LogLevel更改为emerg,但是在一周之后它又是1.3GB,这肯定是太多了.此外,我有一个6MB的access.log和167MB的other_vhosts_access.log.所以我发现问题可能是logrotate无法正常工作.实际上,日志的gzip文件有一个非常旧的日期(2月23日).所以我首先尝试更改apache2的logrotate文件的配置,为文件添加最大大小,现在看起来像这样:

/var/log/apache2/*.log {
    weekly
    size 500M
    missingok
    rotate 20
    compress
    delaycompress
    notifempty
    create 640 root adm
    sharedscripts
    postrotate
                if /etc/init.d/apache2 status > /dev/null ; then \
                    /etc/init.d/apache2 reload > /dev/null; \
                fi;
    endscript
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi; \
    endscript
}
Run Code Online (Sandbox Code Playgroud)

在此之后,我尝试手动强制logrotate运行apache的特定配置

logrotate -f /etc/logrotate.d/apache2
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误:

error: skipping "/var/log/apache2/access.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
error: skipping "/var/log/apache2/error.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
error: skipping "/var/log/apache2/other_vhosts_access.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
Run Code Online (Sandbox Code Playgroud)

奇怪的是,它在某种程度上运行旋转,创建一个空的error.log文件,但具有与旧文件不同的权限,而不是压缩现有的error.log.看看apache日志目录,现在看起来像这样:

-rwxrwxrwx  1 root           adm            6.3M Oct 21 10:54 access.log
-rwxrwxrwx  1 root           adm             22K Feb 18  2014 access.log.1
-rwxrwxrwx  1 root           adm            7.0K Feb 16  2014 access.log.2.gz
-rwxrwxrwx  1 root           adm            4.0K Feb  9  2014 access.log.3.gz
-rw-------  1 amministratore amministratore    0 Oct 21 10:32 error.log
-rw-r--r--  1 root           root           1.3G Oct 21 10:57 error.log.1
-rwxrwxrwx  1 root           adm            167M Oct 21 10:57 other_vhosts_access.log
-rwxrwxrwx  1 root           adm            225K Feb 23  2014 other_vhosts_access.log.1
-rwxrwxrwx  1 root           adm             16K Feb 15  2014 other_vhosts_access.log.2.gz
-rwxrwxrwx  1 root           adm            3.2K Feb  8  2014 other_vhosts_access.log.3.gz
Run Code Online (Sandbox Code Playgroud)

那么正确的方法是什么?我应该更改/ var/log/apache2目录的权限吗?(现在是777)我没有设置这些权限,如果它是正确的我不知道.或者我应该告诉logrotate哪个用户用于轮换?如何?

mcn*_*ium 43

只需添加su root adm到配置文件:

/var/log/apache2/*.log {
    # …
    su root adm
}
Run Code Online (Sandbox Code Playgroud)

  • 我不明白为什么logrotate关心这个? (9认同)
  • 答案没有给出任何背景为什么这是一个问题和其他方法来解决它(可能权限是一个问题!).不酷. (6认同)
  • su之后的用户名和组必须加载日志文件.在我的情况下,我不得不添加:su syslog adm (2认同)

sis*_*ssy 32

按照网站的说明,我刚刚更改了logrotate配置文件,添加了请求的su指令,如下所示,现在它以正确的方式旋转.

su <user> <group>
Run Code Online (Sandbox Code Playgroud)

  • 刚给你一个+1.我一直在为一个令人尴尬的长时间拍摄而烦恼,想知道为什么我的anacron logrotate不起作用.我的日志目录是我自己的.在这种情况下,logrotate需要所有者为root.所以我将log dir chown to root并将`su root me`添加到logrotate配置文件中.感谢您发布解决方案. (3认同)
  • 请尽量避免发布外部链接.链接现在坏了.惊喜,惊喜.. (3认同)
  • 您能否总结一下链接停止工作时的说明? (2认同)

Den*_*kov 5

我在尝试强制旋转系统日志时遇到“父目录具有不安全的权限”。
我是这样解决的:

cat /etc/logrotate.conf
    ...
    # use the syslog group by default, since this is the owning group
    # of /var/log/syslog.
    su root syslog

vim /etc/logrotate.d/rsyslog
    # Add to top:
    su root syslog

logrotate -f /etc/logrotate.d/rsyslog
    # No errors now, log is rotated.
Run Code Online (Sandbox Code Playgroud)