In this tutorial, we will show you how to install and configuration Logrotate on Ubuntu 16.04 LTS. For those of you who didn’t know, Logrotate helps to manage your log files. It can periodically read, minimize, back up, creates new log files, and basically anything you may ever want to do with them. This is usually used to help prevent any single log file from getting unwieldy in size. It is commonly also used to delete old log files so as not to fill your server’s hard drive. The most important features of Logrotate are automatic log rotation, log compression, log removal, and emailing the log files.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of Logrotate on a Ubuntu 16.04 (Xenial Xerus) server.
Prerequisites
- A server running one of the following operating systems: Ubuntu 16.04 (Xenial Xerus).
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Logrotate on Ubuntu 16.04 LTS
Step 1. First, make sure that all your system packages are up-to-date by running the following apt-get
commands in the terminal.
sudo apt-get update sudo apt-get upgrade
Step 2. Installing Logrotate on Ubuntu.
To install Logrotate, just use your package manager:
sudo apt-get install logrotate
Verify that the installation was successful:
sudo logrotate
Step 3. Configure Logrotate.
The main configuration file for logrotate is /etc/logrotate.conf
while application-specific configuration files are stored in the /etc/logrotate.d
directory. In stock Ubuntu, any config file you put into /etc/logrotate.d
is going to run once per day. Logs are typically rotated once per day or less (Apache default in Ubuntu is in fact weekly). Let’s look over Apache’s default in Ubuntu – /etc/logrotate.d/apache2
:
/var/log/apache2/*.log { weekly missingok rotate 52 compress delaycompress notifempty create 640 root adm sharedscripts postrotate /etc/init.d/apache2 reload > /dev/null endscript prerotate if [ -d /etc/logrotate.d/httpd-prerotate ]; then \ run-parts /etc/logrotate.d/httpd-prerotate; \ fi; \ endscript }
This will rotate any files in /var/log/apache2 that end in “.log”. This is why, when we create a new Apache virtual host, we typically put the logs in /var/log/apache2. Logrotate will automatically manage the log files! Let’s go through the options:
- weekly: Rotate logs once per week. Available options are daily, weekly, monthly, and yearly.
- missingok: If no *.log files are found, don’t freak out
- rotate 52: Keep 52 files before deleting old log files (That’s a default of 52 weeks, or one year worth of logs!)
- compress: Compress (gzip) log files
- delaycompress: Delays compression until 2nd time around rotating. As a result, you’ll have one current log, one older log which remains uncompressed, and then a series of compressed logs.
- compresscmd: Set which command to used to compress. Defaults to gzip.
- uncompresscmd: Set the command to use to uncompress. Defaults to gunzip.
- notifempty: Don’t rotate empty files
- create 640 root adm: Create new log files with set permissions/owner/group, This example creates a file with user
root
and groupadm
. In many systems, it will beroot
for owner and group. - sharedscripts: Run post-rotate script after all logs are rotated. If this is not set, it will run post-rotate after each matching file is rotated.
- postrotate: Scripts to run after rotating is done. In this case, Apache is reloaded so it writes to the newly created log files. Reloading Apache (gracefully) lets any current connection finish before reloading and setting the new log file to be written to.
- prerotate: Run before log rotating begins.
For more datails and configuration options you can check the logrotate man page:
man logrotate
Congratulations! You have successfully installed Logrotate. Thanks for using this tutorial for installing Logrotate to manage logs on Ubuntu 16.04 LTS (Xenial Xerus) system. For additional help or useful information, we recommend you to check the official Logrotate website.