Learn to modify the log retention period (in days) for your website in CloudPanel.

How to Increase Nginx Log Retention Time for a Website in CloudPanel

A proper log management is essential for monitoring the performance or security of your web application. Nginx logs can accumulate quickly, especially on high-traffic websites, consuming valuable disk space if they're not properly managed. CloudPanel, the modern server control panel for PHP-based applications, solves all that by using logrotate to handle logs, and in the case of access and error logs, they're just preserved for 7 days. In my case, 7 days isn't enough to identify problems in my applications since the usage cycle of the application cannot be observed in that short time frame, so I require at least the logs of the last 30 days. After some research, I found how to change the default retention period of the Nginx logs in CloudPanel, so you don't have to take that much time accomplishing this.

In this article, I'll explain how to easily increase the retention time of the Nginx log of a website hosted in CloudPanel.

1. List all logrotate files of your websites

CloudPanel creates a separate logrotate config for each vhost (website) hosted in CloudPanel, which are typically stored in /etc/logrotate.d, where each website will have its configuration file. You can list all of them with:

ls -la /etc/logrotate.d

A logrotate file is a configuration file used by the logrotate utility to manage the automatic rotation, compression, and removal of log files.

2. Customize a website logrotate file

You need to identify the logrotate file of the website hosted in CloudPanel that you want to modify. Once you have identified the site, change the file with nano:

nano /etc/logrotate.d/website-username

Where website-username will be the username of the website hosted in your CloudPanel instance. The content of the file will look something similar to the following:

  • On your case, the route of the username and the website name will change:
/home/ourcodeworld/logs/*/*.log {
    su root root
    daily
    missingok
    rotate 7
    dateext
    dateformat -%Y-%m-%d
    create 0640 ourcodeworld ourcodeworld
    postrotate
      /etc/init.d/nginx reload &> /dev/null || true
    endscript
}

In this case, I want to extend the retention time from only 7 days to 30, so I can analyze them very carefully at the end of the month. So you only need to modify the rotate directive, providing the new value (change the rotate directive value to the number of days that you want to keep your logs):

rotate 30

Save the changes to the logrotate file that you just modified, and apply the changes to logrotate, providing as an argument the path to the file:

sudo logrotate -f /etc/logrotate.d/cloudpanel-yourdomain.com

Alternatively, update all the logrotate files of your system:

sudo logrotate -f /etc/logrotate.conf

This will generate an output similar to the following (one line per logrotate file):

* Reloading nginx configuration nginx [ OK ]

And that's it! You will now have a single log file for each day of the last 30 days.


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors