Skip to content

Instantly share code, notes, and snippets.

@fwenzel
Created January 25, 2010 09:56
Show Gist options
  • Save fwenzel/285758 to your computer and use it in GitHub Desktop.
Save fwenzel/285758 to your computer and use it in GitHub Desktop.
A Debian init script to run one or more FCGI-based Django applications.
#! /bin/sh
### BEGIN INIT INFO
# Provides: FastCGI servers for Django
# Required-Start: networking
# Required-Stop: networking
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: Start FastCGI servers with Django.
# Description: Django, in order to operate with FastCGI, must be started
# in a very specific way with manage.py. This must be done
# for each DJango web server that has to run.
### END INIT INFO
#
# Author: Guillermo Fernandez Castellanos
# Version: @(#)fastcgi 0.1 11-Jan-2007 guillermo.fernandez.castellanos AT gmail.com
# Altered by David Reynolds for Debian Stable (http://davidreynolds.me.uk/blog/2007/mar/16/django-fcgi-init-script/)
#### SERVER SPECIFIC CONFIGURATION
export PYTHONPATH=/var/www/django/trunk/:/var/www/django/
DJANGO_SITES="mysite stmarys"
SITES_PATH=/var/www/django
RUNFILES_PATH=$SITES_PATH/run
HOST=127.0.0.1
PORT_START=3033
RUN_AS=www-data
#### DO NOT CHANGE ANYTHING AFTER THIS LINE!
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="FastCGI servers"
NAME=$0
SCRIPTNAME=/etc/init.d/$NAME
#
# Function that starts the daemon/service.
#
d_start()
{
# Starting all Django FastCGI processes
PORT=$PORT_START
for SITE in $DJANGO_SITES
do
echo -n ", $SITE"
if [ -f $RUNFILES_PATH/$SITE.pid ]; then
echo -n " already running"
else
start-stop-daemon --start \
--exec /usr/bin/python $SITES_PATH/$SITE/manage.py runfcgi method=threaded \
host=$HOST port=$PORT pidfile=$RUNFILES_PATH/$SITE.pid --pidfile $RUNFILES_PATH/$SITE.pid
chmod 400 $RUNFILES_PATH/$SITE.pid
fi
let "PORT = $PORT + 1"
done
}
#
# Function that stops the daemon/service.
#
d_stop() {
# Killing all Django FastCGI processes running
for SITE in $DJANGO_SITES
do
echo -n ", $SITE"
start-stop-daemon --stop --quiet --pidfile $RUNFILES_PATH/$SITE.pid \
|| echo -n " not running"
if [ -f $RUNFILES_PATH/$SITE.pid ]; then
rm $RUNFILES_PATH/$SITE.pid
fi
done
}
ACTION="$1"
case "$ACTION" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
restart|force-reload)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "Usage: $NAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
exit 0
$HTTP["host"] =~ "^(www\.)?example\.com" {
fastcgi.server = (
"/mysite.fcgi" => (
"main" => (
# Use host / port instead of socket for TCP fastcgi
"host" => "127.0.0.1",
"port" => 3033,
#"socket" => "/home/user/mysite.sock",
"check-local" => "disable",
)
),
)
alias.url = (
"/media" => "/srv/www/django/mysite/media/",
"/admin-media" => "/srv/www/django/virtualenvs/mysite/lib/python2.6/site-packages/django/contrib/admin/media/",
)
url.rewrite-once = (
"^(/media.*)$" => "$1",
"^(/admin-media.*)$" => "$1",
#"^/favicon\.ico$" => "/media/favicon.ico",
"^(/.*)$" => "/mysite.fcgi$1",
)
}
#!/usr/bin/env python
import site
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)
# load virtualenv if set
if settings.VIRTUALENV:
site.addsitedir(settings.VIRTUALENV)
site.addsitedir(settings.path('apps'))
if __name__ == "__main__":
from django.core.management import execute_manager
execute_manager(settings)
# Django settings.py additions
DEBUG = False # debug in production is evil
VIRTUALENV = "/srv/www/django/virtualenvs/mysite/lib/python2.6/site-packages"
# force removal of mysite.fcgi from URL:
# http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/#forcing-the-url-prefix-to-a-particular-value
FORCE_SCRIPT_NAME = ''
@diegows
Copy link

diegows commented Nov 8, 2011

In Django 1.3.1 I have to use FORCE_SCRIPT_NAME="" in setting.py to get it working. I don't know why but without that Django sends a weird redirect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment