Mar 18,
2017

Lighttpd and Ephemeral Logs

Recently I've found out that lighttpd doesn't start and bails out with an error if it can't find its log file.

The backstory is that my home NAS uses an Intel Z-U130 USB SSD as the system disk1 and unfortunately it's tragically slow, especially writing data. According to Intel documentation the write speed is rated at 24 GiB/s, I've never bothered to measure it but it feels way slower.

Anyway, to offset the subjective slow write speed and somewhat extend the lifespan of the device, I decided to mount /tmp and /var/log to the RAM file-system.

First of all, I'm not suggesting to indiscriminately mount log files on tmpfs, the reader should evaluate the pros and the cons of such arrangement starting from considering RAM usage and clearly the ephemeral nature of log files stored on tmpfs.

Another thing that I'm not suggesting is avoiding to save the contents of /var/log during shutdown.

In my case I don't bother, so it's not a surprise that at boot time the log directory is empty and lighttpd fails to start. The fix is trivial tho, before launching the daemon the required path needs to be created.

Enough Ramblings

A simple way to do it is using systemd and a sensible way to do it is appending the required actions in a local configuration file instead of modifying the service file directly. In this way changes are preserved when installing new versions of the package.

This is accomplished by creating a directory with the same name of the service, in this specific case /etc/systemd/system/lighttpd.service.d and storing inside a file ending with the .conf suffix with the required instructions.

For example local.conf will do:

[Service]
# When mounting log on tmpfs, creating the lighttpd log path 
ExecStartPre=/bin/sh -c 'mkdir -p /var/log/lighttpd && chown www-data:adm /var/log/lighttpd'

As a side note, on Debian the canonical way to configure the use of tmpfs is using the /etc/defaults/tmpfs file but it looks like it's missing an option for logs. I reverted to use the mount options in the /etc/fstab file.

# log on tmpfs
tmpfs    /var/log        tmpfs defaults,mode=0755 0 0
tmpfs    /var/log/apt    tmpfs defaults,mode=0755 0 0

To the astute reader it would be clear that this post is not about how to use lighttpd with log mounted as tmpfs but more as a example to a proper way to do it with systemd, as I can probably find half a dozen different ways to do it.

Challenge Accepted

A friend, after reading a draft of this post, invited me to enumerate at least 6 different ways to do it.

OK, let's see:

  • editing rc.local

  • putting a script in /etc/init.d

  • using a shell helper/trampoline script

  • exploiting cron (gnu cron has a @reboot directive)

  • using overlayfs 2

  • modifying the source

Mic drop.

Picking up the mic from the floor: apparently, the proper way to fix it with systemd3 is to create a file in /etc/tmpfiles.d directory with the necessary parameters. In this case this is the content of my lighttpd-log.conf file:

D /var/log/lighttpd/ 0755 www-data adm

The details are explained on the freedesktop.org site.

Mic drop again.


  1. should we stop calling them disks? ↩︎

  2. Debian Jessie's default kernel is without overlayfs support ↩︎

  3. something similar happened with ConsoleKit when mounting /var/log on tmpfs ↩︎