-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ New tutorial - "Run Django app on Webhosting or Managed Server" (#971)
* add django www ms tutorial * fix formatting * Spelling and formatting updates --------- Co-authored-by: svenja.michal <[email protected]>
- Loading branch information
1 parent
e897332
commit 2a78157
Showing
2 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
235 changes: 235 additions & 0 deletions
235
tutorials/run-django-app-on-webhosting-or-managed-server/01.en.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
--- | ||
SPDX-License-Identifier: MIT | ||
path: "/tutorials/run-django-app-on-webhosting-or-managed-server" | ||
slug: "run-django-app-on-webhosting-or-managed-server" | ||
date: "2024-11-06" | ||
title: "Run Django app on Webhosting or Managed Server" | ||
short_description: "In this tutorial we will learn how to run a Python Django app on Webhosting or Managed Server" | ||
tags: ["Managed Server", "Webhosting", "Django"] | ||
author: "Alexander Knerlein" | ||
author_link: "https://github.com/alexanderknerlein" | ||
author_img: "https://avatars0.githubusercontent.com/u/48771568" | ||
author_description: "" | ||
language: "en" | ||
available_languages: ["en"] | ||
header_img: "header-7" | ||
cta: "managed" | ||
--- | ||
|
||
## 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](https://www.hetzner.com/webhosting?country=ot) with SSH support (>= Level 9) or [Managed Server](https://www.hetzner.com/managed-server?country=ot) with enabled SSH access | ||
|
||
## Step 1 - Install dependencies | ||
|
||
### Step 1.1 - Install and enable virtualenv | ||
|
||
```bash | ||
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. | ||
|
||
```bash | ||
pip install django | ||
``` | ||
|
||
### Step 1.3 - Install Flup (Optional, required for FastCGI) | ||
|
||
Install the wsgi-to-fcgi flup server. | ||
|
||
```bash | ||
pip install flup | ||
``` | ||
|
||
## Step 2 - Create and configure your Django project | ||
|
||
### Step 2.1 - Start project | ||
|
||
Create the project directory and start the project. | ||
|
||
```bash | ||
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. | ||
|
||
```bash | ||
vim /home/holu/djangoprojects/example_com/example_com/settings.py | ||
``` | ||
|
||
Hit `i` to switch to "insert mode" and add all requesting domains: | ||
|
||
```python | ||
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. | ||
|
||
```bash | ||
mkdir -p /usr/home/holu/public_html/example_com | ||
``` | ||
|
||
### Step 3 Option 1 - FastCGI | ||
|
||
#### Create .htaccess | ||
|
||
Create `.htaccess` with the content below. | ||
|
||
```bash | ||
vim /usr/home/holu/public_html/example_com/.htaccess | ||
``` | ||
|
||
Hit `i` to switch to "insert mode". | ||
|
||
```apacheconf | ||
<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. | ||
|
||
```bash | ||
vim /usr/home/holu/public_html/example_com/djangoapp.fcgi | ||
``` | ||
|
||
```python | ||
#!/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. | ||
|
||
```bash | ||
chmod 744 /usr/home/holu/public_html/example_com/djangoapp.fcgi | ||
``` | ||
|
||
### Step 3 Option 2 - CGI | ||
|
||
#### Create .htaccess | ||
|
||
Create `.htaccess` with the content below. | ||
|
||
```bash | ||
vim /usr/home/holu/public_html/example_com/.htaccess | ||
``` | ||
|
||
Hit `i` to switch to "insert mode". | ||
|
||
```apacheconf | ||
<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. | ||
|
||
```bash | ||
vim /usr/home/holu/public_html/example_com/djangoapp.cgi | ||
``` | ||
|
||
```python | ||
#!/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. | ||
|
||
```bash | ||
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. | ||
|
||
![Django test](images/djangotest.png) | ||
|
||
## Conclusion | ||
|
||
Now you can deploy your Django apps on the managed OS. | ||
|
||
- [Django documentation](https://docs.djangoproject.com) | ||
|
||
##### License: MIT | ||
|
||
<!-- | ||
Contributor's Certificate of Origin | ||
By making a contribution to this project, I certify that: | ||
(a) The contribution was created in whole or in part by me and I have | ||
the right to submit it under the license indicated in the file; or | ||
(b) The contribution is based upon previous work that, to the best of my | ||
knowledge, is covered under an appropriate license and I have the | ||
right under that license to submit that work with modifications, | ||
whether created in whole or in part by me, under the same license | ||
(unless I am permitted to submit under a different license), as | ||
indicated in the file; or | ||
(c) The contribution was provided directly to me by some other person | ||
who certified (a), (b) or (c) and I have not modified it. | ||
(d) I understand and agree that this project and the contribution are | ||
public and that a record of the contribution (including all personal | ||
information I submit with it, including my sign-off) is maintained | ||
indefinitely and may be redistributed consistent with this project | ||
or the license(s) involved. | ||
Signed-off-by: [Alexander Knerlein [email protected]] | ||
--> |
Binary file added
BIN
+38.8 KB
tutorials/run-django-app-on-webhosting-or-managed-server/images/djangotest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.