Preparing your Raspberry Pi Environment

Installing Flask, uWSGI, and Python

This is the procedure that I went through to get my Raspberry Pi ready to be a home automation server. This is a quick and dirty setup guide to get everything up and going. Feel free to copy and paste these commands into your Raspberry Pi to expedite the process. This is written for the June 2014 release of Raspbian with all the packages updated to 8-1-2014. I’m sure this tutorial will need updated from time to time to account for software updates. If the instructions don’t work for you, leave me comment. I will try to help you and your feedback will help me keep this post updated. Thanks 😉

If you would like to learn more about the process of getting this going, I found this website extremely useful. I encourage you to check it out. This site goes into much more detail in the process of getting Flask, uWSGI and Nginx up and going. http://vladikk.com/2013/09/12/serving-flask-with-nginx-on-ubuntu/

 Step 1: Install Raspbian

Unless otherwise indicated, all of the projects on this site use Raspbian. The tutorials and instructions are written for it. To be in the best possible place to start, download and install the latest version of Raspbian from http://www.raspberrypi.org/downloads/. Follow the instructions in their image installation guide.

Step 2: Install the software

raspberrypi# sudo -i
raspberrypi# apt-get update
raspberrypi# apt-get upgrade
raspberrypi# apt-get install python-pip uwsgi uwsgi-plugin-python nginx
raspberrypi# pip install flask

This should get all your software in place, now we have to edit a couple of configurations.

Step 3: Configure the Software

We have to manually edit a couple of config files in order to get everything working together nicely.

Let’s start with the Nginx. In case you were wondering, Nginx is a light weight web server. It is a program that will serve your webpages.

vi /etc/nginx/sites-available/default

Starting at the ‘location /’ line, change the lines to read this:

 location / { try_files $uri @yourapplication; }
 location @yourapplication {
                include uwsgi_params;
                uwsgi_pass unix:///tmp/uwsgi.sock;
        }

Now edit the uWSGI configuration. This will be a new empty file.

raspberrypi# vi /etc/uwsgi/apps-available/hello.ini

Just paste the following lines.

[uwsgi]
socket = /tmp/uwsgi.sock
plugins    = python
wsgi-file = /var/www/hello.py
callable = app
process   = 3

Create the symbolic link in /etc/uwsgi/apps-enabled to tell the server that you want this to start at startup.

raspberrypi# cd /etc/uwsgi/apps-enabled
raspberrypi# ln -s ../apps-available/hello.ini hello.ini

This tells us uSWGI to start our yet unwritten python script at startup.

Now lets write our test python script. First, create a directory to store our python script and open a new file for writing our script.

raspberrypi# mkdir /var/www
raspberrypi# vi /var/www/hello.py

Paste these lines into that new file.

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

Restart uWSGI and Nginx so you and try it out.

raspberrypi# service nginx restart
raspberrypi# service uwsgi restart

Try to access it through your web browser. If you were successful, it will look like this:

success

 

Congratulations! You are ready to get started programming your Pi. Close

About

I’m just your everyday average home automation hobbyist. I’ve been into technology since I was a little guy and I am currently an IT director at a small community college in rural Oklahoma. While working and playing I’ve had a great deal of fun getting to play with a very wide variety of technology, and I love to play with technology!

Posted in Operating System Tagged with:

Social