Rotate and compress log files with Logrotate (page 2)

botond published Jan. 2019, 04, 17:23 p.m. time

2. page content

 

Continuation

Logrotate allows you to automatically rotate and compress log files generated by our various services. The on the first page we got to know the basic settings of Logrotate, and on this page we continue to get acquainted with the program with our own examples.

 

 

use

In this chapter, we set up the optionally installed through two of our own lifelike examples PHP system PHP-FPM rotated log files.

Rotate PHP 7 log file

In the first example, an earlier one Debian 8 (Jessie) for the perfect server cost optionally installed with 7.1 PHP system for which the PHP-FPM log file will be rotated. On this server, the default PHP is 5.6, which was properly installed from a package, so its log file rotation is already resolved. However, for PHP 7.1, which is then compiled and installed from source, we have to solve this ourselves. This may also apply to other versions of PHP that have been compiled from source.

First, log into the logrotate.d directory:

cd /etc/logrotate.d/

Then create a configuration file to rotate:

nano php-7.1-fpm
It does not matter what filename we use, but try not to confuse it with others later, so it is advisable to choose a name that indicates what it contains settings.

Then, insert the following content:

/opt/php-7.1/var/log/php-fpm.log {
        rotate 14
        daily
        compress
        delaycompress
        missingok
        notifempty
		postrotate
			/bin/kill -SIGUSR1 `cat /opt/php-7.1/var/run/php-fpm.pid 2>/dev/null` 2>/dev/null || true
		endscript
}

Here, the above parameters are already familiar, as well as a postrotate section that asks for a log file to reopen from the process with the process ID read from the PHP-FPM pid file (which is the php-7.1-fpm master process).

Interestingly, we can find it here for more details on how the PHP-FPM event handler works when one does SIGUSR1 signal-t cap.

If you have a different pid file path in php-fpm.conf when compiling PHP, then of course you will use it.

No need to restart, once you save the configuration file, then Logrotate does the job.

If we did everything right, then a / Opt / php-7.1 / var / log our directory (or where we installed PHP) will look something like this in a few days:

-rw------- 1 root root    0 ápr    2 06:25 php-fpm.log
-rw------- 1 root root  212 ápr    2 06:25 php-fpm.log.1
-rw------- 1 root root  238 márc  28 06:25 php-fpm.log.2.gz
-rw------- 1 root root 8,1K márc  27 06:25 php-fpm.log.3.gz

Rotate PHP 5 log file

In this example, in reverse, one Debian 9 (Stretch) for the perfect server translated from source and optionally PHP 5.6 installed system PHP-FPM log file is rotated using Logrotate.

The default PHP version on this server is 7.0, so the rotation of its log files has already been set during installation from the package.

Here too, log into your logrotate settings directory:

cd /etc/logrotate.d/

And let's create a file:

nano php-5.6.40-fpm

Then add the following content:

/opt/php-5.6.40/var/log/php-fpm.log {
    rotate 14
    daily
    compress
    delaycompress
    missingok
    notifempty
        postrotate
            /bin/kill -SIGUSR1 `cat /opt/php-5.6.40/var/run/php-fpm.pid 2>/dev/null` 2>/dev/null || true
        endscript
}

Here, the contents of the file are the same as in the previous example, except that the path to the log file and the path to the pid file are changed. After saving the file, the php-fpm log file will start rotating similarly, resulting in a log file structure similar to the example above.

 

 

Testing

Testing is also possible to make sure that our current setup is working properly. To do this, run the following command after saving your new setting:

/usr/sbin/logrotate -vdf /etc/logrotate.d/<beállítófájl>

Where the switches:

  • v: Verbose mode, prints everything
  • d: Debug mode. Then you just simulate what you would do in the next rotation cycle
  • f: Force mode, forcing rotation even when not required by settings.

And if you really want to run the rotation, skip the -d switch:

/usr/sbin/logrotate -vf /etc/logrotate.d/<beállítófájl>

A new log file is created.

 

Conclusion

With this little simple but useful program you can keep your logs organized so you don't miss any events. If you are installing another service on the server, it is advisable to check immediately after the installation whether Logrotate is already configured. If not, we can set it up easily.

 

 

Navigation

This description consists of several pages: