Django (with Nginx & uWSGI)

04 July 2014
This is a condensed version of the Django+uWSGI+nginx how-to at readthedocs.org, which has been modified to use tarball installs rather than package management. I have long given up contending with differences that various Linux distributions insist on having, so I much prefer manual installations even when there are packaged versions available. I wrote up these instructions a while ago, and since it is intended as a fast reference rather than a detailed one, verbose descriptions have been omitted as they tend not to add that much value.

Installation procedure

Here /home/django is used as a placeholder installation root, and assume the reader is knowledgeable enough to make adjustments as needed. Apart from binding to port 80, none of this should need root access.

Python

Much the same procedure I used to get wxPython working:

tar -xzvf Python-2.7.6.tgz cd Python-2.7.6/ ./configure --prefix=/home/django/Python make && make install export PATH=/home/django/Python/bin/:$PATH

Nginx

I assume Nginx has already been installed. This configuration snippit summarises what settings are needed:

server { server_name django.local; root /home/django/; location /images { autoindex on; root /home/django/images/; index index.shtml index.html index.php; } location /static { alias /home/django/mysite/static; #alias mysite/static; } location / { include uwsgi_params; uwsgi_pass unix://tmp/django.sock; } }

uWSGI

Install uWSGI from tar-ball and then run it with test.py (offsite link). Test by pointing a browser at you Nginx server.

wget http://projects.unbit.it/downloads/uwsgi-2.0.4.tar.gz tar -xzvf uwsgi-2.0.4.tar.gz cd uwsgi-2.0.4/ make ./uwsgi --socket /tmp/django.sock --wsgi-file test.py --chmod-socket=666

The makefile does not seem to support make install so eventually you will need to copy the uwsgi binary to somewhere suitable (such as /usr/local/bin). Note that the version of uWSGI included in Ubuntu 12.04 is v1.0.3 which does not support the --wsgi-file parameter.

Django

Next stage is to replace the test script above with a Django install:

wget https://www.djangoproject.com/download/1.6.5/tarball/ -O Django-1.6.5.tar.gz tar -xzvf Django-1.6.5.tar.gz cd Django-1.6.5/ python setup.py install cd .. django-admin.py startproject mysite cd mysite echo -e "\n\nSTATIC_ROOT = os.path.join(BASE_DIR, 'static')" >> mysite/settings.py python manage.py collectstatic ../uwsgi-2.0.4/uwsgi --socket /tmp/django.sock --module mysite.wsgi

If this works, you can replace the commandline parameters with the following uwsgi.ini file:

[uwsgi] chdir=/home/django/mysite module=mysite.wsgi socket=/tmp/django.sock

..and use it as follows:

./uwsgi-2.0.4/uwsgi --ini mysite/uwsgi.ini

General impressions

The tutorials are a bit long-winded, and partly as a result of using it as a guide to reimplement a micro-site of mine, the order it covers stuff feels a bit unnatural. It will likely take a few throwaway site projects before things seem to fall into place, which will be frustrating at times. Nevertheless once things do fall into place, it seems to hit a productivity sweet-spot. Django is clearly aimed at those who find full content management systems such as Plone to be that bit too rigid and/or overkill, but nevertheless still want off-the-shelf stuff like user logins and an administration backend. It feels much like a content management system in kit form, so leaves me with the somewhat-unfair impression that it is intended for medium-sized sites rather than customisability, even though the latter is precisely what it is good for.