Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 522
Harry Avatar asked Jun 22 '12 14:06

Harry


People also ask

Is Nginx required with Gunicorn?

Nginx and Gunicorn work together Gunicorn translates requests which it gets from Nginx into a format which your web application can handle, and makes sure that your code is executed when needed. They make a great team! Each can do something, which the other can't.

Do I need Gunicorn with Django?

Gunicorn takes care of everything which happens in-between the web server and your web application. This way, when coding up your a Django application you don't need to find your own solutions for: communicating with multiple web servers. reacting to lots of web requests at once and distributing the load.

Can I use Gunicorn in production?

You can run Gunicorn by using commands or integrate with popular frameworks like Django, Pyramid, or TurboGears. For deploying Gunicorn in production see Deploying Gunicorn.


1 Answers

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..

like image 184
Harry Avatar answered Oct 17 '22 03:10

Harry