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.

ADVERTISEMENTS

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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
15 comments… add one
  • Colin Jan 11, 2007 @ 0:28

    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.

  • Colin Jan 11, 2007 @ 1:29

    Oops, left out the owner:group parameter:

    /var/log/lighttpd.access.log www

  • Colin Jan 11, 2007 @ 1:31

    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.)

  • 🐧 nixCraft Jan 11, 2007 @ 12:13

    Colin,

    Thanks for sharing newsyslog configuration directive

    Appreciate your post.

  • aman Aug 4, 2007 @ 12:18

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

  • F4jr Sep 2, 2008 @ 9:48

    Is it possible to do something like that :

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

    thanks for your help !

  • Rob Oct 28, 2008 @ 1:30

    @ 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

  • F4jr china Nov 12, 2008 @ 10:59

    It works perfectly !

    Thanks !

  • Geoff Jan 5, 2009 @ 0:23

    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.

  • Rytis Sep 15, 2009 @ 2:32

    Good question Geoff.

    I also wonder why does lighttpd has to be restarted after logs are rotated?

    If the logs are rotated everyday, the server is restarted every day.

  • Rob Sep 15, 2009 @ 23:32

    @ Geoff and Rytis:

    I don’t think the HUP signal actually restarts lighttpd, from my understanding it tells the daemon to close any open file handles, open a new logfile and reload the config file

  • Rytis Sep 16, 2009 @ 0:49

    @ Rob

    mod_status reports lighttpd started up after logs were rotated, and all the previous stats are cleared. Since some of my logs are roated daily, my server uptime reported by mod_status is never more than a day.

    I was just wondering if it’s possible to rotate the logs and keep the server running (or is it good to restart it every day anyways?)

  • Rob Sep 16, 2009 @ 2:33

    @ Rytis

    mod_status shows no change in uptime between log rotations for me … lighttpd-1.4.23 on FreeBSD 7.2-STABLE

    /etc/newsyslog.conf

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

  • Ela Oct 1, 2009 @ 15:02

    Am getting this problem very biggly

    Please help me out

    vi /etc/logrotate.conf
    # see “man logrotate” for details
    # rotate log files weekly
    weekly

    # keep 4 weeks worth of backlogs
    rotate 4

    # create new (empty) log files after rotating old ones
    create

    # uncomment this if you want your log files compressed
    #compress

    # RPM packages drop log rotation information into this directory
    include /etc/logrotate.d

    # no packages own wtmp — we’ll rotate them here
    /var/log/wtmp {
    monthly
    minsize 1M
    create 0664 root utmp
    rotate 1
    }

    cat /etc/logrotate.d/httpd

    /var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    postrotate
    /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
    }

    My server is running nearly 10 sites and all those creating log files under statistics in vhosts.
    it wil become more than 30 gb with in week. how to control it. please reply me

  • Sagan Sep 21, 2012 @ 12:03

    What if your Debian doesn’t have a crond?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.