/ linux

Nginx, Uwsgi, Virtualenv and Django on Ubuntu 12.04

First install required applications.

sudo apt-get install nginx uwsgi uwsgi-plugin-python python-virtualenv

Versions of packages that will be used:

  • Nginx 1.1.19.1
  • Uwsgi 1.0.3+dfsg-1ubuntu0.1
  • Virtualenv 1.7.1.2-1
  • Django 1.4.1

Virtualenv.

I store my project in ~/projects. Now I'm creating python virtual environment for my project and I'm installing Django.

cd ~/projects/
virtualenv eshlox.net
cd eshlox.net
source bin/activate
pip install django
django-admin.py startproject project

Nginx configuration.

IMHO, by default, nginx is configured for basic tasks. I won't change this configuration in this entry. Configuration files are stored in /etc/nginx/sites-available. Go to this directory and create a new file.

cd /etc/nginx/sites-available
vim eshlox.net

It's example configuration.

server {
    listen  80;
    server_name eshlox.net www.eshlox.net;
    access_log /var/log/nginx/eshlox.net_access.log;
    error_log /var/log/nginx/eshlox.net_error.log;

    location / { 
        uwsgi_pass  unix:///tmp/eshlox.net.sock;
        include     uwsgi_params;
    }   

    location /media/  {
        alias /home/eshlox/projects/eshlox.net/project/project/media/;
    }

    location  /static/ {
        alias  /home/eshlox/projects/eshlox.net/project/project/static/;
    }
}

We must create symlink to enable this.

cd /etc/nginx/sites-enabled
ln -s ../sites-available/eshlox.net .

Uwsgi.

Like with Nginx.. configuration files are stored in /etc/uwsgi/apps-available. Go to this directory and create a new file.

cd /etc/uwsgi/apps-available
vim eshlox.net.ini

Edit eshlox.net.ini

[uwsgi]
vhost = true
plugins = python
socket = /tmp/eshlox.net.sock
master = true
enable-threads = true
processes = 2
wsgi-file = /home/eshlox/projects/eshlox.net/project/project/wsgi.py
virtualenv = /home/eshlox/projects/eshlox.net
chdir = /home/eshlox/projects/eshlox.net/project
touch-reload = /home/eshlox/projects/eshlox.net/project/reload

Enable this.

cd /etc/uwsgi/apps-enabled/
ln -s ../apps-available/eshlox.net.ini .

That's all. Now, run this services.

sudo service nginx start
sudo service uwsgi start

Of course, this is a very basic configuration. Change it according to your needs.