Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Run Flask app on Webhosting or Managed Server

profile picture
Author
Alexander Knerlein
Published
2024-11-06
Time to read
4 minutes reading time

Introduction

In this tutorial we will learn how to run a Python Flask app on Hetzner Webhosting or Hetzner Managed Server. Flask is a python web framework. By default it runs on WSGI (interface between webserver and application), but mod_wsgi is not available on the managed Apache server. Nevertheless, there are different ways to convert the WSGI to other compatible interfaces.

Prerequisites

Step 1 - Install dependencies

Step 1.1 - Install and enable virtualenv

pip3 install --break-system-packages virtualenv
mkdir /usr/home/holu/virtualenvs
python3 -m virtualenv /usr/home/holu/virtualenvs/example_com
. /usr/home/holu/virtualenvs/example_com/bin/activate

Step 1.2 - Install Flask

Install the framework Flask.

pip install flask

Step 1.3 - Install Flup (Optional, required for FastCGI)

Install the wsgi-to-fcgi flup server.

pip install flup

Step 2 - Create your Flask project

Create the project directory and your Flask app with the content below.

mkdir -p /usr/home/holu/flaskprojects/example_com
vim /usr/home/holu/flaskprojects/example_com/flaskapp.py

Hit i to switch to "insert mode" and add this:

from flask import Flask
app = Flask(__name__)

@app.route('/', methods=['GET'])
def itworks():
  html = """<html>
  <head>
  <title>It works!</title>
  </head>
  <body>
  <h1>It works!<h1>
  </body>
  </html>"""
  return html

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

Hit esc the switch back to "command mode" and enter :wq to save and exit.

Step 3 - Prepare document root of webserver

Create an empty website directory and change the document root in konsoleH.

mkdir -p /usr/home/holu/public_html/example_com

Step 3 Option 1 - FastCGI

Create .htaccess

Create .htaccess with the content below.

vim /usr/home/holu/public_html/example_com/.htaccess

Hit i to switch to "insert mode".

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ flaskapp.fcgi/$1 [QSA,L]
</IfModule>

Hit esc the switch back to "command mode" and enter :wq to save and exit.

Create the ".fcgi"-script

Create flaskapp.fcgi with the content below.

vim /usr/home/holu/public_html/example_com/flaskapp.fcgi
#!/usr/home/holu/virtualenvs/example_com/bin/python

import sys
from flup.server.fcgi import WSGIServer
from flaskapp import app

sys.path.append('/usr/home/holu/flaskprojects/example_com')

class ScriptNameStripper(object):
  def __init__(self, app):
    self.app = app

  def __call__(self, environ, start_response):
    environ['SCRIPT_NAME'] = ''
    return self.app(environ, start_response)

app = ScriptNameStripper(app)

WSGIServer(app).run()

Set the executable bit for the owner.

chmod 744 /usr/home/holu/public_html/example_com/flaskapp.fcgi

Step 3 Option 2 - CGI

Create .htaccess

Create .htaccess with the content below.

vim /usr/home/holu/public_html/example_com/.htaccess

Hit i to switch to "insert mode".

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ flaskapp.cgi/$1 [QSA,L]
</IfModule>

Hit esc the switch back to "command mode" and enter :wq to save and exit.

Create ".cgi"-script

Create flaskapp.cgi with the content below.

vim /usr/home/holu/public_html/example_com/flaskapp.cgi
#!/usr/home/holu/virtualenvs/example_com/bin/python

import sys
from wsgiref.handlers import CGIHandler
from flaskapp import app

sys.path.append('/usr/home/holu/flaskprojects/example_com')

class ScriptNameStripper(object):
  def __init__(self, app):
    self.app = app

  def __call__(self, environ, start_response):
    environ['SCRIPT_NAME'] = ''
    return self.app(environ, start_response)

app = ScriptNameStripper(app)

CGIHandler().run(app)

Set the executable bit for the owner.

chmod 744 /usr/home/holu/public_html/example_com/flaskapp.cgi

Step 4 - Test

Test it by visiting your domain. You should see something like on the screenshot below.

Flask test

Conclusion

Now you can deploy your Flask apps on the managed OS.

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue

Discover our

Managed Servers

Focus on your projects. We'll take care of the configuration and updates.

Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more