Running django with fastcgi is not a dif­fi­cult task, also because of the excel­lent doc­u­men­ta­tion pro­vided. Anyway the doc pro­vides a very basic script to autom­a­tize the start/stop fcgi process, so today I had to write my own so I don’t have to man­u­ally fix things if some­thing goes wrong since I let my script handle the var­i­ous situations.

It has a very basic start/stop/restart inter­face like normal startup scripts. Let see an exam­ple 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 down­load it, here it is.