Gracefully Restart Lighttpd Web Server

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:

ADVERTISEMENTS

#!/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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
4 comments… add one
  • Charon May 11, 2009 @ 8:32

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

    • 🐧 nixCraft May 11, 2009 @ 11:52

      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
  • Stefan Mar 7, 2010 @ 13:40

    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.

  • 🐧 nixCraft Mar 7, 2010 @ 15:30

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

Leave a Reply

Your email address will not be published.

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