Given the following folder structure for an "productsapi" application based on Django REST framework and virtualenv.
/webapps/
└── projects
├── bin
│ ├── activate
│ ├── activate.csh
│ ├── activate.fish
│ ├── activate_this.py
│ ├── django-admin
│ ├── django-admin.py
│ ├── easy_install
│ ├── easy_install-2.7
│ ├── gunicorn
│ ├── gunicorn_django
│ ├── gunicorn_paster
│ ├── gunicorn_start.sh
│ ├── pip
│ ├── pip2
│ ├── pip2.7
│ ├── python
├── include
├── lib
├── local
├── logs
├── run
├── static
└── productsapi
├── products
├── manage.py
├── requirements.txt
└── productsapi
For this I would like to prepare a start script for gunicorn as described an article from 2013. I came up with the following:
#!/bin/bash
NAME="productsapi" # Name of the application
DJANGODIR=/webapps/projects/ # Django project directory
USER=exampleuser # User to run as
GROUP=examplegroup # Group to run as
NUM_WORKERS=3 # Worker processes to spawn
DJANGO_SETTINGS_MODULE=productsapi.settings # Settings file should Django use
DJANGO_WSGI_MODULE=productsapi.wsgi # WSGI module name
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
source ./../bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH
# Start your Django Unicorn
# Programs meant to be run under supervisor should
# not daemonize themselves (do not use --daemon)
exec ./bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--user=$USER \
--group=$GROUP \
--bind=127.0.0.0:8080 \
--log-level=DEBUG \
--log-file=-
I start the script as follows:
$ /webapps/projects/bin/gunicorn_start.sh
This fails with the following error message:
ImportError: No module named productsapi.wsgi
I looks like some paths are not set correctly.
I can however start the application server when I do the following (mind the different path):
$ cd /webapps/projects/productsapi
$ gunicorn productsapi.wsgi:application --user=exampleuser \
--groups=exampleuser --bind 127.0.0.1:8080 --log-file=- --log-level DEBUG
As you've noticed, your DJANGODIR should be
/webapps/projects/productsapi
.Basically, it should be the directory that has
manage.py
.You'll need to change
./bin/
to../bin
in your exec line.