How to install and configure logrotate in Alpine Linux

In Alpine Linux, you can install the logrotate package using the apk package manager:

$ apk add logrotate

Once logrotate is installed, you can configure it by editing the /etc/logrotate.conf file. This file contains the global options for logrotate, such as the default rotation interval and the default compression method.

For example, to rotate logs on a weekly basis, you could add the following line to the /etc/logrotate.conf file:

weekly

To configure log rotation for a specific log file, you can create a separate configuration file in the /etc/logrotate.d directory. For example, to rotate the /var/log/nginx/access.log file, you could create a file named /etc/logrotate.d/nginx with the following content:

/var/log/nginx/access.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
}

This configuration rotates the access.log file weekly, keeps 4 rotations, compresses the rotated logs, and continues compressing even if the log is missing.

After making changes to the logrotate configuration, you can test the configuration by running the following command:

$ logrotate -d /etc/logrotate.conf

This will display the actions that logrotate would take without actually rotating the logs. If everything is configured correctly, you can run the following command to actually rotate the logs:

$ logrotate /etc/logrotate.conf

By default, logrotate is run daily through the cron service.

Leave a Comment