본문 바로가기

리눅스

Apache 로그 logrotate(로테이드) 설정

반응형

Apache 로그 logrotate(로테이드) 설정

Apache 로그 파일을 로테이트하기 위해 logrotate를 설정하는 방법은 다음과 같습니다.

 

1. logrotate 설치

logrotate를 사용하기 위해서는 시스템에 logrotate가 설치되어 있어야 합니다. 대부분의 Linux 배포판에서는 기본적으로 설치되어 있지만, 설치되어 있지 않다면 패키지 관리자를 사용하여 설치해야 합니다.

2. 로그 파일 위치 확인

Apache의 로그 파일 위치를 확인합니다. 기본적으로 Apache의 로그 파일은 /var/log/apache 또는 /var/log/httpd와 같은 디렉토리에 있습니다.

3. logrotate 설정 파일 생성

logrotate 설정 파일을 생성합니다. 이 설정 파일은 /etc/logrotate.d/ 디렉토리에 저장됩니다. 예를 들어, 다음과 같이 /etc/logrotate.d/apache 파일을 생성합니다.

  • logrotate 실행
/sbin/logrotate -f /etc/logrotate.d/apache
$ ls -l /logs/access
www.sangchul.kr-access-ssl-20210107.log.1
728x90

4. 설정 파일 작성

에디터가 열리면 다음과 같이 설정 파일을 작성합니다.

vim /etc/logrotate.d/apache
$ vim /etc/logrotate.d/apache
/logs/access/*-access-*.log
/logs/error/*-error-*.log
{
	daily
	rotate 7
	notifempty
	missingok
	compress
	delaycompress
	sharedscripts
	postrotate
		/bin/kill -HUP `cat /usr/local/apache/logs/httpd.pid 2>/dev/null` 2> /dev/null || true
	endscript
}
  • daily: 매일 로그 파일을 로테이트합니다.
  • missingok: 로그 파일이 존재하지 않아도 오류를 발생시키지 않습니다.
  • rotate 7: 최대 7개의 이전 로그 파일을 유지하고, 그 이후에는 삭제합니다.
  • compress: 로그 파일을 압축합니다.
  • delaycompress: 로그 파일을 압축하기 전에 이전 로그 파일을 하루 지연시킵니다.
  • notifempty: 비어 있지 않은 로그 파일만 로테이트합니다.
  • create: 새로운 로그 파일을 생성할 때, 해당 권한과 소유자를 설정합니다.
  • sharedscripts: 모든 로그 파일에 대해 postrotate 스크립트를 한 번만 실행합니다.
  • postrotateendscript: 로그 파일 로테이션 후 실행할 명령어를 지정합니다. 이 예제에서는 Apache를 다시 로드합니다.

5. 설정 파일 저장

설정 파일을 저장하고 종료합니다.

6. logrotate 테스트

logrotate 설정이 올바로 작동하는지 테스트해봅니다. 다음 명령을 실행하여 logrotate를 수동으로 실행합니다.

  • logrotate 실행(상세 내용 출력)
/sbin/logrotate -v /etc/logrotate.d/apache
$ /sbin/logrotate -v /etc/logrotate.d/apache
reading config file /etc/logrotate.d/apache
Allocating hash table for state file, size 15360 B

Handling 1 logs

rotating pattern: /logs/access/*-access-*.log
/logs/error/*-error-*.log
 after 1 days (7 rotations)
empty log files are not rotated, old logs are removed
considering log /logs/access/test.sangchul.kr-access-20210107.log
  log does not need rotating (log has been already rotated)considering log /logs/access/www.sangchul.kr-access-ssl-20210107.log
  log does not need rotating (log has been already rotated)considering log /logs/error/*-error-*.log
  log /logs/error/*-error-*.log does not exist -- skipping
not running postrotate script, since no logs were rotated

 

이제 logrotate는 Apache 로그 파일을 로테이트하고 압축하는 작업을 수행합니다. 일반적으로 cron 작업으로 logrotate를 주기적으로 실행하도록 설정하는 것이 좋습니다.

 

728x90
반응형

'리눅스' 카테고리의 다른 글

fabric hello print on CentOS 7  (0) 2021.01.11
fabric hello print on CentOS 8  (0) 2021.01.11
[리눅스] diff 명령어  (0) 2020.12.31
리눅스 sed 명령어  (0) 2020.12.31
CentOS 7에서 SoftEther VPN 클라이언트를 설치하는 방법-2  (0) 2020.12.30