django under sub-url powered by uwsgi behind nginx

By | March 30, 2011

The Django frameworkThis post describes how to deploy django application under sub-url of web site and serve it behing nginx frontend.

There are many different ways to serve python application. This article will touch configuration of only one of them: uwsgi – lightweight application server, which can serve WSGI applications.

What is covered in this post:

  • preparing python environment
  • configuring django applicaiton for sub-url deployment
  • preparing django application for uwsgi
  • configuring nginx
  • starting application

What is not covered in this post:

  • installing django framework
  • installing uwsgi
  • installing nginx

Tested on: django v.1.2, uwsgi 0.9.7.1 with this patch, nginx 0.8.54.

Python virtual environment installed at /opt/python-django-app
Django application deployed at /htdocs/my-django-app


There is a problem in compiling mentioned above version of uwsgi. In order to compile, first the following patch should be applied to source tree: http://permalink.gmane.org/gmane.comp.python.wsgi.uwsgi.general/1140

Preparing python environment

It is very convenient to install python virtual environment to have independent python installation per application. Virtualenv is used to maintain separate instances of python. After installing virtualenv it is as simple as running the following command in shell to create another python environment, assuming python binary is on your path:

python virtualenv.py /opt/python-django-app

 

Configuring django applicaiton for sub-url deployment

Edit settings.py, add FORCE_SCRIPT_NAME and edit ADMIN_MEDIA_PREFIX as below:

FORCE_SCRIPT_NAME = '/suburl'
ADMIN_MEDIA_PREFIX = '%s/static/admin/' % FORCE_SCRIPT_NAME

 

Preparing django application for uwsgi

Under django application root create a file, lets name it wsgi_app.py, the content is as follows:

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Uwsgi will use this file to start django application.

Configuring nginx

Add the following to nginx configuration:

upstream my-django-app {
    server unix:///tmp/uwsgi.socket;
}
server {
    listen 80;
    server_name www.host.name;
    location /suburl/static/admin/ {
        alias /opt/python-django-app/lib/python2.6/site-packages/django/contrib/admin/media/;
    }
    location /suburl/ {
        include uwsgi_params;
        uwsgi_param SCRIPT_NAME         /suburl;
        uwsgi_param UWSGI_SCRIPT        wsgi_app;
        uwsgi_modifier1                 30;
        uwsgi_pass                      my-django-app;
    }
    access_log    /var/log/nginx/access_log main;
    error_log     /var/log/nginx/error_log;
}

 

Starting application

Start django application by running the following:

uwsgi -i -M -p 1 -T -t 30 -R 2000 -m --limit-as 256 -H /opt/python-django-app --python-path /htdocs/my-django-app -s /tmp/uwsgi.socket -C -w wsgi_app --ignore-script-name --pidfile /var/run/my-django-app.pid

Start nginx and try to access your application from the browser under the corresponding url: http://…/suburl/

More details on configuring uwsgi behind nginx can be found here.


Leave Your Comment

Your email will not be published or shared. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>