Configure your Web Server to Auto-Start at Boot


Description

Running a Web Server on your Pi is useful, but if you have to manually start the server each time you boot your Pi it becomes less useful. In the last project, we added routes to our server to allow our Pi to be rebooted or shutdown from the browser. In this project I’ll show you how to configure your Flask Web Server that we so that it will automatically start when the Pi is booted.

Parameters

This is some information about this project and the conditions under which it was done. If you try to replicate it in the future and it doesn’t work, you can evaluate these parameters to see if any changes between these and your configuration might have impacted your results. An example might be changes to future versions of Python or Flask.

  • Date: July 17, 2021
  • Skill: Beginner
  • Raspberry Pi Model(s): Zero-W, 3B+, 4B
  • OS: Raspberry Pi OS version 10 (Buster)
  • Python Version: 3.7.3
  • Flask Version: 1.0.2

Steps

  1. First, make sure that your server successfully starts manually. If it won’t start, then it will difficult to verify that the following steps are working correctly.
  1. Next, create a new file in Geany or your favorite editor. Enter the following lines:
[Unit]
Description=Pi Server
After=network.target

[Service]
ExecStart=/usr/bin/python3 -u sample.py
WorkingDirectory=/home/pi/sample
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Save the file to your Desktop and name it sample.service.

  1. Now we need to move it to the proper location. Open a Terminal and enter the following commands:
cd /home/pi/Desktop
sudo cp sample.service /etc/systemd/system/sample.service
cd /etc/systemd/system
ls

Assuming no errors along the way, after the last ls command you should see a listing of the files in the system directory. Hopefully our sample.service file is one of them. If not, double-check to make sure you’ve entered the commands above correctly.

  1. Now let’s start the server using the systemctl utility. First, make sure it’s not already running. If it is, you’ll need to stop it first. You can only have one server running on a given IP:port combination. Once you’re ready, enter the following commands in Terminal:
sudo systemctl start sample.service
systemctl status sample.service

You should now see a message in the Terminal that shows the status of your web server. The important line to look for is one that says active (running). You should now be able to test your server through the browser to validate that it is working correctly. If you still have debug=True set in your sample.py server code, any future changes you make will still cause the server to auto-restart once you save them. Again, once you get to a stable version, it is recommended that you change your code to debug=False, but once you do that your server will no longer auto-restart with code changes. Just keep that in mind.

Press Ctrl-C to exit the status window.

If you do need to restart the server, you can enter the following command:

sudo systemctl restart sample.service

To stop the server, you can enter this command:

sudo systemctl stop sample.service
  1. Okay, now we know how to start and stop the server, we need to enable auto-start. To do that, enter the following command in the Terminal:
sudo systemctl enable sample.service
systemctl status sample.service

Now in the status message, you should see Loaded: loaded (/etc/systemd/system/sample.service; enabled. This indicates that the server will auto-start at boot.

Press Ctrl-C to exit the status window.

To test, close all of your open files and reboot your Pi. (You can reboot it using your new Reboot route that we added previously!) Once it comes back up and is connected to the network, test your server through your browser to make sure it worked. Remember, you can check the status of the server by entering the command:

systemctl status sample.service
  1. If at some point you want to disable auto-start, you can enter this command in the Terminal:
sudo systemctl disable sample.server

This won’t stop the server, just disable auto-start. To stop it, enter the systemctl stop command from above.

  1. Another useful feature for monitoring your server is this command that you can enter in the terminal:
sudo tail -f /var/log/syslog

This will give you a running list of the tail end of the syslog file. You can also see the output from any print() statements in your server here.

Again, use Ctrl-C to exit the tail window.

Summary

This project will help make your Pi Server a lot easier to maintain. If you use your reboot route or you’re just starting up your Pi, you know the web server will be running. The challenge is still those times when you lose the network connection to your Pi and you can’t reboot it from your browser. In a future project I’ll show you how to add the ability for your Pi to self-monitor it’s network connection and reboot itself if needed. This, in conjunction with the auto-start of the server, will greatly increase the reliability and availability of your Pi Server.

Learn More

Raspberry Pi / Raspberry Pi OS

Python / HTML / CSS

Flask

Leave a Comment

Your email address will not be published. Required fields are marked *