How to install and configure logrotate in Alpine Linux

Posted on in Categories , , last updated May 29, 2017

I installed Alpine Linux as LXD (“Linux Container”). How do I install logrotate to configure log rotating for Nginx server?

You need to use the apk command command to install logrotate. It is an easy to use sysadmin tool that manages large numbers of log files. You can do automatic rotation, compression, removal and much more. This tutorial shows you how to manage log files with logrotate on Alpine Linux running in lxd or VM or any other cloud service.

Alpine Linux install and configure logrotate

Installation

Type the following command:
# apk add logrotate
Sample outputs:

(1/2) Installing popt (1.16-r6)
(2/2) Installing logrotate (3.10.0-r0)
Executing busybox-1.25.1-r0.trigger
OK: 89 MiB in 51 packages

Configuration

Your logrotate will get called everyday using a cron job. Here is a default cronjob:
# cat /etc/periodic/daily/logrotate
Sample outputs:

#!/bin/sh
 
if [ -f /etc/conf.d/logrotate ]; then
	. /etc/conf.d/logrotate
fi
 
if [ -x /usr/bin/cpulimit ] && [ -n "$CPULIMIT" ]; then
	_cpulimit="/usr/bin/cpulimit --limit=$CPULIMIT"
fi
 
$_cpulimit /usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

The default logrotate file is located at /etc/logrotate.conf:
# cat /etc/logrotate.conf
Sample outputs:

# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# exclude alpine files
tabooext + .apk-new

# uncomment this if you want your log files compressed
compress

# main log file
/var/log/messages {}

# apk packages drop log rotation information into this directory
include /etc/logrotate.d

# system-specific logs may be also be configured here.

For nginx server create/update /etc/logrotate.d/nginx file as follows:
# cat /etc/logrotate.d/nginx
Sample outputs:

/var/log/nginx/*.log {
	missingok
	sharedscripts
	postrotate
	  /etc/init.d/nginx --quiet --ifstarted reopen
	endscript
}

What this means is that:

  1. /var/log/nginx/*.log – Work on all log files in /var/log/nginx/ directory.
  2. missingok – Do not halt on any error and carries on with the next log file.
  3. sharedscripts – The sharedscripts means that the postrotate script will only be run once (after the old logs have been compressed), not once for each log which is rotated.
  4. postrotate ... script ... endscript – Run this script after the old logs have been compressed. In this case reopen log files for nginx.

This will rotate log file every week. For more info see logrotate(8) man page.

This entry is 2 of 2 in the Installing Linux, Nginx, MySQL/MariaDB, PHP (LEMP stack) in Alpine Linux series. Keep reading the rest of the series:
  1. Install Nginx On Alpine Linux
  2. How to install and configure logrotate

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.