Thursday, May 3, 2012

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

No comments:

Post a Comment