SIGN IN / UP
    opened image

    In Linux OS, almost all programs and services that run in the background, Apache, Nginx, MySQL, etc., write their status, results and errors to log files. The location of the logs is located in the standard /var/log folder. If there are a lot of sites on the server, or the services are not configured correctly, the sites have a lot of errors, this folder will quickly fill up and may "clog" all the free space on the server.

    Logrotate is a utility that manages the rotation and compression of log files. That will help protect server owners from stopping services and making sites unavailable.

    In this article, we will look at how to set up log rotation so that disk space does not clog. Let's set up a standard Logrotate configuration, and we'll also see how to customize it to fit your needs.

    Most distributions have this utility installed by default. The version can be checked with the command:
     

    logrotate --version



    If you do not have logrotate installed, then you can install it with the following commands:

    On Ubuntu:

     

     

     

    apt install logrotate

     


    On Centos:

     

     

     

     

    yum install logrotate

     



    Logrotate configuration files are located:

     

     

     

     

    /etc/logrotate.conf and /etc/logrotate.d directory

     


    The /etc/logrotate.conf file contains general settings, by default.


    The /etc/logrotate.d/ folder contains configuration files, programs, services, whose logs we will rotate.


    Important!!!
    All configuration files located in the /etc/logrotate.d folder will override the default values set in the /etc/logrotate.conf configuration file



    And so, let's say we need to rotate logs for vestacp. As you can see, a configuration file with standard settings has already been automatically created for it.


    Let's take a look at the directives:

    /usr/local/vesta/log/*.log is the path to the files to be rotated. * - indicates that all files in the given folder /usr/local/vesta/log with .log extension will be cleaned up.

    missingok - do not write an error message if the log file is missing.
    notifempty - do not change the log file if it is empty.
    size 30k - say that until the log size exceeds 30k it will not be rotated. (You can set your own value)
    yearly - rotation once a year. This overrides weekly by default. (Let's look again)


    create 0600 root root - creates a file after rotation with permissions 0600 owner and group root.

    Below are the rest of the directives that can be used to set up rotation. Many other directives can be viewed with the command:

     

     

     

     

    man logrotate

     


    Let's add to the standard file:

    rotate 2 - this way 2 old logs will be kept. Which overrides the default rotate 4
    daily - rotation once a day. This overrides the default, once a week weekly.
    Add compress - to compress rotated files. (In this case gzip will be used, this will append to .gz files)

    Now the config we need looks like this:


    You can add these and other values that are described in man logrotate to another file you need in the /etc/logrotate.d/ folder, or you can tweak the ones that already exist.

    To configure logrotate for another service you need, or one that does not have a configuration file in the /etc/logrotate.d/ folder, you can simply create one.

    After creating/modifying, we can run it with the command:

     

     

     

     

    logrotate -f [config file path]

     


    Now you can add/change the necessary configs so that the free space on the server is not clogged.

    Uninterrupted work for you.