Integrate django, uwsgi and nginx to run python web application

I am currently working on setting up django-cms in ubuntu 12.04.django is a popular python framework used widely for web application.uwsgi is a plugin used to connect python app to web server.

We can run a python(django) application without any webserver as below in virtualenv:

python manage.py runserver 127.0.0.1:8000

Integrating the application with web server results better performance and accessibility.Here’s how I did it.

Created the application directory in /usr/share/intranet/space.site.com, and installed virtualenv inside it.Following are the files and directories resides at top level in space.site.com.

env
manage.py
project.db
space
static

virtualenv installed files are in env and the django app(django-cms) is installed in space.

Install uwsgi
sudo apt-get install uwsgi

Create uwsgi configuration file space.site.com.ini in /etc/uwsgi/apps-available. Add below entries to the file.

[uwsgi]
virtualenv=/usr/share/intranet/space.site.com/env
thread=3
master=1
env = DJANGO_SETTINGS_MODULE=space.settings
module = django.core.handlers.wsgi:WSGIHandler()
chdir = /usr/share/intranet/space.site.com
socket = /run/uwsgi/app/space.site.com/space.site.com.socket
logto = /var/log/uwsgi/space.site.com.log

Note that my app name is space.

Now create link to apps-enabled directory.

ln -s /etc/uwsgi/apps-available/space.site.com.ini /etc/uwsgi/apps-enabled/space.site.com.ini

Create nginx configuration file named space for the app in /etc/nginx/sites-available.Add below entries to it.

server {
listen 80;
server_name space.site.com;

location / {
uwsgi_pass unix:///run/uwsgi/app/space.site.com/space.site.com.socket;
include uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param SERVER_SOFTWARE nginx/$nginx_version;

}

location /static {
root /usr/share/intranet/space.site.com/space/static;
index index.html index.htm;

}

}

Now create link to sites-enabled directory.
ln -s /etc/nginx/sites-available/space /etc/nginx/sites-enabled/space

Restart nginx
sudo service nginx restart

Restart uwsgi
service uwsgi restart

Access the application in browser by typing http://space.site.com/.It should open the site.

For any error check the log file.

Here are few errors I came across.

Tue Nov 11 07:02:06 2014 - opendir(): No such file or directory [uwsgi.c line 471]

This issue was fixed by creating the directory below.
mkdir -p /usr/lib/uwsgi/plugins

unavailable modifier requested: 0

This issue was fixed by installing uwsgi-plugin-python
sudo apt-get install uwsgi-plugin-python

django CMS installation error

django CMS is a python based cms which can be used as intranet application.While installing it in python virtualenv with djangocms-installer, I came accross the error below.

This was tested in Ubuntu 12.04.4 LTS.

EnvironmentError: Pillow is not compiled with JPEG support, see 'Libraries installation issues' documentation section: http://djangocms-installer.readthedocs.org/en/latest/libraries.html

Steps below can be followed to fix this issue.

sudo apt-get install libjpeg-dev libfreetype6-dev zlib1g-dev
sudo pip uninstall pillow
sudo pip install pillow