Friday 29 January 2016

Install Nginx with uWSGI and Django on Raspberry Pi


This tutorial is aimed at installing a lightweight web-server  on the Raspberry Pi with python support.
The required packages are:
1. django
2. uWSGI
3. NGINX
4. Python

Note: This installation is based on the assumption you already have a correct django project.




There are many tutorials out there but not one gave me a straight forward step to do it, so here are the steps i took, and replicated over again to make sure i got it right.
This tutorial assumes that you already have a working django project in place, so i won't put you through the steps for that.
Assumption is also made that you have a working pi distribution in place. This tutorial was used on Raspian Wheezy.
Python version used here with be 3.x

Step 1: Install Python
Though this is the first step but its optional depending on the python version used to write your django project. I use version 3.2.3 for mine. Python 2.6 & 2.7 are installed by default, to get version 3, run the following:


Step 2: Install Django
Using your web browser, browse to Django download site and download the latest release. The release at the time of this tutorial was Django-1.6.5. Using WinSCP, copy the django tarball to /home/pi.
run,

It would spew a lot of lines...just wait till it finishes.



Again....it will spew a lot of lines...waits till it finishes without errors.

Step 3: Download, Compile and Configure uWSGI
Download uWSGI and using WinSCP, copy the  uwsgi tarball to '/home/pi'. Current release at time of tutorial was uwsgi-2.0.5.1.






Next is to compile uwsgi.
However here is a shortcut; Im using Raspian Wheezy 3.12.20+(You can get the version using uname -a).
Here is the compiled file uwsgi(download).
I've actually transferred the file to another version of Raspian Wheezy and it worked with no issues.
I've also used this compiled version for future wheezy and it works ok.
Copy with WinSCP to '/home/pi/uwsgi-2.0.5.1/'



The above command is important...or else you won't be able to run uwsgi.
Next, copy uwsgi_params to '/path/to/your/django/mysite/'
Next is to create a uwsgi config file..
an example of the config file can be found here......
we'll use
----------------------------------------------------------
[uwsgi]
#socket = 127.0.0.1:8080
socket = /home/pi/Local_WISP/mysite.sock     #path to where mysite.sock file will be created
chdir = /home/pi/Local_WISP/                         #path to you django project main project
pythonpath = /usr/bin/python3                          #path to your installed python ver
env = DJANGO_SETTINGS_MODULE=Local_WISP.settings     #path to your django settings file
module = django.core.handlers.wsgi:WSGIHandler()
#master = true
processes = 4
threads = 2
stats = 127.0.0.1:9191
----------------------------------------------------------

Save the content within your django project--------- as startup.ini( e.g /home/pi/Local_WISP/startup.ini)
Here we use unix socket instead of TCP port ---- there's less overhead.
We have to ensure uWSGI runs at boot, so edit /etc/rc.local and add the below lines to it

/home/pi/uwsgi-2.0.5.1/uwsgi --ini /home/pi/Local_WISP/startup.ini --chmod-socket=666 &












Press Ctrl + X to save.


Step 4: Install and Configure NGINX
Run:


Next is to setup your django nginx configuration. You can edit /etc/nginx/nginx.conf but i prefer creating the configuration file in my django project folder. This way i can move my project to another server and easily configure it on another nginx server.
Save the below  code as wisp_nginx.conf in your django project folder
-----------------------------------------------------------------------------------------------------------------------
# the upstream component nginx needs to connect to
upstream django {
server unix://home/pi/Local_WISP/mysite.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# include /etc/nginx/sites-enabled/*;
# include /etc/nginx/conf.d/*.conf;
# configuration of the server
server {
# the port your site will be served on
listen      80;
# the domain name it will serve for
server_name raspberrypi; # substitute your machine's IP address or FQDN
charset     utf-8;

# max upload size
client_max_body_size 75M;   # adjust to taste
   # Django media
location /media {
# alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
}
location /static {
alias /home/pi/Local_WISP/templates/Media; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass  django;
include     /home/pi/Local_WISP/uwsgi_params; # the uwsgi_params file you installed
}
}

----------------------------------------------------------------------------------------------------------------------
Please note that this file is a working file that i'm using, all you have to do is to change the variable to fit your file folder layout.

Next is to ensure nginx can see your django project.
Ensure the server is shutdown: sudo service nginx stop
Next, you make a link of your django nginx configuration(wisp_nginx.conf) to /etc/nginx/sites-enabled/





You're done.
Final step is to restart your pi and when you do, the site will be available

No comments:

Post a Comment