Gracefully Restart Lighttpd Web Server

by on May 10, 2009 · 4 comments· LAST UPDATED May 10, 2009

in , ,

How do I restart my lighttpd+php fastcgi web server gracefully under Linux / UNIX operating systems?

You need to send a SIGINT single to lighttpd process. It will only shutdown the server after the client connections are closed successfully without interrupting the connections. This is useful for reloading configuration options. If you are using sysv style script, make sure reload() looks like as follows:

#!/bin/bash
 
# only works under RHEL / Fedora / CentOS Linux
source /etc/init.d/functions
 
pidfile=/var/run/lighttpd.pid
prog=lighttpd
conf=/etc/lighttpd/lighttpd.conf
lighttpd=/usr/sbin/lighttpd
 
reload(){
  echo -n $"Reloading $prog  "
  killproc -p $pidfile $prog -INT
  start
  local RETVAL=$?
  echo
  return $RETVAL
}
 
start() {
        echo -n $"Starting $prog: "
        daemon $lighttpd -f $conf
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
        return $RETVAL
}
 
stop() {
        echo -n $"Stopping $prog: "
        killproc $lighttpd
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
        return $RETVAL
}
 
case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                start
                ;;
        condrestart)
                if [ -f /var/lock/subsys/$prog ]; then
                        stop
                        start
                fi
                ;;
        reload)
                reload
                ;;
        status)
                status $lighttpd
                RETVAL=$?
                ;;
        *)
                echo $"Usage: $0 {start|stop|restart|condrestart|reload|status}"
                RETVAL=1
esac
exit $RETVAL

Command line options for UNIX / Linux

If you do not have /init.d/ or /rc.d/ style script use the following procedure. You can send INT single from command line itself and start server again:
# kill -INT $(cat /var/run/lighttpd.pid)
# lighttpd -f /etc/lighttpd.conf

You should see something as follows in your lighttpd error log file:

2009-05-08 18:54:45: (server.c.1355) [note] graceful shutdown started
2009-05-08 18:54:45: (log.c.97) server started
2009-05-08 18:54:49: (server.c.1469) server stopped by UID = 0 PID = 957
2009-05-08 18:56:57: (log.c.97) server started 

A note about php fastcgi process

Make sure php socket configured as follows (without the following config, php will not get reloaded after graceful restart):

"socket" => "/tmp/php-cgi.socket" + var.PID

Here is a sample php5 fastcgi lighttpd.conf configuration:

fastcgi.server    = ( ".php" =>
        ((
                "bin-path" => "/usr/bin/php-cgi",
                "socket" => "/tmp/php-cgi.socket"+ var.PID,
                "max-procs" => 1,
                "idle-timeout" => 30,
                "bin-environment" => (
                        "PHP_FCGI_CHILDREN" => "2",
                        "PHP_FCGI_MAX_REQUESTS" => "2000"
                ),
                "bin-copy-environment" => (
                        "PATH", "SHELL", "USER"
                ),
                "broken-scriptfilename" => "enable"
        ))
)

A note about RHEL / CentOS Linux init.d script

Most modern Linux distributions comes with a script to start / stop / restart and reload lighttpd:
# /etc/init.d/lighttpd reload
However, you need to patch up /etc/init.d/lighttpd under RHEL / CentOS Linux. Find reload():

 
reload() {
        echo -n $"Reloading $prog: "
        killproc $lighttpd -HUP
        RETVAL=$?
        echo
        return $RETVAL
}

Replace with:

 
reload() {
        echo -n $"Reloading $prog: "
        killproc $lighttpd -INT
        RETVAL=$?
        echo
        return $RETVAL
}

Debian / Ubuntu Linux users

If you are using Debian / Ubuntu Linux, enter:
# /etc/init.d/lighttpd reload

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 4 comments… read them below or add one }

1 Charon May 11, 2009 at 8:32 am

I do not have killproc utility on my Fedora 10 box. Is it from lighthttpd distribution?

Reply

2 nixCraft May 11, 2009 at 11:52 am

killproc is a shell script function defined in /etc/init.d/functions file. This file is part of CentOS / RHEL and Fedora Linux. You need to add the following line before calling killproc

source /etc/init.d/functions
        killproc $lighttpd -INT

Reply

3 Stefan March 7, 2010 at 1:40 pm

Please don’t recommend such stupid things:
“socket” => “/tmp/php-cgi.socket” + var.PID

You will just get maaany php backends running…

PS: I’m missing a preview button here.

Reply

4 nixCraft March 7, 2010 at 3:30 pm

@ Stefan,

Ok, so how do you propose to solve this problem?

You will just get maaany php backends running…

Really? How? You can clean dead sockets using cron after 30 days or so..

Reply

Leave a Comment

Tagged as: , , , , , , ,

Previous Faq:

Next Faq: