Running Flask As A Linux Service (Linux / Python)

Continuing on with the Software Engineering & Agile module of my degree, I need to be able to show my Flask web application running. After picking a cloud platform (in this case Azure) I git cloned my repository to the Linux Ubuntu instance that will be running it and realised that I’ve not included a method to run the Flask application in a live / production environment.

The server that comes with Flask is meant for development / testing purposes, so I decided to use Gunicorn and set up a Linux system service (via Systemd) to auto start the web application.

I created a file called assets.service within /etc/systemd/system and inside that file put the following details:

assets.service file

The [Service] sections asks the service to run as azureuser (default user for Azure Linux instance), use the cloned repository as the working directory and then start Gunicorn from the Python Virtual Environment. assets:app is my Flask web application which I will post more about once the Software Engineering module is complete.

The -b is short for –bind, and the -w is short for –workers.

Once the file has been created systemctl needs to be reloaded (so it can see the file) and the service needs to be started, this can be accomplished using the commands:

sudo systemctl daemon-reload
sudo systemctl start assets.service

And the service can be checked on using the command:

sudo systemctl status assets.service