virtualenv with gunicorn not working

December 3, 2014 8.2k views

I am developing a django project in the instantiate (droplet) in which is created ubuntu server 14.04 + django.

far so good, but when configuring the instance with virtualenv as the documentation says http://gunicorn-docs.readthedocs.org/en/latest/deploy.html#using-virtualenv the virtualenv is not recognized because when installing an app (with pip installer) eg "django framework rest" this is not recognized and throws 500 error since there is no such app.

I was also seeing this post https://www.digitalocean.com/community/questions/usinv-virtualenv-in-django-image in which mention the modification of the configuration file in which you must assign the address of the new gunicorn that is created in the virtualenv but at the time of the service restart it throws an error 502: /

/* structure directory*/
/home/django
            /myproject (django)
            /envs/myapp (virtualenvs)


/* Gunicorn file */
description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

respawn

setuid django
setgid django
chdir /home/django

exec gunicorn \ (**Here is where you change the url and gave 502 error)
   --name=myproject \
   --pythonpath=myproject \
   --bind=0.0.0.0:9000 \
   --config /etc/gunicorn.d/gunicorn.py \
   conf.wsgi:application

have no idea why this situation? someone been in a situation like that can help me to solve this problem.

1 comment
3 Answers

When working inside of a virtualenv, you need to modify the Upstart script to use gunicorn from inside the virtualenv. So the exec stanza would be something like:

exec exec /home/envs/myapp/bin/gunicorn \
   --name=myproject \
   --pythonpath=myproject \
   --bind=0.0.0.0:9000 \
   --config /etc/gunicorn.d/gunicorn.py \
   conf.wsgi:application

The problem you are facing with Django REST framework is a little unclear. Any further information you could provide would probably help. To install it with pip the command is: pip install djangorestframework

  • i am also getting 502 when adding my /path/to/my/virtualenv/bin/gunicorn in my Upstart script

  • I am using default OneClick Installation Image and I tried your solution but it didn't worked. Default site is working though but the second site on exact same path with virtual environment like /home/django/projectenv/bin/gunicorn is not accepting the connection. Please advise as I've been trying this from a while.

    Following are my settings:
    description "Gunicorn daemon for Django project"

    start on (local-filesystems and net-device-up IFACE=eth0)
    stop on runlevel [!12345]

    If the process quits unexpectadly trigger a respawn

    respawn

    setuid django
    setgid django
    chdir /home/django

    exec exec /home/django/projectenv/bin/gunicorn \
    --name=projectenv \
    --pythonpath=projectenv\
    --bind=127.0.0.1:9500 \
    --config /etc/gunicorn.d/gunicorn.py \
    projectenv.wsgi:application

I'm using supervisor with the following start up script maybe this will help

#!/bin/bash

NAME="project_name"                               # Name of the application
DJANGODIR=/var/www/my_project/project_name/project_name            # Django project directory
SOCKFILE=/var/www/my_project/env/run/gunicorn.sock      # we will communicte using this unix socket
USER=nobody                                         # the user to run as
NUM_WORKERS=3                                       # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=config.settings.production      # which settings file should Django use
DJANGO_WSGI_MODULE=config.wsgi              # WSGI module name

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source ../../env/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 ../../env/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER \
  --bind=0.0.0.0:8001 \
  --log-level=debug \
  --log-file=-

My project structure is:

/var/www/my_project
   - /env (Virtual Environment)
   - /project_name   
      - /bin
      - /logs
      - /project_name (Django)
      - /requirements
      - requirements.txt

The route I went was to install gunicorn in my virtual environment and then call gunicorn from there. Here is my upstart config:

#!/bin/bash

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django

script
    exec 2>>/tmp/upstartconfig.log
    set -x
    # Run gunicorn from the virtualenv
    cd /home/django/my_virtualenv

    exec /home/django/my_virtualenv/bin/gunicorn \
        --name=my_project \
        --pythonpath=my_project \
        --bind=127.0.0.1:9000 \
        --config /etc/gunicorn.d/gunicorn.py \
        wsgi:application
end script

/tmp/upstartconfig.log will give you any errors upstart encounters when executing the "script" portion of your upstart config.

I installed "myproject" in the "myvirtualenv" directory.

Have another answer? Share your knowledge.