mugshot, larkspur


Gopal Venkatesan's Former Journal

My writings on free/open source software and other technologies

Previous Entry Share Next Entry
Configuring nginx with daemontools
mugshot, larkspur

This content has been updated and moved to a new place.

In this blog post I will how to set up nginx with daemontools.
Installation of nginx is covered in my previous blog post. The reason I chose daemontools over recent monitoring tools like monit is it's simplicity in configuring a service. While a lot of posts out there criticising D.J.Bernstein's philosophies, it should be admitted that his works focus a lot on simplicity, portability and performance. Here is a good post from Aaron Swartz on DJB, it is worth reading.

Installing daemontools

Installing daemontools is extremely easy.

sudo mkdir /package && sudo chmod 1755 /package && cd /package
sudo wget && sudo tar -xzpf daemontools-0.76.tar.gz
cd admin/daemontools-0.76 && sudo package/install

Just in case you are getting an error about "errno: TLS definition in /lib/", then follow the steps below. I got the error on my CentOS 5.5 VPS and the following steps worked for me.

shell$ sudo package/install
Linking ./src/* into ./compile...
Compiling everything in ./compile...
./load envdir unix.a byte.a 
/usr/bin/ld: errno: TLS definition in /lib/ section .tbss mismatches non-TLS reference in envdir.o
/lib/ could not read symbols: Bad value
collect2: ld returned 1 exit status
make: *** [envdir] Error 1
Copying commands into ./command...
cp: cannot stat `compile/svscan': No such file or directory

Edit /package/admin/daemontools/src/conf-cc and add -include /usr/include/errno.h to the gcc line. Rerun package/install and it should work.
The recent versions of daemontools set the appropriate steps to start svscan, for example, adding the svscan in /etc/inittab on SysV systems. In case your system is a BSD, a reboot is necessary for running svscan.

Configuring nginx

daemontools cannot supervise daemons that put itself into the background. According to D.J.Bernstein, author of daemontools, it is a bad software design. That makes most daemons bad, including nginx! Fortunately nginx allows itself to be run in foreground by just adding daemon off in its configuration.
So, after adding daemon off in /opt/nginx/conf/nginx.conf, it is fairly simple to put nginx supervised by daemontools.
Create a directory nginx under /service and create a shell script named run under it with the following contents.

echo starting nginx
exec /opt/nginx/sbin/nginx

Set the following permissions to the files.

shell$ sudo chmod 1755 /service/nginx/run

That's it, supervise will automatically start and monitor nginx now.
To see the status of nginx, just run svstat /service/nginx.
After altering nginx configuration, just kill nginx using pkill nginx, supervise will take care of automatically restarting the service. Alternately the following is a better option for nginx to re-read its configuration.

shell$ svc -h /service/nginx


Log in