Skip to content

Instantly share code, notes, and snippets.

@tr00st
Created April 28, 2015 10:02
Show Gist options
  • Save tr00st/9c3781640e748712de87 to your computer and use it in GitHub Desktop.
Save tr00st/9c3781640e748712de87 to your computer and use it in GitHub Desktop.
Nginx + Django + Gunicorn + Python 3.4 Configs

What?

This is a minimal but effective set of configuration files and setup for running Django (targetted at 1.8) with Python 3.4, Gunicorn, Nginx

Why?

Because I'm sick and tired of writing this config over and over :)

How?

Create the files specced above, place them in the relevant locations, replacing "django-project" with the name of your project.

# /etc/nginx/sites-available/django-project
server {
listen 80;
location ^~ /static/ {
root /srv/django-project/;
}
location / {
proxy_pass http://localhost:28000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
cd /srv/django-project
virtualenv -p /usr/bin/python3.4 --prompt='(django-project) ' env
. env/bin/activate
pip install gunicorn tornado
pip install -r requirements.txt
sudo service django-project start
sudo service nginx restart
# /etc/init/django-project.conf
description "Django Project"
start on (filesystem)
stop on runlevel [016]
respawn
setuid nobody
setgid nogroup
chdir /srv/django-project
exec /srv/django-project/env/bin/gunicorn django-project.wsgi -b localhost:28002 -k tornado
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment