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 logrotateOnce 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:
weeklyTo 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 {
weeklyrn rotate 4
compressrn delaycompressrn missingokrn notifemptyrn}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.confThis 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.confBy default, logrotate is run daily through the cron service.
