June 9, 2012, midnight

Setting up Django with Nginx and uwsgi on Ubuntu 12.04

The good news is that ubuntu 12.04 comes with almost everything done, there is no need to download and compile stuff, you just have to fill the blank spaces. The first thing that is necessary is install the correct packages:

sudo apt-get install nginx-full uwsgi uwsgi-plugin-python python-pip && sudo pip install django

After the installation, you will have the following new config files and folder structures at /etc

To add a new application, let’s say: voorloopnul, you have to create the file /etc/uwsgi/apps-available/voorloopnul.ini with the minimum content:

workers = 2
chdir = /srv/apps/voorloopnul
env = DJANGO_SETTINGS_MODULE=voorloopnul.settings
module = django.core.handlers.wsgi:WSGIHandler()

The line 3 should point to the root of your django project, the line 4 should point to your project settings

A restart (service uwsgi restart) is required each time you add a new application at /etc/uwsgi/apps-enabled, this will create the directory structure at /run/uwsgi/app//{socket,pid}. It’s important to note, that every app file must and in .ini

The next logic step is make nginx talk with uwsgi servers, this will happens throught unix socket files located at /run/uwsgi/app//socket. So you have to edit the /etc/ngnix/sites-available/voorloonul.com file to make it looks like:

server {
    server_name voorloopnul.com www.voorloopnul.com;
    access_log /var/log/nginx/voorloopnul.com.access.log;
    location / {
        uwsgi_pass unix:///var/run/uwsgi/app/voorloopnul/socket;
        include uwsgi_params;

To remember

To enable apps/sites you have to do a symbolic link from *-avaible to the *-enabled folder. e.g:

ln -s /etc/nginx/sites-available/voorloopul.com /etc/nginx/sites-enabled/voorloopnul.com

ln -s /etc/uwsgi/apps-available/voorloopnul.ini /etc/uwsgi/apps-enabled/voorloopnul.ini

comments powered by Disqus