You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: When coming up with this URL, you can test by seeing if psql can connect to it::
80
+
Note: When coming up with this URL, you can test by seeing if psql can
81
+
connect to it::
74
82
75
83
$ psql $DATABASE_URL
76
84
77
-
Create a local directory for static files. This directory needs to be writable during this deploy process, and readable by the Django server at runtime. Call it STATIC_ROOT::
85
+
Create a local directory for static files. This directory needs to be
86
+
writable during this deploy process, and readable by the Django server
87
+
at runtime. Call it STATIC_ROOT::
78
88
79
89
$ export STATIC_ROOT=/path/to/staticfiles
80
90
81
-
(This is for files that come from the site source code and will be served as static files, like ``.css`` and ``.js`` files.)
91
+
(This is for files that come from the site source code and will be served
92
+
as static files, like ``.css`` and ``.js`` files.)
82
93
83
-
Create a local directory for media files. This directory must be readable and writable by the Django process at runtime. Call it MEDIA_ROOT::
94
+
Create a local directory for media files. This directory must be readable
95
+
and writable by the Django process at runtime. Call it MEDIA_ROOT::
84
96
85
97
$ export MEDIA_ROOT=/path/to/mediafiles
86
98
87
-
(This is for any files that might need to be uploaded by users and stored by the Django process, then served again later. Examples: images, avatars, files - it depends on the site.)
99
+
(This is for any files that might need to be uploaded by users and stored
100
+
by the Django process, then served again later. Examples: images, avatars,
101
+
files - it depends on the site.)
88
102
89
-
Generate a secret key to use, which should be a long random string. One way::
103
+
Generate a secret key to use, which should be a long random string. One
104
+
way::
90
105
91
106
#!/usr/bin/env python3
92
107
from django.utils.crypto import get_random_string
@@ -97,32 +112,41 @@ Then set it as DJANGO_SECRET_KEY::
97
112
98
113
$ export DJANGO_SECRET_KEY=<the random string from above>
99
114
100
-
This should be different for each site being deployed, but the same on all servers running Django for a particular site, and not changing over time.
115
+
This should be different for each site being deployed, but the same on
116
+
all servers running Django for a particular site, and not changing over
117
+
time.
101
118
102
119
Set DOMAIN to the hostname the site will be served at::
103
120
104
121
$ export DOMAIN=www.example.com
105
122
106
-
The site might need to send email to admins on errors, and to users for things like password resets. Arrange to make an SMTP server available for outgoing email, then set the following Django settings as environment variables:
123
+
The site might need to send email to admins on errors, and to users for
124
+
things like password resets. Arrange to make an SMTP server available for
125
+
outgoing email, then set the following Django settings as environment
126
+
variables:
107
127
108
128
* EMAIL_HOST
109
129
* EMAIL_HOST_USER
110
130
* EMAIL_HOST_PASSWORD
111
131
* EMAIL_USE_TLS
112
-
* EMAIL_USE_SSL (just set one of EMAIL_USE_TLS or EMAIL_USE_TLS to a non-empty string to indicate "True"; leave the other unset)
113
-
* EMAIL_PORT (optional; defaults to 25, 465, or 587 depending on whether EMAIL_USE_TLS, EMAIL_USE_SSL, or neither are set)
132
+
* EMAIL_USE_SSL (just set one of EMAIL_USE_TLS or EMAIL_USE_TLS to a
133
+
non-empty string to indicate "True"; leave the other unset)
134
+
* EMAIL_PORT (optional; defaults to 25, 465, or 587 depending on
135
+
whether EMAIL_USE_TLS, EMAIL_USE_SSL, or neither are set)
114
136
* DEFAULT_FROM_EMAIL
115
137
* EMAIL_SUBJECT_PREFIX
116
138
117
139
These are documented starting
118
-
`here <https://docs.djangoproject.com/en/3.0/ref/settings/#email-host>`_; I won't bother copying the docs.
There are a couple of tasks that need to be done any time the code is updated, before (re)starting
124
-
the server. The migrate step only needs to be done on one server since it updates the database
125
-
that all servers are sharing. The collectstatic step needs to be done on every server.
146
+
There are a couple of tasks that need to be done any time the code is
147
+
updated, before (re)starting the server. The migrate step only needs to
148
+
be done on one server since it updates the database that all servers are
149
+
sharing. The collectstatic step needs to be done on every server.
126
150
127
151
We generally build this into our deploy process.
128
152
@@ -143,12 +167,16 @@ We generally build this into our deploy process.
143
167
Run Django
144
168
..........
145
169
146
-
To get a process running Django and serving requests, we'll use a tool called `gunicorn <https://gunicorn.org/>`_ that's installed into the virtual environment.
170
+
To get a process running Django and serving requests, we'll use a tool
171
+
called `gunicorn <https://gunicorn.org/>`_ that's installed into the
172
+
virtual environment.
147
173
148
174
We'll run this strictly internally, listening for requests on a Unix port.
149
175
Our web server will proxy to that port.
150
176
151
-
Reminder: arrange for the environment variables mentioned above to be set before gunicorn is started. (You can set them on the gunicorn command line with ``-e``, but it gets unwieldy.)
177
+
Reminder: arrange for the environment variables mentioned above to be set
178
+
before gunicorn is started. (You can set them on the gunicorn command
179
+
line with ``-e``, but it gets unwieldy.)
152
180
153
181
::
154
182
@@ -160,11 +188,14 @@ Gunicorn has lots of options for tuning which you can look up.
160
188
Run a webserver in front
161
189
........................
162
190
163
-
We usually run nginx as our front-end web server. A simple approach is to add a new config file to /etc/nginx/sites-enabled for each site, making sure
164
-
server_name is set correctly in each. E.g. ``/etc/nginx/sites-enabled/www.example.com.conf``
165
-
(the name is completely arbitrary). Then reload or restart nginx.
191
+
We usually run nginx as our front-end web server. A simple approach is to
192
+
add a new config file to /etc/nginx/sites-enabled for each site, making
193
+
sure server_name is set correctly in each. E.g.
194
+
``/etc/nginx/sites-enabled/www.example.com.conf`` (the name is completely
195
+
arbitrary). Then reload or restart nginx.
166
196
167
-
In that config file, we generally want to redirect non-SSL requests to SSL with something like::
197
+
In that config file, we generally want to redirect non-SSL requests to
198
+
SSL with something like::
168
199
169
200
server {
170
201
listen *:80;
@@ -177,11 +208,11 @@ In that config file, we generally want to redirect non-SSL requests to SSL with
177
208
178
209
changing DOMAIN and PATH appropriately.
179
210
180
-
Then we proxy the SSL requests to Django, by adding something like this to the file (the SSL
181
-
cipher settings might be out of date, though).
211
+
Then we proxy the SSL requests to Django, by adding something like this
212
+
to the file (the SSL cipher settings might be out of date, though).
182
213
183
-
Note: *after* this is known to be working, you can uncomment the ``Strict-Transport-Security``
184
-
line if you want.
214
+
Note: *after* this is known to be working, you can uncomment the
215
+
``Strict-Transport-Security`` line if you want.
185
216
186
217
You'll need a valid SSL certificate for this.
187
218
@@ -239,13 +270,15 @@ Again, change the all-caps parts appropriately::
239
270
Troubleshooting
240
271
---------------
241
272
242
-
Once all that is running, you should be able to visit https://www.example.com and see
243
-
the site front page. But, sometimes not everything is quite right the first time :-)
273
+
Once all that is running, you should be able to visit
274
+
https://www.example.com and see the site front page. But, sometimes not
275
+
everything is quite right the first time :-)
244
276
245
-
A gateway error indicates that gunicorn isn't running. Add some gunicorn logging if necessary,
246
-
and check those logs.
277
+
A gateway error indicates that gunicorn isn't running. Add some gunicorn
278
+
logging if necessary, and check those logs.
247
279
248
-
If you see the wrong site, nginx isn't properly routing requests for that server name
249
-
to our server. See http://nginx.org/en/docs/http/server_names.html. Keep in mind that
250
-
nginx defaults to just sending requests to the first server it can find if it doesn't
251
-
recognize the incoming server name.
280
+
If you see the wrong site, nginx isn't properly routing requests for that
281
+
server name to our server. See
282
+
http://nginx.org/en/docs/http/server_names.html. Keep in mind that nginx
283
+
defaults to just sending requests to the first server it can find if it
0 commit comments