You are on page 1of 5

Setting up Upstart on RPi using Jessie Minibian

Written by Peter Dalmaris as a resource for students of the Raspberry Pi: Full Stack course

This setup guide covers three scenarios:


1. Running your application using Python 2.7.9 in a virtual environment
2. Running your application using the system Python 2.7.9 (no virtual environment)
3. Running your application using Python 3.4.2 in a virtual environment. This is experimental
as the Adafruit DHT library is not updated for Python 3.

I m p or t a n t

If you are using the Raspberry Pi 3, you will need to use MINIBIAN 2016-03-12, which works
without any issues (tested with Python 2.7)

Instructions
1. Download the image from: https://minibianpi.wordpress.com/download/
2. Extract the contents
Note: The exact way of doing it, depends on your OS.
3. Get the location of the SD card using the Disk Utility.
Note: depending on your OS, you need to find out the SD card device on your file system.
4. Unmount the SD card
Note: Again, this depends on your OS.
5. Go to the terminal
6. Write the image to the SD card:
sudo dd bs=1m if=path_of_your_image.img of=/dev/rdisk4
Note: Again, this depends on your OS.
7. Insert the SD card to the RPi, connect it to Ethernet and Power
8. Use SSH to connect to the RPi: ssh root@192.168.111.63
Use password: raspberry
9. Update: apt-get update
10. Upgrade: apt-get upgrade
11. Install Raspiconfig: apt-get install raspi-config
12. Use Raspiconfig to expand the file system: raspi-config
Note: Reboot and reconnect
13. Install prerequisites:
apt-get install build-essential
apt-get install libncurses5-dev libncursesw5-dev libreadline6-dev
apt-get install libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
14. Install Python dev tools: apt-get install python-dev
15. Install SSL/TLS support for pip: apt-get install libssl-dev openssl
16. Install vim: apt-get install vim

Which Python?
From here on choose to install either Python 3 or Python 2. The later works with all Python
scripts in the course. Python 3 is new and as far as software for the RPi is concerned,
experimental, with lots of bugs.
2
If you choose to proceed with Python 2, you can either use the system Python 2.7.9
interpreter, or setup a virtual environment for the application only with its own Python 2.7.9
interpreter. The benefit of using the virtual environment is that the application is self
contained, not dependent on the system Python, and can be changed/upgraded without
affecting the system.

I m p or t a n t

There seem to be problems with Python 3 and various libraries needed for the RPi. So, unless
you are a Python expert I recommend Python 2 until these issues are resolved. Minibian
Jessie comes with Python 2.7.9, which is what we'll use here.

Proceeding with Python 2


Note: If you are going to use Python 3 instead, skip to step #18
17. Test your new Python: python version
The response should be: Python 2.7.9
Note: If you are going to use Python 2, skip to step #24

Proceeding with Python 3


18. We'll install Python 3.4. First download it:
wget https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz
Create a new directory for the source code. I downloaded in: /var/python-source
19. Extract the archive: tar zxvf Python-3.4.2.tgz
20. Go in the Python source directory and start the installation process:
./configure --prefix=/usr/local/opt/python-3.4.2
21. Make: make
22. Install: make install
Note: Python 3.4.2 is now installed in /usr/local/opt/python-3.4.2
23. Test your new Python: /usr/local/opt/python-3.4.2/bin/python3.4 version
The response should be: Python 3.4.2
Note: If you dont plan to use the virtual environment skip to step #24

Install the virtual environment


Python 2 Python 3
Note: If you have already created a venv using a. Create a virtual environment for the app:
Python 3, just delete or rename the venv /usr/local/opt/python-
directory and continue from here to install a 3.4.2/bin/pyvenv-3.4
Python 2 venv.
a. Install the Python virtual environment: proceed with step c, here below:
apt-get install python-
virtualenv
b. Create a virtual environment for the app:
virtualenv venv
Note: Setup a virtual environment in the
application folder, using the 2.7 interpreter.

c. With either Python 2 or Python 3 you should activate venv:


source venv/bin/activate
Note: You should see "venv" at the start of the command line prompt.

Setting up Upstart on RPi using Jessie Minibian | Peter Dalmaris


3
24. Install nginx: apt-get install nginx
25. Create the lab_app directory: mkdir /var/www/lab_app
26. Go in the lab_app directory: cd /var/www/lab_app
27. Install flask: pip install flask
Note: If pip is not installed (i.e. when using the system Python), install it with: apt-get install python-pip
28. Create a simple app: vim hello.py
29. Copy this in the hello.py file:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)

30. Run the app: python hello.py


31. Use the browser to confirm it is working: http://192.168.111.63:8080/
32. Install uwsgi: pip install uwsgi
33. Remove the nginx default site: rm /etc/nginx/sites-enabled/default
34. Create a new file in the lab_app directory named lab_app_nginx.conf:
vim lab_app_nginx.conf
35. Copy this new app site configuration:
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 75M;

location /static {
root /var/www/lab_app/;
}

location / { try_files $uri @labapp; }


location @labapp {
include uwsgi_params;
uwsgi_pass unix:/var/www/lab_app/lab_app_uwsgi.sock;
}
}
36. Link this file to the nginx configuration directory with a symbolic link:
ln -s /var/www/lab_app/lab_app_nginx.conf /etc/nginx/conf.d/
37. Restart nginx: /etc/init.d/nginx restart
38. Create the uwsgi configuration file: vim lab_app_uwsgi.ini
with the following content:

content of the uwsgi file


System Python Python 3
[uwsgi] [uwsgi]
#application's base folder #application's base folder
base = /var/www/lab_app base = /var/www/lab_app
chdir=/var/www/lab_app
#python module to import
#python module to import app = hello
app = hello module = %(app)
module = %(app)
home = %(base)/venv

Setting up Upstart on RPi using Jessie Minibian | Peter Dalmaris


4
pythonpath = %(base)
pythonpath =
/usr/lib/python2.7:/usr/lib/python2.7/ #socket file's location
plat-arm-linux- socket = /var/www/lab_app/%n.sock
gnueabihf:/usr/lib/python2.7/lib-
tk:/usr/lib/python2.7/lib- #permissions for the socket file
old:/usr/lib/python2.7/lib- chmod-socket = 666
dynload:/usr/local/lib/python2.7/dist-
packages:/usr/lib/python2.7/dist- #the variable that holds a flask
packages:/usr/lib/pymodules/python2.7 #application inside the module
#imported at line #6
#socket file's location callable = app
socket = /var/www/lab_app/%n.sock
#location of log files
#permissions for the socket file logto = /var/log/uwsgi/%n.log
chmod-socket = 666

#the variable that holds a flask


#application inside the module
#imported at line #6
callable = app

#location of log files


logto = /var/log/uwsgi/%n.log

If using the system Python, to find out your pythonpath, use:


python -c "import sys; print ':'.join(x for x in sys.path if x)"

39. Create the log directory for uwsgi: mkdir -p /var/log/uwsgi


40. Start uwsgi manually and visit http://192.168.111.63 to verify the web app is working:
uwsgi --ini /var/www/lab_app/lab_app_uwsgi.ini
41. Install upstart: apt-get install upstart
42. Reboot: reboot
43. Connect: ssh root@192.168.111.63
44. Create the upstart configuration file: vim /etc/init/uwsgi.conf
with the following content:

content of the upstart configuration file


System Python Python 3
#Full path: /etc/init/uwsgi.conf #Full path: /etc/init/uwsgi.conf
#The upstart script for uWSGI Emperor #The upstart script for uWSGI Emperor
description "uWSGI" description "uWSGI"
start on runlevel [2345] start on runlevel [2345]
stop on runlevel [06] stop on runlevel [06]
respawn respawn

env UWSGI=/usr/local/bin/uwsgi env UWSGI=/var/www/lab_app/venv/bin/uwsgi


env LOGTO=/var/log/uwsgi/emperor.log env LOGTO=/var/log/uwsgi/emperor.log

exec $UWSGI --master --emperor exec $UWSGI --master --emperor


/etc/uwsgi/vassals --die-on-term -- /etc/uwsgi/vassals --die-on-term --uid
uid root --gid root --logto $LOGTO root --gid root --logto $LOGTO

45. Link the upstart configuration file to the vassals directory:


ln -s /var/www/lab_app/lab_app_uwsgi.ini /etc/uwsgi/vassals/
46. Start uwsgi: start uwsgi
Note: You can stop uwsgi with stop uwsgi, and restart it with restart uwsgi

Setting up Upstart on RPi using Jessie Minibian | Peter Dalmaris


5
47. Verify in the browser: http://192.168.111.63/
48. Sanity test: restart the RPi and use the browser to test that uwsgi is still running (thanks
to upstart)
49. Reboot: reboot
50. Check: http://192.168.111.63/
Note: uwsgi and the application should be working after a reboot
51. Install rpi.GPIO: pip install rpi.gpio
Note: This assumes that you are logged on to your RPi, in the app directory (/var/www/lab_app) and have
activated the virtual environment, unless you are not using one . venv/bin/activate"
This works for both Python 3 and Python 2.
52. Try out the GPIO module. Create a file for the blinking LED script: vim blink.py
with the following content:
import RPi.GPIO as GPIO ## Import GPIO Library
import time ## Import 'time' library (for
'sleep')
pin = 7 ## We're working with pin 7
GPIO.setmode(GPIO.BOARD) ## Use BOARD pin numbering
GPIO.setup(pin, GPIO.OUT) ## Set pin 7 to OUTPUT

for i in range (0, 20): ## Repeat 20 times


GPIO.output(pin, GPIO.HIGH) ## Turn on GPIO pin (HIGH)
time.sleep(1) ## Wait 1 second
GPIO.output(pin, GPIO.LOW) ## Turn off GPIO pin (LOW)
time.sleep(1) ## Wait 1 second
GPIO.cleanup()

53. Try it out: python blink.py


The LED (hopefully already connected to pin 7 (+) and 6 (GND) will blink 20 times.
54. Install sqlite3: apt-get install sqlite3
Although not fully tested, other stack components, like the sqlite3 database, are installed
as per the current instructions in the course without apparent problems.
Note: This is only tested with Python 2.
55. Install git: apt-get install git-core
56. Setup git user name: git config --global user.name "Peter"
57. Setup git email address: git config --global user.email peter@txplore.com
58. We'll try out the DHT library from Adafruit. Let's download it:
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
Note: This assumes that you are in the /var/www/lab_app directory
59. Install the library: cd Adafruit_Python_DHT/; python setup.py install
Note: Move into the Adafruit directory before you run the setup.py script.
60. Run the example script:
cd examples;python AdafruitDHT.py 2302 17
This assumes that you have activated the virtual environment, unless you did the setup
using system python. Either way, you should see something like Temp=27.2*
Humidity=62.4%
61. If you are using RPi 3, you will need to enable Wifi and Bluetooth:
apt-get install firmware-brcm80211 pi-bluetooth wpasupplicant
Note: The RPi 3 contains hardware that was not available in previous models, hence this extra step.
62. Restart to make the changes effective: reboot

Setting up Upstart on RPi using Jessie Minibian | Peter Dalmaris

You might also like