virtualenv with gunicorn not working

Posted December 3, 2014 34.5k 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 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 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*/
            /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]


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= \
   --config /etc/gunicorn.d/ \

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

1 comment

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
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= \
   --config /etc/gunicorn.d/ \

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


    setuid django
    setgid django
    chdir /home/django

    exec exec /home/django/projectenv/bin/gunicorn \
    –name=projectenv \
    –bind= \
    –config /etc/gunicorn.d/ \

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


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
source ../../env/bin/activate

# Create the run directory if it doesn't exist
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= \
  --log-level=debug \

My project structure is:

   - /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:


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

setuid django
setgid django

    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= \
        --config /etc/gunicorn.d/ \
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.