LoginSignup
45
48

More than 5 years have passed since last update.

logrotateによるApacheログローテーション設定

Last updated at Posted at 2014-12-21

Apacheのログファイルはrotatelogsでローティションさせる事が出来ますが、logrotate.dでもApacheのログファイルをローティションさせる事が可能です。以下はlogrotateにより1日単位でApacheのログローティションさせる場合の設定メモです。

/etc/logrotate.d/httpdに以下のように記述しておくと、1日単位でApacheログファイルをローティション出来ます。AmazonLinux + Apache2.2.29環境で確認しました。

■環境
AmazonLinux
Apache2.2.29

■ログローティションさせたいApacheログファイル
/var/log/httpd/access_log
/var/log/httpd/error_log

■ログローティション設定
以下のファイルを作成します。

# vi /etc/logrotate.d/httpd
/var/log/httpd/*log {
    daily
    missingok
    rotate 7
    notifempty
    sharedscripts
    compress
    delaycompress
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}
# chmod 644 /etc/logrotate.d/httpd

■ログローティション設定テスト
エラーが出ない事を確認します。

# logrotate -dv /etc/logrotate.d/httpd
reading config file /etc/logrotate.d/httpd
reading config info for /var/log/httpd/*log

Handling 1 logs

rotating pattern: /var/log/httpd/*log after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/httpd/access_log
  log does not need rotating
considering log /var/log/httpd/error_log
not running postrotate script, since no logs were rotated

■ログローティション結果確認
日単位でログローティションされている事を確認します。

# ls -lrt /var/log/httpd/
 (略)
-rw-r--r-- 1 root root     607 12月 20 03:09 error_log-20141219.gz
-rw-r--r-- 1 root root    2569 12月 20 03:09 access_log-20141219.gz
-rw-r--r-- 1 root root 2523077 12月 21 03:31 access_log-20141221
-rw-r--r-- 1 root root     215 12月 21 03:32 error_log-20141220.gz
-rw-r--r-- 1 root root   50743 12月 21 03:32 access_log-20141220.gz
-rw-r--r-- 1 root root     332 12月 21 03:32 error_log-20141221
-rw-r--r-- 1 root root     256 12月 21 03:32 error_log
-rw-r--r-- 1 root root 1931294 12月 21 20:41 access_log
# 
45
48
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
48