Django hosting by ungleich¶
- Table of contents
- Django hosting by ungleich
Status¶
This document is IN PRODUCTION (since 2016-04-08).
Pricing can be found on the ungleich products page.
Version 2: Kubernetes Based (active as of 2023)¶
Deployment preparation¶
- Create a (docker) container that contains everything the django app needs to run
- The container should listen on port 9000
- This allows running django on port 8000 and potentially an nginx or similar inside for serving static files as well
- The container needs to read the database configuration from environment variables (see below)
Database configuration¶
The database configuration is passed into the container using environment variables as follows:
POSTGRES_USER POSTGRES_DB POSTGRES_PASSWORD POSTGRES_HOST
Additionally the following variable is passed in that needs to be interpolated (i.e. variables need to resolve):
DATABASE_URL = postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):5432/$(POSTGRES_DB)
Static (pre-compiled) files¶
If you want to serve static, pre-compiled files, add them into your container in /app/static.
All files stored in that directory will be exposed using an integrated nginx.
Data directory¶
If your application needs to store files to persistent storage, it should be stored below /data. Usually the following structure is used:
- DJANGO_MEDIA_ROOT = /data/media
Version 1: VM Based (obsolete as of 2023)¶
How to use¶
- Configure your app
- Deploy your app
- Install project specific python requirements into the pyvenv
- Restart uwsgi to restart your app
- Go to http://your-hostname/ and enjoy!
Configure your app¶
WSGI¶
- Ensure that there is projectname/wsgi.py in your project root
Database¶
Add the following to your settings.py:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'app', } }
Static and media files¶
Static files should be placed in /home/app/app/static and media files in /home/app/app/media.
Use the following code for configuration:
PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) STATIC_ROOT = os.path.join(BASE_DIR, "static/") MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
Usually to generate/deploy the static files, you can use collectstatic:
python manage.py collectstatic
Deploy (first time)¶
You can login to your VM as user app:
ssh app@MYHOSTNAME git clone <path-to-your-project> ~/app
- Deploy / update your app into the folder ~app/app (that is the app folder in the home of the user app)
- We recommended to use git to deploy it
Deploy (update)¶
ssh app@MYHOSTNAME cd ~/app git pull
Install python requirements into the pyvenv¶
Pyvenv has been installed to ~app/pyvenv for you.
Required packages for the hosting like PostgreSQL support and uwsgi support have already been placed.
You need to use pyvenv found in ~app/pyvenv, as uwsgi loads this pyvenv before loading your app.
To install your requirements use:
. ~/pyvenv/bin/activate pip install ...
Restarting the app¶
- sudo systemctl restart uwsgi
Viewing logfiles¶
- nginx access log: tail -F /var/log/nginx/access.log
- nginx error log: tail -F /var/log/nginx/error.log
- uwsgi log: tail -F /var/log/uwsgi/app/app.log
Description of the stack¶
Technologies¶
- Debian
- nginx
- PostgreSQL
- uwsgi
- python3 venv
Configuration¶
- user "app"
- in group "adm" (to view logfiles)
- PostgreSQL with
- database "app"
- Listens only on localhost / socket (no remote connections)
Updated by Nico Schottelius 3 months ago ยท 30 revisions