Search code examples
djangonginxvirtualenvgunicornupstart

Gunicorn and Django with Upstart and Nginx


First of all I have many Django instances setup and running like this.

In each project I have a script.sh shell script that starts gunicorn etc.:

 #!/bin/bash
  set -e
  LOGFILE=/var/log/gunicorn/app_name.log
  LOGDIR=$(dirname $LOGFILE)
  NUM_WORKERS=3
  # user/group to run as
  USER=root
  GROUP=root
  PORT=8060
  IP=127.0.0.1
  cd /var/www/webapps/app_name
  source ../bin/activate
  test -d $LOGDIR || mkdir -p $LOGDIR
  exec /var/www/webapps/bin/gunicorn_django -b $IP:$PORT -w $NUM_WORKERS \
    --user=$USER --group=$GROUP --log-level=debug --log-file=$LOGFILE 2>>$LOGFILE

When running this script from the command line with bash script.sh, the site works perfectly, so Nginx is setup right.

As soon as I use upstart with service app_name start the app starts and then just stops. It does not even write to the log file.

This is the app_name.conf file in /etc/init/app_name.conf :

description "Test Django instance"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
exec /var/www/webapps/app_name/script.sh

So what is the problem here? Cause running from command line works, but doing trough upstart does not. And I dont know where to see whats wrong?


Solution

  • Well, I figured it out. If any one ever run into something like this...

    Its basically a lack of knowledge about shell scripts that was holding me back.

    After commenting out each line o the script file i found the problem with the line : source ../bin/activate and all after that.

    The problem was that it had 2 spaces on front of it, and now I know it needs to be left aligned all the way. Now it works.

    This is how i figured it out:

    tail -f /var/log/syslog
    Jun 26 10:54:59 saturn7 init: app_name main process (3521) terminated with status 127
    

    I found out that status 127 is basically a command that is not found. So I know the problem was actually in the script file.

    But I am not sure why bash ./script.sh would work and not tell me anything is wrong? I need to read up about schell scripts..