Today I installed the Django web framework on my Raspberry Pi, using Nginx as the web server and fastcgi to delegate page requests to Django. Here is a summary of the steps involved, for future reference. I hope it's effective so please comment if anything is unclear.
1. Install Django and Nginx
I used Paul Hallett's one-click installer. When it finishes it reports the Django version (such as 1.4.1) if successful. Note that his script installs a local version of Python so bear in mind some packages may need to be symlinked to work.
2. Configure Nginx
There's a useful and informative article on using Nginx for personal websites on Ars Technica. Ignore the part to install the development version because there isn't a pre-built package for the ARM11 CPU used in the Raspberry Pi. Once installed (I'm using nginx version 1.2.1 from the Raspbian repository), you need to edit the file: /etc/nginx/nginx.conf
worker_processes 1;The Raspberry Pi has only a single CPU core so there's no need for more than one worker process. This file can also be tweaked for compressing large files, and to improve security (not covered here).
Create a new website corresponding to a URL such as www.mysite.org by copying the default file. Name it according to your URL.
cd /etc/nginx/sites-available/ cp default www ln -s /etc/nginx/sites-available/www /etc/nginx/sites-enabled/www rm /etc/nginx/sites-enabled/defaultThe last 2 commands enable your new site and disable the default one. Edit the new file as follows:
server { listen 80; #listen [::]:80 default_server ipv6only=on; root /var/html/www; index index.html index.htm; access_log /var/log/nginx/www.access.log; error_log /var/log/nginx/www.error.log; # Make site accessible from http://localhost/ server_name localhost; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # try_files $uri $uri/ /index.html; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules # Django # https://code.djangoproject.com/wiki/DjangoAndNginx include fastcgi_params; fastcgi_pass 127.0.0.1:8080; } }
The root tag is where your website files would normally be installed. However, your Django site can be placed anywhere. The access_log and error_log tags are useful for logging information. The important tags are include fastcgi_params and fastcgi_pass 127.0.0.1:8080 which tells nginx to forward page requests to Django on port 8080. See the Django and Nginx Wiki for further info. Restart nginx to load your new settings.
sudo /etc/init.d/nginx reload
3. Set up Django and fastcgi
Install Flup and ensure it's in your Python path (check your local configuration). Version 0.5 or later should be ok, I have 1.0 from the Raspbian repo.
sudo aptitude install python-flup sudo ln -s /usr/lib/python2.7/dist-packages/flup* \ ~/lib/python2.7/site-packages/
4. Create a test website
Following the Django tutorial, create your test site as follows.
django-admin.py startproject mysite cd mysite python ./manage.py runfcgi host=127.0.0.1 port=8080Your website is now running and if all goes well Nginx will forward requests to port 8080. Type in the IP address of your Raspberry Pi into a browser window on another machine, and you should see a welcome to Django message. That means it worked! :)