Adding a logrotation for your custom vhost apache logs

On the Virtual Hosts page there is line about having custom logs for your host but they will not be in logrotation by default. The logroatate guide is also helpful, but unconnected to the vhost setup. If anyone see's any errors, please comment.

~~[https://library.linode.com/hosting-website#sphconfiguring-name-based-virtual-hosts" target="blank">](https://library.linode.com/hosting-webs … tual-hosts">https://library.linode.com/hosting-website#sph_configuring-name-based-virtual-hosts](

~~[https://library.linode.com/linux-tools/utilities/logrotate" target="_blank">](https://library.linode.com/linux-tools/ … /logrotate">https://library.linode.com/linux-tools/utilities/logrotate](

Maybe it will help someone.

–--------------------

The service logrotate is pretty straight forward. My logs are made by apache so there are some extra stuff that has to be included. This is not realy a script as much as a small directive file. This file is named mycustomfile and it is placed in the /etc/logrotate.d/ folder. I don't think it matters what the name of the file is. It will be added to what logrotate daemon does. The following example assumes you followed the directions on setting up your vhosts in the linode library.

/home/example_user/public/example.com/log/*.log {
        weekly
        missingok
        rotate 52
        compress
        delaycompress
        notifempty
        create 640 root root
        sharedscripts
        postrotate
                if [ -f "`. /etc/apache2/envvars ; echo ${APACHE_PID_FILE:-/var/run/apache2.pid}`" ]; then
                        /etc/init.d/apache2 reload > /dev/null
                fi
        endscript
}

Meaning

The opening line shows the location of the logs with a wildcard statement.

weekly - Run Weekly

missingok - If the file isn't there for a sec, don't sweat it.

compress - compress the old files delaycompress - only compress the file after enough time elapses for apache to restart and start seeing the new file.

notifyempty - don't rotate it if it is empty create 640 root root - create the new file with 640 privs and root user and group

sharedscripts - run once for each

the last bit tells the apache service to restart so that it will start using the new log file.

(Everything about the naming of the files and backups should be automatic.)

Testing

You may want to run tests in verbose mode that actually don't run the log rotation. Remove the "d" which is test mode and replace that with a "f." F will FORCE running even if the scrip calls for running on certain days.

Test

logrotate -vd /etc/logrotate.d/yourcustomdirective

For Real

logrotate -vf /etc/logrotate.conf/yourcustomdirective

0 Replies

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