l2t

How to rotate nginx log files in ubuntu?

How to Install Logrotate in Linux or Ubuntu?

Why do we need to rotate logs?

Logs are very useful when debugging an application. You might store different types of logs on your server for example:

  • nginx logs
  • application logs

Keep in mind that if you do not manage your log files than it will grow in size and it will full your server disk in some days. If you are storing tons of logs everyday you need to store them on daily basis and later you want to access them.

Logrotate is a linux utility that helps you manage & rotate the logs based on their size, date etc..

How to install logrotate?

You can install logrotate on different operating systems like RHEL/CentOS, Dabian/Ubuntu, Fedora as show below:

# install on RHEL/CentOS
yum install logrotate

# install on Debian/Ubuntu/Linux
sudo apt-get install logrotate

# install on Fedora
dnf install logrotate

How to configure logrotate?

Once you have logrotate installed on your machine you can configure it based on your requirement.

Sample configuration file can be found in /etc/logrotate.conf:

# rotate log files weekly
weekly

# use the adm group by default, since this is the owning group
# of /var/log/syslog.
su root adm

# 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

# uncomment this if you want your log files compressed
#compress

# use files in below directory for specific log rotation
include /etc/logrotate.d

According to above configuration file it includes all configuration stored in /etc/logrotate.d directory. Let say that we want to rotate our nginx log files. We would create a file called /etc/logrotate.d./nginx with following configurations:

/var/log/nginx/*.log {
	daily
	missingok
	rotate 14
	compress
	delaycompress
	notifempty
	create 0640 www-data adm
	sharedscripts
	postrotate
		invoke-rc.d nginx rotate >/dev/null 2>&1
	endscript
}

Let's understand what above configuration means:

  • rotate all logs that found in /var/log/nginx/*.log
  • daily: rotate them on daily basis
  • missingok: errors are not to be written logs
  • notifempty: logs are not to be rotated if empty
  • rotate: we will keep last 14 log files
  • prerotate/postrotate: tells logrotate that the script to run, starts on the next line
  • endscript: command says that the script is done

Once the configuration file has been saved. By default, logrotate automatically configures a cron job scheduled to be run daily.

How to check if logrotate is configured correctly?

Once configuration file is saved you can run following command to make sure logrotate is configured correctly:

# run logrotate forcefully
logrotate -d /etc/logrotate.conf

# run logrotate forcefully with verbose mode
logrotate -vf /etc/logrotate.conf

# run logrotate in a debug mode
logrotate -d /etc/logrotate.conf

# check the status file to see which files rotated
cat /var/lib/logrotate.status

Hope this helps to all developer who are seeking to rotate application or server logs on daily basis. You can rotate as many logs files you want customize according to your need.