Code

Django, Nginx, uWsgi Setup

The following are useful configurations for setting up Django on Ubuntu 12.04 with Nginx and uWsgi with ssl encryption.

Nginx config (/etc/nginx/sites-enabled):

server {
    server_name <SERVER NAME>;
 
    listen 80;
 
    rewrite ^(.*)$ https://$host$1 permanent;
}
 
server {
    server_name <SERVER NAME>;
 
    listen 443;
 
    ssl on;
    ssl_certificate <PATH TO CERT FILE>.crt;
    ssl_certificate_key <PATH TO PRIVATE KEY>.key;
 
    ssl_prefer_server_ciphers On;
    ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:RC4-SHA;
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/<PATH TO SOCK FILE>/<FILE>.sock;
        uwsgi_read_timeout 150;
    }
 
    location /static {
        alias <PATH TO STATIC FOLDER>/static;
    }
 
}

 

uWsgi config (/etc/uwsgi/apps-enabled):

[uwsgi]
chdir          = <PATH TO DJANGO CODEBASE>
module         = <PATH TO DJANGO WSGI FILE>.wsgi:application
socket         = <PATH TO SOCK FILE>.sock
chmod-socket   = 660
master         = true
enable-threads = true
vacuum         = true
harakiri       = 120
max-requests   = 2000
stats          = 127.0.0.1:9191
memory-report  = true
 
cheaper-algo                = busyness
workers                     = 50
cheaper                     = 5
cheaper-initial             = 5
cheaper-overload            = 30
cheaper-rss-limit-soft      = 1051066368
cheaper-rss-limit-hard      = 1366386278
cheaper-busyness-multiplier = 10
cheaper-busyness-penalty    = 2
cheaper-busyness-verbose    = true

 

This uses the uWsgi cheaper algorithm which is a really easy and cool way to dynamically scale the uWsgi workers.

http://uwsgi-docs.readthedocs.org/en/latest/Cheaper.html

These are the basic configuration files, remember to install any project dependencies and check permissions on log directories!