Dunaevsky Maxim
Dunaevsky Maxim

Reputation: 3199

Running Django projects from virtualenv with Gunicorn >=19

I saw news on docs.gunicorn.org for gunicorn v.19:

Deprecations

run_gunicorn, gunicorn_django and gunicorn_paster are now completely deprecated and will be removed in the next release. Use the gunicorn command instead.

I run my applications from virtual environments, created with virtualenv with this command in supervisor:

[program:my_app]
command=/var/www/.virtualenvs/my_app/bin/gunicorn_django -c /var/www/my_app/conf/gunicorn.conf.py

user=www-data
group=www-data

daemon=false
debug=false

autostart=true
autorestart=true

redirect_stderr=true
stdout_logfile=/var/www/my_app/log/supervisor.log

How should I change my settings to run my projects with the new version gunicorn?

Upvotes: 3

Views: 544

Answers (1)

Kevin Campbell
Kevin Campbell

Reputation: 774

The command line should be changed to the following

command=/var/www/.virtualenvs/my_app/bin/gunicorn my_app.wsgi:application -c /var/www/my_app/conf/gunicorn.conf.py

This is assuming that you have the file my_app/wsgi.py. Since Django 1.4, startproject has generated a wsgi.py file for you as part of your project. I'd assume you have this, but if not you can use the following snippet to create that file.

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_app.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

See https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/

You may need to ensure that the my_app directory is in your PYTHONPATH if it is not already, or you will get errors.

To test this out standalone on the command line with a new django project, the following should work assuming you have django and gunicorn already installed in you current environment.

django-admin.py startproject myproject
cd myproject
export PYTHONPATH=$PYTHONPATH:.
gunicorn myproject.wsgi:application -b localhost:8008

Upvotes: 1

Related Questions