virtualenv with gunicorn not working

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.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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 “my_project” in the “my_virtualenv” directory.

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

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