Lighttpd rotating log files with logrotate tool

Lighttpd logo

Last time I wrote about setting up virtual hosting for Lighttpd web server. Naturally next step is to setup log rotating with logrotate which rotates, compresses log files.

Our setup

Our sample setup has total 6 log files:
Default domain/IP log files:
/var/log/lighttpd/access.log
/var/log/lighttpd/error.log

nixcraft.com virtual domain log files:
/var/log/lighttpd/nixcraft.com/access.log
/var/log/lighttpd/error.log

theos.in virtual domain log files:
/var/log/lighttpd/theos.in/access.log
/var/log/lighttpd/theos.in/error.log

logrotate Configuration

All you need to do is open/create logrotate configuration file for lighttpd. Open file /etc/logrotate.d/lighttpd:
# vi /etc/logrotate.d/lighttpd

Append following text:
"/var/log/lighttpd/*.log" "/var/log/lighttpd/nixcraft.com/*.log " "/var/log/lighttpd/theos.in/*.log " {
missingok
copytruncate
rotate 7
compress
notifempty
sharedscripts
postrotate
/etc/init.d/lighttpd reload
endscript
}

Where,

  • "/var/log/lighttpd/*.log" "/var/log/lighttpd/nixcraft.com/*.log " "/var/log/lighttpd/theos.in/*.log ": Log files with wild card specification as per our setup.
  • missingok: If the log file is missing, go on to the next log file without issuing an error message.
  • copytruncate: Truncate the original log file to zero size in place after creating a copy, instead of moving the old log file and optionally creating a new one
  • rotate 7: Log files are rotated 7 times before being removed or mailed to the address specified in a mail directive. If count is 0, old versions are removed rather then rotated.
  • compress: Old versions of log files are compressed with gzip to save disk space.
  • notifempty: Do not rotate the log if it is empty
  • sharedscripts
    postrotate
    /etc/init.d/lighttpd reload
    endscript:
    The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. These directives may only appear inside a log file definition. In our case we are reloading lighttpd. Other opting could be send –HUP single using kill command.

Make sure crond runs automatically after system reboot

Now your logs will rotate with logrotate command which is called from cronjob (/etc/cron.daily/logrotate) everyday. So make sure crond is running all the time:
# /etc/init.d/crond start
# chkconfig --list crond
# chkconfig crond on

Alternatively, run text based GUI tool for same purpose (Redhat/CentOS/Fedora and friends):
# ntsysv

If you are using Debian Linux, type the following command to configure crond using text based GUI tools:
# rcconf

Alternatively you can use update-rc.d command (Debian / Ubuntu Linux) to start crond automatically after system reboot:
# update-rc.d crond defaults

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 9 comments… read them below or add one }

1 Colin 01.11.07 at 12:28 am

If you are running BSD, you can use BSD’s newsyslog to do the same. Just add this line to /etc/newsyslog.conf:

/var/log/lighttpd.access.log \
644 7 * $D0 \
B /var/run/lighttpd.pid

It rotates the lighttpd access log everyday at midnight, saves seven previous logs, and sends -HUP to lighttpd. See “man newsyslog.conf” for more information.

2 Colin 01.11.07 at 1:29 am

Oops, left out the owner:group parameter:

/var/log/lighttpd.access.log www

3 Colin 01.11.07 at 1:31 am

Sigh… it got truncated:

/var/log/lighttpd.access.log www:www 644 7 * $D0 B /var/run/lighttpd.pid

(If you have lighttpd running as a different user/group than www, modify accordingly.)

4 nixcraft 01.11.07 at 12:13 pm

Colin,

Thanks for sharing newsyslog configuration directive

Appreciate your post.

5 aman 08.04.07 at 12:18 pm

What is this crond above refers to if u please tell me then its really helpful to me

6 F4jr 09.02.08 at 9:48 am

Is it possible to do something like that :

“/var/log/lighttpd/*/*.log”

thanks for your help !

7 Rob 10.28.08 at 1:30 am

@ F4jr
It sure is! You do need to tell newsyslog that the logfile is a shell pattern with the G flag:


/var/log/lighttpd.*.log www:www 644 7 * $D0 GJ /var/run/lighttpd.pid

8 F4jr china 11.12.08 at 10:59 am

It works perfectly !

Thanks !

9 Geoff 01.05.09 at 12:23 am

Why does lighttpd need to be restarted after rotating the log files? Isn’t the log simply copied, then truncated? I don’t see why a restart is needed.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tagged as: , , , , , , , ,

Previous post: Howto: Linux write (burn) data to DVD or DVD/RW

Next post: Book review: DNS in Action – A Detailed And Practical Guide to Dns Implementation, Configuration, And Administration