The only issue with it is that finding good straightforward tutorial on its deployment hasn't been easy for me. The problem araise not by gunicorn but the rather convulted method of supervisor which adds more complexity to the process.
Fortunately there is the much simpler way to keep gunicorn running: upstart
First you need to install pip and virtaulenv:
apt-get install python-pip
pip install virtualenv
Switch to a non-root user and create a virtualenv
su myuser
virtualenv ~/.myenv
virtualenv ~/.myenv
activate the virtaualenv
source ~/.myenv/bin/activate
(.myenv)# pip install gunicorn
create a gunicorn.sh in /path/to/project, like this:
Note: DO NOT create /tmp/gunicorn.sock#!/bin/bash
NAME="myproject" # Name of the application
DJANGODIR=/path/to/project # Django project directory
SOCKFILE=/tmp/gunicorn.sock # we will communicte using this unix socket
USER=john # the user to run as
GROUP=www-data # the group to run
NUM_WORKERS=9 # twice the number of cpu cores
DJANGO_SETTINGS_MODULE=sai.settings # which settings file should Django use
DJANGO_WSGI_MODULE=sai.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source /home/user/.virtualenv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /home/john/.virtualenv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER --group=$GROUP \
--bind=unix:$SOCKFILE \
--log-level=debug \
--log-file=-
gunicorn will do that for you
Make sure that upstart is installed:
apt-get install upstart
Now create an upstart job in /etc/init to run gunicorn.sh
nano /etc/init/gunicorn.conf
And put in it:
description "Gunicorn application server handling myproject"
start on runlevel [2345]
stop on runlevel [!2345]
respawn
setuid john
setgid www-data
chdir /path/to/project
exec bash gunicorn.sh
Now you can start gunicorn like any service :
service gunicorn restart
The only remaining step is to pass request to gunicorn in nginx, by a file like this:
Note 2: If you just started upstart, you may need to reboot to be able to service gunicorn restart.upstream myproject {proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host;
server unix:///tmp/gunicorn.sock; # the socket will be created automatically
}
server {
#...other stuff
location / {if (!-f $request_filename) {
proxy_pass http://myproject;
break;
}
}
}
That's it!
Restart nginx and enjoy gunicorn.
No comments:
Post a Comment