Prepare Raspberry Pi Environment - ThomasLamb/pi-alarm-clock GitHub Wiki

For this setup I'll be using:

  • Nginx as webserver
  • Flask as web framework
  • uWSGI the go between Flask and Nginx
  • Python as server-side language

Most of this content is from Preparing your Raspberry Pi Environment and Serving Flask with Nginx.

  1. Install Raspian on the Pi, expand drive, enable WiFi and SSH, change the password.
  2. Install software:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-pip uwsgi uwsgi-plugin-python nginx
sudo pip install flask
  1. Configure Nginx:
sudo nano /etc/nginx/sites-available/default

Find the line location / and change it to:

 location / { try_files $uri @pi-alarm-clock; }
 location @pi-alarm-clock {
                include uwsgi_params;
                uwsgi_pass unix:///tmp/uwsgi.sock;
        }
  1. Configure uWSGI:
sudo nano /etc/uwsgi/apps-available/pi-alarm-clock.ini

This should create a new file. In the file add the following lines:

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

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

cd /etc/uwsgi/apps-enabled
sudo ln -s ../apps-available/pi-alarm-clock.ini pi-alarm-clock.ini
  1. Add now the actual Python page:
sudo nano /var/www/pi-alarm-clock.py

And in the file add the following simple Hello World test:

from flask import Flask
app = Flask(__name__)

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

Then restart web services:

service nginx restart
service uwsgi restart

And finally browse to your web server on a web browser and (fingers crossed) it works! You should see a simple "Hello World!" message.