django deployment with nginx and uwsgi

There are many tutorial/procedures on this topic, I just wish to add a few notes on the pieces I’ve found more obscure and difficult to setup.

I’m running a VM on slicehost and I haven’t alredy found the resources to perform a full upgrade to something newer than the original Ubuntu Hardy which was installed at setup time.

Basically, I’ve compiled latest nginx from sources (older versions did not support uwsgi natively) and installed uwsgi with

sudo pip install uwsgi

not running on a virtualenv on this machine :(

what I’ve found really hard to fix was the wrong filesystem encoding wich lead to the famous encoding errors:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' inposition 86: ordinal not in range(128)

The fix is trivial: you must convince uwsgi that is running in an UTF-8 locale, to do so, you must set environment vars as explained in django docs   (for apache/mod_python, but the root is the same).

Having had an hard time to set env on upstart, I’ve switched to a standard init script for uwsgi:

#! /bin/bash
# /etc/init.d/uwsgi
#
 
daemon=/usr/bin/uwsgi
pid=/tmp/project-master.pid
# run as www-data
args="--uid=33 --gid=33 --ini /home/public_html/project/uwsgi.ini --pidfile $pid"
 
# Carry out specific functions when asked to by the system
case "$1" in
    start)
        echo "Starting uwsgi"
        LC_ALL='en_US.UTF-8' LANG='en_US.UTF-8' start-stop-daemon -p $pid --start --exec $daemon -- $args
        ;;
    stop)
        echo "Stopping script uwsgi"
        LC_ALL='en_US.UTF-8' LANG='en_US.UTF-8' start-stop-daemon --signal INT -p $pid --stop $daemon -- $args
        ;;
    reload)
        echo "Reloading conf"
        kill -HUP $(cat $pid)
        ;;
    *)
        echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
        exit 1
    ;;
esac
 
exit 0

Here the trick is to set env vars on the start-stop-daemon line.

The application ini is something like:

[uwsgi]
chdir=/home/public_html/project/
master=True
vacuum=True
max-requests=5000
daemonize=/var/log/uwsgi/uwsgi.log
socket=127.0.0.1:49152
env = DJANGO_SETTINGS_MODULE=settings
module = django.core.handlers.wsgi:WSGIHandler()

Last missing piece is log rotation:

 

"/var/log/uwsgi/*.log" {
  copytruncate
  daily
  rotate 5
  compress
  delaycompress
  missingok
  notifempty
}

Lascia un Commento