In this tutorial, we will show you how to install and configuration Logrotate on Ubuntu 16.04 LTS server. 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 through the step by step installation Logrotate on a Ubuntu 16.04 (Xenial Xerus) server.
Install Logrotate on Ubuntu 16.04 LTS
Step 1. First, make sure that all your system packages are up-to-date by running these following apt-get commands in the terminal.
Step 2. Installing Logrotate on Ubuntu.
To install logrotate, just use your package manager:
Verify that the installation was successful:
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:
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
adm. In many systems, it will be
rootfor 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:
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.