Django and nginx


6 posts
by ceefan » Tue Jun 19, 2012 7:27 pm
Hi all,

I'm still a few days away from getting my first RPi (3+ months waiting!), but I've started hashing together the beginnings of a project on my Ubuntu system. I was wondering if anybody could comment on the speed that a RPi would handle something like this.

Basically I want to collect data from a USB serial port and dispatch that via a Python script. The data would be fed to a database using POST data and localhost URLs served up by nginx and Django's own FastCGI handler. (see https://code.djangoproject.com/wiki/DjangoAndNginx). Django would store the information into some undetermined flavor of SQL, as well as offer some views into the data (tables, stats, etc.). I doubt there would ever be two users at a time using the system.

Any insight on how it would "feel" to the user? Would the system be able to handle something like Postgre/MySQL? Also, I'm planning on stripping down the rest of the system to the bare essentials.

Thanks in advance for any thoughts!
Posts: 15
Joined: Fri Mar 16, 2012 7:46 pm
by HansH » Tue Jun 19, 2012 8:49 pm
use sqlite, standard available in python

Save that data directly with python/sqlite (not using a post)
use nginx or better thttpd as frontend for presenting the data

that way it will only use a few MB.
try to keep it as simple as possible.....
Posts: 212
Joined: Mon Sep 05, 2011 7:49 am
by ceefan » Thu Jun 21, 2012 8:48 pm
So my Pi came this morning and I put together this script to get nginx and Django up and working. It's in two parts because you have to log out and back in first to make the user permissions stick.

It installs nginx, sets up /var/www with a static folder, adds the user pi to a www-pub group for editing in /var/www, and starts a Django project in /var/www/django

It's using information from the following pages: https://code.djangoproject.com/wiki/DjangoAndNginx and http://serverfault.com/questions/6895/whats-the-best-way-of-handling-permissions-for-apache2s-user-www-data-in-var

By the way, this is how to get the FastCGI running in the background as a daemon: https://code.djangoproject.com/wiki/InitdScriptForLinux (I haven't gotten this part to work yet)

To make it easier on yourself, I'd put these into a text file like 1.txt and 2.txt, and run them as "sudo sh 1.txt" and "sudo sh 2.txt"

Let me know if you run into any issues. I tried this on a brand new Wheezy install, by the way.

Code: Select all
#! /bin/sh
# get nginx
sudo apt-get update
sudo apt-get install nginx sqlite
# set up nginx
cd ~
sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/defaultoriginal
wget https://gist.github.com/gists/2968299/download
tar xzvf download
sudo cp ./gist2968299-ea7165ae2edd7dbb6e37b9388a6d1e1e7f1e520e/default /etc/nginx/sites-available/default
rm -rf download
rm -rf gist2968299-ea7165ae2edd7dbb6e37b9388a6d1e1e7f1e520e
# create user permissions for /var/www
sudo groupadd www-pub
sudo mkdir /var/www
sudo mkdir /var/www/django
sudo mkdir /var/www/static
sudo usermod -a -Gwww-pub pi
sudo chown -R root:www-pub /var/www
sudo chmod 2775 /var/www
sudo find /var/www -type d -exec chmod 2775 {} +
sudo find /var/www -type f -exec chmod 0664 {} +
sudo echo -e " " >> /etc/profile
sudo echo -e "umask 0002" >> /etc/profile
# get Django
cd ~
wget https://www.djangoproject.com/download/1.4/tarball/
tar xzvf tarball
cd Django-1.4
sudo python setup.py install
echo -e " "
echo -e " "
echo -e "import django; print('Django Version ' + django.get_version())" | python
echo -e " "
echo -e " "
echo -e "If you didn't see a python error, type 'logout' and hit enter"
echo -e " "
echo -e " "


Code: Select all
#! /bin/sh
cd /var/www
django-admin.py startproject testsite
mv testsite django
cd django
python ./manage.py runfcgi host=127.0.0.1 port=8080
cd ~
sudo /etc/init.d/nginx restart
wget localhost
cat index.html
rm index.html
Posts: 15
Joined: Fri Mar 16, 2012 7:46 pm
by ceefan » Fri Jun 22, 2012 7:48 pm
[Couldn't edit or delete my earlier post but this is an update... now in the form of Github Gists that I can update.]

This script gets nginx and Django up and working. It's in two parts because you have to log out and back in first to make the user permissions stick.

It sets up /var/www with a static folder, adds the user pi to a www-pub group for editing in /var/www, and starts a Django project in /var/www/django

It's using information from the following pages: https://code.djangoproject.com/wiki/DjangoAndNginx and http://serverfault.com/questions/6895/w ... ata-in-var

By the way, this is how to get the FastCGI running in the background as a daemon: https://code.djangoproject.com/wiki/InitdScriptForLinux (I haven't gotten this part to work yet)

Note: You're going to have to tweak your nginx settings depending on how you want your server set up, this just gets it up and started with the bare minimum setup. See http://wiki.nginx.org/DjangoFastCGI and http://stackoverflow.com/questions/6051 ... for-django

To make it easier on yourself, I'd put these into a text file like part1.sh and part2.sh, and run them as "sudo sh part1.sh" and "sudo sh part2.sh"

Let me know if you run into any issues. I tried this on a brand new Wheezy install, by the way.

(Fixed the FastCGI parameters that get passed.)

Part 1: https://gist.github.com/2974698
Part 2: https://gist.github.com/2974700
Posts: 15
Joined: Fri Mar 16, 2012 7:46 pm
by raspberryfish » Sat Oct 27, 2012 3:43 pm
Iam getting "502 Bad Gateway"
Whats the matter?

Thanks
Peter
Posts: 14
Joined: Wed May 23, 2012 6:13 pm
by armandas » Tue Dec 24, 2013 12:11 pm
It most likely means that the FCGI process is not running or nginx is not configured to look in the right place. There are some more details here:
https://docs.djangoproject.com/en/dev/h ... t/fastcgi/
https://code.djangoproject.com/wiki/DjangoAndNginx
Posts: 1
Joined: Tue Dec 24, 2013 12:06 pm