Project

General

Profile

Django-hosting » History » Version 30

Nico Schottelius, 09/03/2024 02:33 PM

1 23 Nico Schottelius
h1. Django hosting by ungleich
2 1 Nico Schottelius
3 9 Nico Schottelius
{{toc}}
4 1 Nico Schottelius
5 22 Nico Schottelius
h2. Status
6
7 26 Nico Schottelius
This document is **IN PRODUCTION** (since 2016-04-08). 
8
Pricing can be found on the "ungleich products page":https://ungleich.ch/u/products/django-hosting/.
9 22 Nico Schottelius
10 30 Nico Schottelius
h2. Version 2: Kubernetes Based (active as of 2023)
11
12
h3. Deployment preparation
13
14
* Create a (docker) container that contains everything the django app needs to run
15
* The container should listen on port 9000
16
** This allows running django on port 8000 and potentially an nginx or similar inside for serving static files as well
17
* The container needs to read the database configuration from environment variables (see below)
18
19
h3. Database configuration
20
21
The database configuration is passed into the container using environment variables as follows:
22
23
<pre>
24
  POSTGRES_USER
25
  POSTGRES_DB
26
  POSTGRES_PASSWORD
27
  POSTGRES_HOST
28
</pre>
29
30
Additionally the following variable is passed in that needs to be interpolated (i.e. variables need to resolve):
31
32
<pre>
33
DATABASE_URL = postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):5432/$(POSTGRES_DB)
34
</pre>
35
36
37
h3. Static (pre-compiled) files
38
39
If you want to serve static, pre-compiled files, add them into your container in */app/static*.
40
All files stored in that directory will be exposed using an integrated nginx.
41
42
43
h3. Data directory
44
45
If your application needs to store files to persistent storage, it should be stored below /data. Usually the following structure is used:
46
47
* DJANGO_MEDIA_ROOT = /data/media
48
49
50 29 Nico Schottelius
h2. Version 1: VM Based (obsolete as of 2023)
51 1 Nico Schottelius
52 29 Nico Schottelius
h3. How to use
53
54 8 Nico Schottelius
# Configure your app
55
# Deploy your app
56 18 Nico Schottelius
# Install project specific python requirements into the pyvenv
57 8 Nico Schottelius
# Restart uwsgi to restart your app
58 1 Nico Schottelius
# Go to http://your-hostname/ and enjoy!
59
60
61 29 Nico Schottelius
h3. Configure your app
62 8 Nico Schottelius
63 29 Nico Schottelius
h4. WSGI
64 8 Nico Schottelius
65
* Ensure that there is _projectname_/wsgi.py in your project root
66
67 29 Nico Schottelius
h5. Database
68 8 Nico Schottelius
69
Add the following to your settings.py:
70
71
<pre>
72
DATABASES = {
73
    'default': {
74
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
75 1 Nico Schottelius
        'NAME': 'app',
76 8 Nico Schottelius
    }
77
}
78 4 Nico Schottelius
</pre>
79 14 Nico Schottelius
80 29 Nico Schottelius
h4. Static and media files
81 14 Nico Schottelius
82 22 Nico Schottelius
Static files should be placed in */home/app/app/static* and media files in */home/app/app/media*.
83 14 Nico Schottelius
84 16 Nico Schottelius
Use the following code for configuration:
85
86
<pre>
87 21 Nico Schottelius
PROJECT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
88 16 Nico Schottelius
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
89
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
90
</pre>
91 27 Nico Schottelius
92 1 Nico Schottelius
Usually to generate/deploy the static files, you can use collectstatic:
93 27 Nico Schottelius
94
<pre>
95
python manage.py collectstatic                                                             </pre>
96
97
98 29 Nico Schottelius
h3. Deploy (first time)
99 1 Nico Schottelius
100 25 Nico Schottelius
You can login to your VM as user *app*:
101 1 Nico Schottelius
102 10 Nico Schottelius
<pre>
103 17 Nico Schottelius
ssh app@MYHOSTNAME
104 1 Nico Schottelius
git clone <path-to-your-project> ~/app
105 10 Nico Schottelius
</pre>
106 4 Nico Schottelius
107 1 Nico Schottelius
108
* Deploy / update your app into the folder *~app/app* (that is the app folder in the home of the user app)
109
* We recommended to use git to deploy it
110 24 Nico Schottelius
111 29 Nico Schottelius
h3. Deploy (update)
112 1 Nico Schottelius
113 24 Nico Schottelius
<pre>
114
ssh app@MYHOSTNAME
115
cd ~/app
116
git pull
117
</pre>
118
119 11 Nico Schottelius
120 29 Nico Schottelius
h3. Install python requirements into the pyvenv
121 11 Nico Schottelius
122 13 Nico Schottelius
Pyvenv has been installed to *~app/pyvenv* for you.
123
Required packages for the hosting like PostgreSQL support and uwsgi support have already been placed.
124
125 1 Nico Schottelius
You need to use pyvenv found in *~app/pyvenv*, as uwsgi loads this pyvenv before loading your app.
126 13 Nico Schottelius
127
To install your requirements use:
128 11 Nico Schottelius
129 1 Nico Schottelius
<pre>
130 11 Nico Schottelius
. ~/pyvenv/bin/activate
131
pip install ...
132
</pre>
133 10 Nico Schottelius
134 29 Nico Schottelius
h3. Restarting the app
135 2 Nico Schottelius
136 1 Nico Schottelius
* *sudo systemctl restart uwsgi*
137
138 2 Nico Schottelius
139 29 Nico Schottelius
h4. Viewing logfiles
140 4 Nico Schottelius
141
* nginx access log: *tail -F /var/log/nginx/access.log*
142 7 Nico Schottelius
* nginx error log: *tail -F /var/log/nginx/error.log*
143 4 Nico Schottelius
* uwsgi log: *tail -F /var/log/uwsgi/app/app.log*
144 1 Nico Schottelius
145
146
147 29 Nico Schottelius
h3. Description of the stack
148 1 Nico Schottelius
149 29 Nico Schottelius
h4. Technologies
150 1 Nico Schottelius
151 22 Nico Schottelius
* Debian
152 1 Nico Schottelius
* nginx
153
* PostgreSQL
154 3 Nico Schottelius
* uwsgi
155 28 Nico Schottelius
* python3 venv
156 3 Nico Schottelius
157 29 Nico Schottelius
h4. Configuration
158 3 Nico Schottelius
159
* user "app"
160
** in group "adm" (to view logfiles)
161
* PostgreSQL with
162 1 Nico Schottelius
** database "app"
163 3 Nico Schottelius
** Listens only on localhost / socket (no remote connections)