Introduction
In this tutorial we will learn how to run a Python Django app on Hetzner Webhosting or Hetzner Managed Server. Django 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 Django
Install the framework Django.
pip install django
Step 1.3 - Install Flup (Optional, required for FastCGI)
Install the wsgi-to-fcgi flup server.
pip install flup
Step 2 - Create and configure your Django project
Step 2.1 - Start project
Create the project directory and start the project.
mkdir /usr/home/holu/djangoprojects
env -C "/usr/home/holu/djangoprojects" django-admin startproject example_com
Step 2.2 - Configure project
Add all requesting domains to the ALLOWED_HOSTS
variable to allow the access.
vim /home/holu/djangoprojects/example_com/example_com/settings.py
Hit i
to switch to "insert mode" and add all requesting domains:
ALLOWED_HOSTS = ['example.com']
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 ^(.*)$ djangoapp.fcgi/$1 [QSA,L]
</IfModule>
Hit esc
the switch back to "command mode" and enter :wq
to save and exit.
Create ".fcgi"-script
Create djangoapp.fcgi
with the content below.
vim /usr/home/holu/public_html/example_com/djangoapp.fcgi
#!/usr/home/holu/virtualenvs/example_com/bin/python
import sys
import os
import django
from flup.server.fcgi import WSGIServer
from django.core.handlers.wsgi import WSGIHandler
sys.path.append("/usr/home/holu/djangoprojects/example_com")
os.environ['DJANGO_SETTINGS_MODULE']="example_com.settings"
django.setup(set_prefix=False)
WSGIServer(WSGIHandler()).run()
Set the executable bit for the owner.
chmod 744 /usr/home/holu/public_html/example_com/djangoapp.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 ^(.*)$ djangoapp.cgi/$1 [QSA,L]
</IfModule>
Hit esc
the switch back to "command mode" and enter :wq
to save and exit.
Create ".cgi"-script
Create djangoapp.cgi
with the content below.
vim /usr/home/holu/public_html/example_com/djangoapp.cgi
#!/usr/home/holu/virtualenvs/example_com/bin/python
import sys
import os
import django
import wsgiref.handlers
from django.core.handlers.wsgi import WSGIHandler
sys.path.append("/usr/home/holu/djangoprojects/example_com")
os.environ['DJANGO_SETTINGS_MODULE']="example_com.settings"
django.setup(set_prefix=False)
wsgiref.handlers.CGIHandler().run(WSGIHandler())
Set the executable bit for the owner.
chmod 744 /usr/home/holu/public_html/example_com/djangoapp.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 Django apps on the managed OS.