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

$ django-admin.py startproject project_name

Edit _project_name/project_name/wsgi.py file

$ vi project_name/project_name/wsgi.py

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)
site.addsitedir(VENV)

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

[uwsgi]
plugins=python
gid=www-data
uid=www-data
socket=/tmp/uwsgi.project_name.socket
pythonpath=/path_to/project_name/project_name

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

127.0.0.1 domain_name

Restart uWSGI and Nginx

$ sudo service uwsgi restart
$ sudo service nginx restart

On browser

http://domain_name

default response from django server

Software Solutions