Raspberry Pi

Raspberry Pi Full Stack: Replacing Upstart with systemd for autostarting uwsgi 

 February 14, 2018

By  Peter

In the course Raspberry Pi Full Stack, which was published in 2015, I opted for Minibian instead of Raspbian. At the time, Minibian was the best option for a minimal headless operating system, that was still based on Debian (as is Raspbian).

One of the components of the web application that we build in this course is the uwsgi application server. In order to automatically start uwsgi when the Raspberry Pi boots, I used upstart, another popular and well tested choice at the time.

A few years later, the Raspberry Pi release a “Lite” version of Raspbian. This is now very similar in terms of RAM and disk footprint to Minibian. As a result, there is not a good reason to opt for Minibian instead of Rasbian Lite. At the time I am writing this, Raspbian Stretch Lite is available.

In this article, I provide a script so that students can use systemd instead of upstart to autostart uwsgi.

I think that the process is probably easier than what you had to do with upstart, so this switch is an additional bonus.

Here we go…

At this point, I assume that you have Raspbian Stretch Lite, you are logged on as root, and you have already installed and configured uwsgi to control your Flask application. Continue from this point:

1. Continue from the application directory /var/www/lab_app, and as root.
2. Create the systemd configuration file: (lab_app) [email protected]:/var/www/lab_app# vim /etc/systemd/system/emperor.uwsgi.service
Add this content in this file:

Description=uWSGI Emperor
ExecStart=/var/www/lab_app/bin/uwsgi –ini /var/www/lab_app/lab_app_uwsgi.ini
# Requires systemd version 211 or newer

3. Start the service: (lab_app) [email protected]:/var/www/lab_app# systemctl start emperor.uwsgi.service
4. Check the service status: (lab_app) [email protected]:/var/www/lab_app# systemctl status emperor.uwsgi.service

You should get this response

emperor.uwsgi.service – uWSGI Emperor
Loaded: loaded (/etc/systemd/system/emperor.uwsgi.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2018-01-31 06:04:51 UTC; 1min 21s ago
Main PID: 22250 (uwsgi)
Status: “uWSGI is ready”
CGroup: /system.slice/emperor.uwsgi.service
└─22250 /var/www/lab_app/bin/uwsgi –ini /var/www/lab_app/lab_app_uwsgi.ini
Jan 31 06:04:49 RPIFS-v2 systemd[1]: Starting uWSGI Emperor…
Jan 31 06:04:49 RPIFS-v2 uwsgi[22250]: [uWSGI] getting INI configuration from /var/www/lab_app/lab_app_uwsgi.ini
Jan 31 06:04:51 RPIFS-v2 systemd[1]: Started uWSGI Emperor.

5. Check in the browser: (replace with the actual IP address of your Raspberry Pi)

You should see “Hello World”

6. At the moment, you have only started the uwsgi service but not set it to start automatically when the RPi restarts. To do this, do this:
[email protected]:/home/pi# systemctl enable emperor.uwsgi.service

You should see this response:

Created symlink /etc/systemd/system/multi-user.target.wants/emperor.uwsgi.service → /etc/systemd/system/emperor.uwsgi.service.

7. Restart the RPi and check with the browser.: [email protected]:/var/www/lab_app# reboot
8. Wait for a minute. Then refresh your browser. Can you still see “Hello World”?

If the page loads succesfuly, then congradulations, you have setup systemd to automatically start uwsgi at bootup!


Pi, Raspberry, Systemd, Upstart, uWSGI

You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

In this Maker Mind Meld Summit presentation, Jordan Christman explains how FPGAs work, and how you can use them in your projects alongside a microcontroller and microprocessor. FPGAs is a fascinating topic because it entails

Read More
Jordan Christman – Getting Started with FPGAs

In this Maker Mind Meld summit presentation, Dal Gemmell talks about a new generation of wireless networks, based on cutting-edge blockchain technology, that delivers a new kind of open, decentralized wireless network built by individuals

Read More
Dal Gemmell – A blockchain-powered decentralized network owned by you