How to Deploy the Flask app as Ubuntu service

Manivannan Murugavel
2 min readApr 27, 2018

--

This is not using Apache or Nginx.

Step 1:
Create the Flask app and i mentioned the sample code below.
vi server.py(my server.py location is my home directory eg:/home/manivannan/server.py)

#!/usr/bin/python
from flask import Flask, request
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
return "Hai Manivannan this is ubuntu service with flask app"
if __name__ == '__main__':
app.run(host="0.0.0.0", port=9006)

Step 2:
We have to write the following service configuration in /etc/init/flask.conf:
sudo vi /etc/init/flask.conf

description "flask"
start on stopped rc RUNLEVEL=[2345]
respawn
exec python /home/manivannan/server.py

Step 3:
sudo service flask start
If you receive any error, you may have to create an additional file /lib/systemd/system/flask.service
sudo vi /lib/systemd/system/flask.service
Note: User=manivannan is the username

[Unit]
Description=Flask web server
[Install]
WantedBy=multi-user.target
[Service]
User=manivannan
PermissionsStartOnly=true
ExecStart=/home/manivannan/server.py
TimeoutSec=600
Restart=on-failure
RuntimeDirectoryMode=755

Step 4: Change the permission to server.py( Note: manivannan is username)

chown manivannan server.py 
chmod +x server.py

Step 5: Run the service and check the status
sudo service flask start
sudo service flask status

http://0.0.0.0:9006 is the service ip and port.

Final Step:
Check the service running or not
Please hit the localhost:9006 on your browser.

--

--

Manivannan Murugavel
Manivannan Murugavel

Written by Manivannan Murugavel

Artificial Intelligence and Data Science

Responses (4)