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) root@RPIFS-v2:/var/www/lab_app# vim /etc/systemd/system/emperor.uwsgi.service
Add this content in this file:
[Unit]
Description=uWSGI Emperor
After=syslog.target
[Service]
ExecStart=/var/www/lab_app/bin/uwsgi –ini /var/www/lab_app/lab_app_uwsgi.ini
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
3. Start the service: (lab_app) root@RPIFS-v2:/var/www/lab_app# systemctl start emperor.uwsgi.service
4. Check the service status: (lab_app) root@RPIFS-v2:/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: http://192.168.111.42/ (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:
root@RPIFS-v2:/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.: root@raspberrypi:/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!