Project

General

Profile


Restarting FCGI backend without downtime.

In case you have FCGI backend running on local sockets, it's possible to restart FCGI backend so that lighty never sees backend being down.
In short it's that we start backend on temporary socket and when started we rename the socket and then kill the old backend service.

As words don't explain it as good as code does, so here's the code snippet:
# config
PHP_FCGI_CHILDREN=5
SPAWN_SOCKET="/var/run/php/fcgi.sock" 
PHP_FCGI_BINARY=/usr/bin/php.fcgi

# start new instance
socket="$SPAWN_SOCKET" 
pid=$(cat /var/run/php-fcgi.pid)
SPAWN_SOCKET=$(mktemp -p ${SPAWN_SOCKET%/*} ${SPAWN_SOCKET##*/}.XXXXXX) || exit 1
spawn-fcgi -P /var/run/php-fcgi.pid -f $PHP_FCGI_BINARY -s $SPAWN_SOCKET -C $PHP_FCGI_CHILDREN || exit 1
# optional
chown lighttpd $SPAWN_SOCKET
chgrp lighttpd $SPAWN_SOCKET
# let lighty use new socket
mv -f "$SPAWN_SOCKET" "$socket" 

# sleep to let old requests to finish

sleep 10

# now can stop old instance
kill $pid