Saas! Software as a service on Linux OS.

Narendra Harny
Make Android
Published in
4 min readAug 1, 2021

Start the application when the Linux system boots up and keep it running on the Linux machine.

Photo by Arian Darvishi on Unsplash

You will be required to Learn!

  1. how to create a simple web application using Flask.
  2. how to create a Linux Service.
  3. how to run a web application with Linux system boot and keep running with it.
  4. How to restart the web application service again with system boots up
  5. How to set a python virtual environment for an individual web application.
  6. Linux systemctl basics to start, stop, enable, disable and check the status of the running service on Linux os.

First of all, I would like to divide the task to implement this feature.

  1. Design a simplest sample web app, batter go with just a hello world! web app.
  2. Create a Linux Service that hosts the web application.
  3. A bit we need python virtual environment knowledge so we will you that.
  4. Learn some basic systemctl stuff to play with created Linux service.

Design a simple Web Application

So simple! take this code and run on your Linux machine

  1. Create a dir e.g. /home/<username>/mywebapp and /home/<username>/mywebapp/templates

2. Create app.py at location /home/<username>/mywebapp and index.html at location /home/<username>/mywebapp/templates

app.py

#!/bin/bash

from flask import Flask, render_template

app = Flask(__name__)
app.debug = True


@app.route('/')
def hello():
return render_template('index.html')


if __name__ == '__main__':
app.run()

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cypher Segment</title>
</head>
<body style="background-color:blue;">
<font color="white">
<h1>Cypher Segment</h1>
<h1>$>..</h1>
<h1>$>..</h1>
<h1>$>..</h1>
<h1>$>..</h1>
</font>
</body>
</html>

3. Run the application with python app.py and check over the location host if it is running.

The application will run on the URL> http://localhost:5000/

Note: You may encounter an error while running the application.

Traceback (most recent call last):
File "app.py", line 1, in <module>
from flask import Flask
ModuleNotFoundError: No module named 'flask'

Install Flask module on your python virtual environment! Set up the Web app venv first and install the module with that.

Setup virtual environment for web application

  1. Install virtualenv if you don’t have
python3 -m pip install --user virtualenv

set-up virtualenv! and install the Flask module.

export PYTHONVENVWRAPPER_PYTHON=/usr/bin/python3.7
python3 -m venv env
source env/bin/activate
pip install Flask

Check again! Is the web application is successfully running?

Create a Linux service

  1. Create a app.service file at location on your Linux machine > /etc/systemd/system

app.service

[Service]
ExecStart=<path to python virtual env> <path to webapp [app.py]>
[Install]
WantedBy=multi-user.target

<path to python virtual env> is your python virtualenv path e.g. >

/home/<user name>/mywebapp/venv/bin/python

<path to webapp [app.py]> is your web app app.py file path e.g. /home/<user name>/mywebapp/app.py

Few concepts to learn about Linux service creation and running the web app as Linux Service.

The above code segment of the Linux app.service is enough to run the Web application as a Linux service.

So, Let's check the implementation that our web application is running as a Linux service to not.

  1. Load the service as part of Linux os

systemctl daemon-reload

  1. Start the web app as Linux Service.

systemctl start app

Output: None

2. Check the status of the service.

systemctl status app

OutPut: Active: active (running)

Active: active (running) since Sat 2021-06-19 19:58:42 IST; 9s ago
Main PID: 11812 (python)
Tasks: 3 (limit: 4915)
CGroup: /system.slice/app.service
├─11812
...........
...........
...........
Jun 19 19:58:42 username systemd[1]: Started app.service.

3. Check again! on http://localhost:5000/, you will be seeing the web application is running as we have run it before directly on localhost.

Now time to check if the web app still runs after rebooting the Linux system so just reboot the os and check again.

For me! :(

No worries now we will be adding the rest of the code in the app.service file which makes it always running with the Linux system as soon as it boots up.

app.service

[Unit]
Description=This is my web application
[Service]
ExecStart=<path to python virtual env> <path to webapp [app.py]>
[Install]
WantedBy=multi-user.target

If you have something to execute before or after when service starts.

ExecStart=<path to python virtual env> <path to webapp [app.py]>
ExecStartPre=/path of execution file/filename
ExecStartPost=/path of execution file/filename

Make the required changes and rerun the systemctl commands

systemctl daemon-reload
systemctl start app
systemctl status app

If you want to automatically restart the application

systemctl enable app

If you want to disable the server for not to start at bootup

systemctl disable app

You will get the service status active again and sorry to say that but reboot the os and check the service still runs after rebooting.

Thank You!!!

--

--

Narendra Harny
Make Android

Connect on https://medium.com/make-android | Write On, Android AOSP & Applications | Python | DevOps | Java | C++ | Kotlin | Shell | Linux | Android Auto | IVI