Changes between Initial Version and Version 1 of InitdScriptForLinux


Ignore:
Timestamp:
Jan 14, 2007, 4:18:36 PM (17 years ago)
Author:
guillermo.fernandez.castellanos@…
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • InitdScriptForLinux

    v1 v1  
     1= Django FastCGI init.d script for Linux =
     2
     3One of the common deployments are FastCGI servers with a reverse proxy redirecting the requests to the appropriate server. This proxy can be [http://www.djangoproject.com/documentation/fastcgi/#apache-setup Apache], [http://www.djangoproject.com/documentation/fastcgi/#lighttpd-setup Lighttpd], [http://nginx.net/ Nginx],...
     4
     5The standard Django documentation give a simple way to [http://www.djangoproject.com/documentation/fastcgi/#starting-your-fastcgi-server start and stop the FastCGI servers], but sometimes more flexibility and integration with the system is needed. Here, an alternative script is presented.
     6
     7== The initialisation script ==
     8
     9Then init.d script will be called '''fastcgi''', an admittedly unimaginative name. The main requirement is that it should integrate without problems with the init.d scripts, and that it should allow to transparently and uniformly start, stop and restart FastCGI processes for several Django sites with minimum configuration.
     10
     11{{{
     12#! /bin/sh
     13### BEGIN INIT INFO
     14# Provides:          FastCGI servers for Django
     15# Required-Start:    networking
     16# Required-Stop:     networking
     17# Default-Start:     2 3 4 5
     18# Default-Stop:      S 0 1 6
     19# Short-Description: Start FastCGI servers with Django.
     20# Description:       Django, in order to operate with FastCGI, must be started
     21#                    in a very specific way with manage.py. This must be done
     22#                    for each DJango web server that has to run.
     23### END INIT INFO
     24#
     25# Author:  Guillermo Fernandez Castellanos
     26#          <guillermo.fernandez.castellanos AT gmail.com>.
     27#
     28# Version: @(#)fastcgi 0.1 11-Jan-2007 guillermo.fernandez.castellanos AT gmail.com
     29#
     30
     31#### SERVER SPECIFIC CONFIGURATION
     32DJANGO_SITES="site1 site2 site3"
     33SITES_PATH=/path/to/sites
     34RUNFILES_PATH=$SITES_PATH/run
     35HOST=127.0.0.1
     36PORT_START=3000
     37RUN_AS=www-data
     38#### DO NOT CHANGE ANYTHING AFTER THIS LINE!
     39
     40set -e
     41
     42PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
     43DESC="FastCGI servers"
     44NAME=$0
     45SCRIPTNAME=/etc/init.d/$NAME
     46
     47#
     48#       Function that starts the daemon/service.
     49#
     50d_start()
     51{
     52    # Starting all Django FastCGI processes
     53    PORT=$PORT_START
     54    for SITE in $DJANGO_SITES
     55    do
     56        echo -n ", $SITE"
     57        if [ -f $RUNFILES_PATH/$SITE.pid ]; then
     58            echo -n " already running"
     59        else
     60            start-stop-daemon --start --quiet \
     61                       --pidfile $RUNFILES_PATH/$SITE.pid \
     62                       --chuid $RUN_AS --exec /usr/bin/env -- python \
     63                       $SITES_PATH/$SITE/manage.py runfcgi \
     64                       host=$HOST port=$PORT pidfile=$RUNFILES_PATH/$SITE.pid
     65            chmod 400 $RUNFILES_PATH/$SITE.pid
     66        fi
     67        let "PORT = $PORT + 1"
     68    done
     69}
     70
     71#
     72#       Function that stops the daemon/service.
     73#
     74d_stop() {
     75    # Killing all Django FastCGI processes running
     76    for SITE in $DJANGO_SITES
     77    do
     78        echo -n ", $SITE"
     79        start-stop-daemon --stop --quiet --pidfile $RUNFILES_PATH/$SITE.pid \
     80                          || echo -n " not running"
     81        if [ -f $RUNFILES_PATH/$SITE.pid ]; then
     82           rm $RUNFILES_PATH/$SITE.pid
     83        fi
     84    done
     85}
     86
     87ACTION="$1"
     88case "$ACTION" in
     89    start)
     90        echo -n "Starting $DESC: $NAME"
     91        d_start
     92        echo "."
     93        ;;
     94
     95    stop)
     96        echo -n "Stopping $DESC: $NAME"
     97        d_stop
     98        echo "."
     99        ;;
     100
     101    restart|force-reload)
     102        echo -n "Restarting $DESC: $NAME"
     103        d_stop
     104        sleep 1
     105        d_start
     106        echo "."
     107        ;;
     108
     109    *)
     110        echo "Usage: $NAME {start|stop|restart|force-reload}" >&2
     111        exit 3
     112        ;;
     113esac
     114
     115exit 0
     116}}}
     117
     118The variables of the script are:
     119
     120    * '''SITES_PATH:''' The folder where all the sites are stored. The folder structure i supposed to be a common folder ($SITES_PATH, in our case) where each site has a known folder ($SITES_PATH/site1/, $SITES_PATH/site2/, $SITES_PATH/site3/) where all configuration files are stored (manage.py, settings.py,...).
     121    * '''DJANGO_SITES:''' The names of the folders where the different sites are.
     122    * '''RUNFILES_PATH:''' The path to the folder where .pid and .socket files (if using Unix sockets) will be stored.
     123    * '''HOST:''' The IP address where we want the server to run.
     124    * '''PORT_START:''' Each server will be running in a different port. server1 will run on PORT_START, server2 on PORT_START+1, server3 on PORT_START+2,etc.
     125    * '''RUN_AS:''' We want the FastCGI servers to run under a different user than root. RUN_AS is this user. You can put the username or the UID of the user.
     126
     127To automatically start the service at start-up, configure the initial variables, put the fastcgi file in the /etc/init.d/ directory and execute in a terminal:
     128{{{
     129update-rc.d /etc/init.d/fastcgi defaults
     130}}}
     131
     132== With UNIX sockets ==
     133
     134This script uses TCP sockets for communication, but it is really simple to modify it in order to use Unix domain sockets:
     135
     136    * The sockets will be stored in the same folder as the .pid files, and will have the same name but with the .socket extension.
     137    * Modify the init script so it uses '''runfcgi socket=$RUNFILES_PATH/$SITE.socket pidfile=$RUNFILES_PATH/$SITE.pid''' instead of '''runfcgi host=$HOST port=$PORT pidfile=$RUNFILES_PATH/$SITE.pid'''.
     138    * Remove the '''HOST''' and '''PORT''' variables from the fastcgi file.
     139
     140== Some comments ==
     141Make sure to change the ownership of the folders where the FastCGI server must write. This is important for the folder where the .pid files are written.
     142
     143The script could be improved:
     144    * It does not have a finely grained site management. All sites are treated alike.
     145    * The port management is done a bit ad-hoc.
     146    * The FastCGI server does not work under root, but still has some flaws. For example, the .pid files can be read by www-data and can not be written in /var/run/. This is due to the fact that Django's manage.py does not seem to have an option to downscale to a lower-permission user as www-data.
     147    * The script is known to be working in '''Debian''' and '''Ubuntu''' based systems, but will not work on '''Gentoo''' due to it's particular init.d system. It should work at is in other '''Redhat based systems''' (Mandriva, Suse,...), although this has not been confirmed.
Back to Top