Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

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
# 
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした