# rc.d for invoking a django FastCGI daemon



## zeissoctopus (Sep 26, 2013)

Hello, I have written a small rc.d script for invoking an instance of the Django fastcgi daemon as follows.

```
#!/bin/sh
#===============================================================================
# version: 0.5
# author:  Hon-Yu Lawrence Cheung, a.k.a. zeissoctopus (cheunghonyu@gmail.com)
#
# Aims: To start an instant of fastcgi daemon for django 1.5 website with
#       a non-root user a/c automatically.
#===============================================================================
# The prerequisites of this rc.d script are:
# 1. install lang/python port
# 2. install www/py-flup port
# 3. install www/py-django port
# 4. create a dummy user a/c (this script default : django15cgi)
# 5. create directory /var/run/django15cgi with access right for django15cgi a/c
# 6. create directory /var/log/django15cgi with access right for django15cgi a/c
#===============================================================================
# How to use:
#
# Copy this rc.d script into directory /usr/local/etc/rc.d with executive
# permission.
#
# To start the daemon, please add the line django15fcgid_enable="YES" to the
# /etc/rc.conf. You may override default parameters values with your own setting
# in /etc/rc.conf, too. The followings are the default parameters values.
#
# django15fcgid_enable="NO"
# django15fcgid_projectdir="/home/www/django15cgi/yourproject"
# django15fcgid_user="django15cgi"
# django15fcgid_host="127.0.0.1"
# django15fcgid_port="7080"
# django15fcgid_protocol="fcgi"
# django15fcgid_method="threaded"
# django15fcgid_maxrequests="800"
# django15fcgid_pidfile="/var/run/django15cgi/django15fcgid.pid"
# django15fcgid_errlog="/var/log/django15cgi/django15fcgid-error.log"
#===============================================================================

# PROVIDE: django15fcgid
# REQUIRE: LOGIN
# KEYWORD: shutdown

. /etc/rc.subr

load_rc_config django15fcgid

#===============================================================================
# set defaults
#===============================================================================
django15fcgid_enable=${django15fcgid_enable:-"NO"}
django15fcgid_projectdir=${django15fcgid_projectdir:-"/home/www/django15cgi/yourproject"}
django15fcgid_user=${django15fcgid_user:-"django15cgi"}
django15fcgid_userhome=${django15fcgid_userhome:-"/home/django15cgi"}
django15fcgid_host=${django15fcgid_host:-"127.0.0.1"}
django15fcgid_port=${django15fcgid_port:-"7080"}
django15fcgid_protocol=${django15fcgid_protocol:-"fcgi"}
django15fcgid_method=${django15fcgid_method:-"threaded"}
django15fcgid_maxrequests=${django15fcgid_maxrequests:-"800"}
django15fcgid_pidfile=${django15fcgid_pidfile:-"/var/run/django15cgi/django15fcgid.pid"}
django15fcgid_errlog=${django15fcgid_errlog:-"/var/log/django15cgi/django15fcgid-error.log"}

django15_fcgi_daemon_cmd="${django15fcgid_projectdir}/manage.py runfcgi \
protocol=${django15fcgid_protocol} \
host=${django15fcgid_host} \
port=${django15fcgid_port} \
method=${django15fcgid_method} \
maxrequests=${django15fcgid_maxrequests} \
pidfile=${django15fcgid_pidfile} \
workdir=${django15fcgid_userhome} \
errlog=${django15fcgid_errlog} \
daemonize=YES"

name=django15fcgid
rcvar=django15fcgid_enable

start_cmd="${name}_start"
stop_cmd="${name}_stop"

django15fcgid_start()
{
    su -l ${django15fcgid_user} -c "exec ${django15_fcgi_daemon_cmd}"
    echo "django 1.5.x FastCGI daemon start successfully..."
}

django15fcgid_stop()
{
    if [ -f ${django15fcgid_pidfile} ]; then
         kill `cat ${django15fcgid_pidfile}`
         rm -rf ${django15fcgid_pidfile}
    echo "django 1.5.x FastCGI daemon stop successfully..."
    else
       echo "django 1.5.x FastCGI daemon is not running"
    fi
}

run_rc_command "$1"
```


----------

