Django (with Nginx & uWSGI)
04 July 2014This 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