All posts All posts by this author Paper color Change page color Announcements

Django + uWSGI + Nginx configuration on ubuntu 13.04

Considaring you have Nginx and uWSGI installed

Create virtualenv:

$ virtualenv venv

Activate virtualenv:

$ source venv/bin/activate

Install Django:

$ pip install django

Create Django project

$ startproject project_name

Edit _project_name/project_name/ file

$ vi project_name/project_name/

Add following to the file

import os
import sys
import site

DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(DIR, '..'))

ENV_PATH = '../../venv/lib/python2.7/site-packages'
VENV = os.path.join(DIR, ENV_PATH)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings")

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

Uwsgi configuration

Install uWSGI python plugin

$ sudo apt-get install uwsgi-plugin-python

Create _/etc/uwsgi/apps-available/project_name.ini

$ sudo vi /etc/uwsgi/apps-available/project_name.ini

Add following to the file


Create soft link

$ sudo ln -s /etc/uwsgi/apps-available/project_name.ini /etc/uwsgi/apps-enabled/project_name.ini

Nginx configuration

Create /etc/nginx/sites-available/domain_name

$ sudo vi /etc/nginx/sites-available/domain_name

Add following to the file

server {
    listen   80;
    charset utf-8;
    root /path_to/project_name;
    server_name domain_name;
    access_log /path_to/project_name/access.log;
    error_log /path_to/project_name/error.log;
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi.project_name.socket;
        uwsgi_param UWSGI_PYHOME /path_to/venv;
        uwsgi_param UWSGI_CHIDIR /path_to/project_name/project_name;
        uwsgi_param UWSGI_SCRIPT wsgi;

Create soft link

$ sudo ln -s /etc/nginx/sites-available/domain_name /etc/nginx/sites-enabled/domain_name

Edit /etc/hosts file

$ sudo vi /etc/hosts

Add following to the file domain_name

Restart uWSGI and Nginx

$ sudo service uwsgi restart
$ sudo service nginx restart

On browser


default response from django server