This blog is more like technical documentation on how to setup Django, Nginx and uWSGI. Let's see short and sweet definition of Nginx and Django.

Nginx ("engine x") is a lightweight and high-performance HTTP server, reverse proxy and mail proxy, released under a BSD-like license

Django is the MVC framework written Python for perfectionists with deadlines with primary goal is to ease the creation of complex database-driven websites.

This tutorial is divided into three sections: Installation Django, Nginx and MySQL, Creation of Django Project and Configuration of Nginx and uWSGI.

This blog is developed based on my hands on experice during server setting up process for ontreat.com. We have used on EC2 and Window Azure to host ontreat.

Part 1: Installation Django, Nginx and MySQL

1) Install mysql

$sudo apt-get install mysql-server mysql-client

2) Install Nginx

$sudo apt-get install nginx

3) Install uWSGI

$sudo aptitude install python-dev
$sudo apt-get install python-pip
$sudo pip install uwsgi

4. Install Django4.1 Install Python MySQL Connector - MySQL Connector/Python (http://dev.mysql.com/downloads/connector/python/) - Download source - http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.2.tar.gz - Extract and go inside mysql-connector-python-* package

$sudo python setup.py install

4.2 Install MySQLdb:

$sudo apt-get install python-mysqldb

- Download - $wget https://www.djangoproject.com/m/releases/1.7/Django-1.7.1.tar.gz

$tar -xf Django-*.tar.gz //extract
$cd Django-* folder //change directory
$sudo python setup.py install  //Install

Part 2: Creation of Django Project

Django project:1) Generate project:

$django-admin startproject yoursite

2) Create Database

$cd mysql -uroot
create database yoursite

3) Update database to MySQL in yoursite/yoursite/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'yoursite',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': '',
    }
}

4) Generate frist app:

$python manage.py startapp analytics

5) Activate module by adding in INSTALLED_APPS in settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'analytics'
)

Part 3: Configuration of Nginx and uWSGI

Configure Nginx1) Create vhost file /etc/nginx/site-available

upstream django {
    server unix:///home/ubuntu/yoursite/yoursite/uwsgi.sock;
}

server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    server_name yoursite.com; # you also can add IP e.g. 232.232.12.21
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/ubuntu/yoursite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/ubuntu/yoursite/static; # your Django project's static files - amend as required
    }

    location / { # All non-static requests will be passed to Django.
        uwsgi_pass django;
        include /home/ubuntu/yoursite/yoursite/wsgi_params;
    }
}

2) Create params file /home/ubuntu/yoursite/yoursite/wsgi_params

uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;

3) Enable site, go to /etc/nginx/site-enable, $sudo ln -s ../site-available/yoursite 4) Restart Nginx

$sudo /etc/init.d/nginx restart

Configure uWSGI1) setup uwsgi.ini (param file)

[uwsgi]
; Starting path
chdir = /home/ubuntu/yoursite/yoursite
; uWSGI will be accessible through a file socket:
socket = /home/ubuntu/yoursite/yoursite/uwsgi.sock
; 664 or 644 offers better security...
chmod-socket = 666
; Set logging file
logto = /home/ubuntu/yoursite/uwsgi.log
; The entry point into the application.
module = yoursite.wsgi
; Ensure logging is enabled.
disable-logging = false

2) Run uWSGI

uwsgi --ini /home/ubuntu/yoursite/yoursite/uwsgi.ini &