Deploying Django project with gunicorn and Nginx

Check out the new site at https://rkblog.dev.

gunicorn is a WSGI HTTP server that helps deploy various Python applications with servers like Nginx. It support Django, Pylons (paster) or other WSGI Python applications.

You can install gunicorn from sources or using PyPi: easy_install -U gunicorn. In doc/htdocs/ subfolder (you can get the sources) we will find documentation and various examples.

Django, gunicorn and NGINX

Here is the most basic configuration for a Django project. Open terminal and go to Django project folder and execute:
gunicorn_django --workers=2
Next configuje Nginx:
worker_processes 1;

user nobody nogroup;
pid /tmp/nginx.pid;
error_log /tmp/nginx.error.log;

events {
    worker_connections 1024;
    accept_mutex off;
}

http {
    include mime.types;
    default_type application/octet-stream;
    access_log /tmp/nginx.access.log combined;
    sendfile on;

    upstream app_server {
        #server unix:/tmp/gunicorn.sock fail_timeout=0;
        # For a TCP configuration:
         server 127.0.0.1:8000 fail_timeout=0;
    }

    server {
        listen 80 default;
        client_max_body_size 4G;
        server_name _;

        keepalive_timeout 5;

	location /site_media/  {
			root /PATH/TO/DJANGO/PROJECT;
			}
        location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            if (!-f $request_filename) {
                proxy_pass http://app_server;
                break;
            }
        }
	
    }
}
Set the path to your Django projet to get static files support and start up Nginx - your Django project should be alive on localhost. In the gunicorn documentation you will find other examples using sockets or start/stop bash scripts. Everything easy and fast to deploy.
RkBlog

Django web framework tutorials, 9 May 2010


Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact