habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Easy Setup Logrotate Nginx Ubuntu

1 min read

Log rotation is an important aspect of managing log files on a Linux system, including those generated by Nginx. Logrotate is a utility that helps automate the rotation, compression, removal, and mailing of log files. Here’s a basic guide on how to set up log rotation for Nginx on Ubuntu using logrotate.

Create a Logrotate Configuration File for Nginx:

Open your preferred text editor and create a new logrotate configuration file for Nginx. The convention is to use a separate file for each service. Let’s create a file for Nginx:

sudo nano /etc/logrotate.d/nginx

Inside the file, you can add the following configuration:

/var/log/nginx/*.log
/var/log/nginx/*.access
/var/log/nginx/*.error
{
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 0640 www-data adm
        sharedscripts
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi \
        endscript
        postrotate
                invoke-rc.d nginx rotate >/dev/null 2>&1
        endscript
}
  • daily: Rotates the logs on a weekly basis. You can change to weekly if want make dailiy rotation
  • missingok: Ignores errors if the log file is missing.
  • rotate 14: Keeps 14 rotated log files.
  • compress: Compresses the rotated log files using gzip.
  • delaycompress: Delays compression of the previous log file until the next rotation cycle.
  • notifempty: Does not rotate the log if it is empty.
  • create: Sets the permissions and ownership for the new log file.
  • sharedscripts: Runs the postrotate script only once for all matching files.
  • postrotate: The script to run after the log files are rotated. In this case, it sends a signal to Nginx to reopen its log files.
See also  Install Nginx PHP-FPM on Ubuntu 18.04 LTS

Test Log Rotation

You can test log rotation manually by running.

sudo logrotate -d /etc/logrotate.conf

The -d option is for debugging, and it will show you what logrotate would do without actually doing it. Once you are satisfied with the output, you can run logrotate without the -d option to apply the rotation.

sudo logrotate /etc/logrotate.conf

Additionally, you can force log rotation for a specific log file.

sudo logrotate -f /etc/logrotate.d/nginx

Verify Log Rotation

After log rotation, check that the log files have been rotated, compressed, and that Nginx is still able to write to the logs.

By setting up Logrotate Nginx Ubuntu, you ensure that log files are managed efficiently, preventing them from consuming too much disk space and making it easier to analyze historical log data.

habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Centos Failed Update Kernel

Today I did a kernel update on my server with Centos 7 OS. At the end of the update process, I found a kernel...
habibzain
1 min read

Easy Fix Missing mirrorlist http://mirrorlist.centos.org on CentOS 7

When running yum update or command that utilize the yum system, errors similar to the following are produced: If you’re encountering issues with the...
habibzain
1 min read

Easy Create Laravel Project with Composer

Requirement Laravel, a popular PHP framework, is renowned for its elegant syntax and robust features, making it a top choice for web developers. One...
habibzain
1 min read

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss good article from us, get weekly updates in your inbox