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
- Webhosting with SSH support (>= Level 9) or Managed Server with enabled SSH access
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.
Conclusion
Now you can deploy your Flask apps on the managed OS.