Setting up logrotate for php error log

Hi, I'm using a LAMP stack with Ubuntu 16.04 and fastcgi/php7.0-fpm. I have logrotate set up across all my logs apart from one php error log (/var/log/php/error.log). My problem is that I'm not sure how to set up logrotate for this as I understand that I need to add a postrotate command to re-open the log after rotation or add copytruncate to the logrotate code.

Does php need to be reloaded by Apache? Should I just add the standard Apache pre- and postrotate commands?

if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi; \

and

if /etc/init.d/apache2 status > /dev/null ; then \
/etc/init.d/apache2 reload > /dev/null; \
fi;

or should I be running this:

/usr/lib/php/php7.0-fpm-reopenlogs

which contains this code:

#!/bin/sh
CONFFILE=/etc/php/7.0/fpm/php-fpm.conf
[ -r /etc/default/php7.0-fpm ] && . /etc/default/php7.0-fpm
CONF_PIDFILE=$(sed -n 's/^[[:space:]]*pid[[:space:]]*=[[:space:]]*//p' $CONFFILE)
PIDFILE=${CONF_PIDFILE:-/run/php/php7.0-fpm.pid}
[ -r "$PIDFILE" ] && kill -USR1 $(cat "$PIDFILE") > /dev/null
exit 0

as this is the default for another php log (/var/log/php7.0-fpm.log)?

Another possibility is to use rsyslog:

invoke-rc.d rsyslog rotate > /dev/null

At the moment, I think that this should work but I'm not certain:

/var/log/php/error.log {
weekly
copytruncate
missingok
notifempty
compress
delaycompress
postrotate
 invoke-rc.d rsyslog rotate > /dev/null
endscript
}

Any advice would be appreciated, many thanks.

EDIT: words…

2 Replies

Hey @DBR - It sounds like you're on the right track with the copytruncate and postrotate options. Based on a few forums I was looking through, copytruncate specifically was a common solution for a few other folks that were running into similar issues:

From Stack Overflow: How to configure logrotate with php logs

From LinuxQuestions.org: How does logrotate work?

I know you mentioned that you're using Ubuntu, but this CentOS post has some good examples of logrotate configs (they should act the same):

[CentOS] How to rotate PHP error log - since it belongs to apache

And that article linked to a stellar guide with more examples:

HowTo: The Ultimate Logrotate Command Tutorial with 10 Examples

Based on the examples in that article, your config looks like it should work pretty well. Hopefully you got things working!

*Just in case you hadn't seen it yet, we also have a guide on setting up logrotate:

How to Use logrotate to Manage Log Files

This is what I use (/etc/logrotate.d/php7.3-fpm):

/var/log/php/php7.3-fpm.log {
    rotate 4
    monthly
    missingok
    notifempty
    compress
    delaycompress
    postrotate
        /usr/lib/php/php7.3-fpm-reopenlogs
    endscript
    create 0644 root adm
}

I don't have any of the rest of that stuff you mentioned. I did move the php log from /var/log to /var/log/php.

-- sw

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct