Running Django with fastcgi
October 8th, 2008
Running django with fastcgi is not a difficult task, also because of the excellent documentation provided. Anyway the doc provides a very basic script to automatize the start/stop fcgi process, so today I had to write my own so I don’t have to manually fix things if something goes wrong since I let my script handle the various situations.
It has a very basic start/stop/restart interface like normal startup scripts. Let see an example of basic usage:
kratorius@becks:~/prj$ sudo sh startserver.sh start
Starting fcgi process... done!
kratorius@becks:~/prj$ sudo sh startserver.sh stop
kratorius@becks:~/prj$ sudo sh startserver.sh restart
fcgi process is not running
kratorius@becks:~/prj$ sudo sh startserver.sh start
Starting fcgi process... done!
kratorius@becks:~/prj$ sudo sh startserver.sh restart
Starting fcgi process... done!
And here it is:
#!/bin/bash
# Start and stop a django fcgi process
# Giuliani Vito Ivan <giuliani.v@gmail.com>
# project directory
PROJDIR=`pwd`
# user owner (usually www-data)
USER_OWNER="www-data"
# group owner (usually www-data)
GROUP_OWNER="www-data"
# extra python path (leave empty if unneeded)
PYTHONPATH="../python:.."
# do not edit anything below
PIDFILE="$PROJDIR/technosec.pid"
SOCKET="$PROJDIR/technosec.sock"
start_fcgi()
{
if [ -f $PIDFILE ] || [ -f $SOCKET ]; then
echo "The fcgi process is already running, please stop"
echo "that before running another process"
else
echo -n "Starting fcgi process... "
nohup /usr/bin/env - \
PYTHONPATH=$PYTHONPATH \
python manage.py runfcgi socket=$SOCKET pidfile=$PIDFILE > /dev/null 2>&1 &
if [ $? -eq 0 ]; then
sleep 1
chown $USER_OWNER:$GROUP_OWNER $SOCKET
chown $USER_OWNER:$GROUP_OWNER $PIDFILE
echo "done!"
else
echo "failed!"
fi
return $?
fi
return 1
}
stop_fcgi()
{
cd $PROJDIR
if [ -f $PIDFILE ]; then
kill `cat -- $PIDFILE`
rm -f -- $PIDFILE
rm -f -- $SOCKET
return 0
else
echo "fcgi process is not running"
return 1
fi
}
restart_fcgi()
{
stop_fcgi
if [ $? -eq 0 ]; then
start_fcgi
fi
}
case "$1" in
start)
start_fcgi
;;
stop)
stop_fcgi
;;
restart)
restart_fcgi
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
exit 0
Just in case you may want to download it, here it is.