-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hosting-django-react-website-on-heroku.html
312 lines (294 loc) · 13.3 KB
/
Hosting-django-react-website-on-heroku.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<title>Hosting django-react website on heroku</title>
<style>
* {
box-sizing: border-box;
}
body {
background-color: aqua;
}
h2 {
color: blue;
margin-left: 30px;
margin-right: 30px;
margin-top: 20px;
font-size: 19px;
font-weight: 400;
}
span {
font-size: large;
color: green;
}
.span-container {
border: 1px solid blue;
background-color: white;
}
li {
color: green;
font-size: large;
}
.div-width,
.div-diff {
width: fit-content;
}
.special-width {
width: fit-content;
}
@media(max-width: 768px) {
.longer-div {
margin-left: 10px;
margin-right: 10px;
}
.div-width {
width: 315px;
}
.special-width {
width: 315px;
}
.div-diff {
margin-left: 20px !important;
margin-right: 20px !important;
}
}
</style>
</head>
<body>
<h1 class="text-center pt-2" style="color: blue; font-weight: 400;">Complete guide to host your django-react website
on heroku</h1>
<hr color="blue">
<h2>1. Copy your project separately.</h2>
<h2>2. Go to the official website of heroku and create your account.</h2>
<h2>3. The Heroku CLI requires git, so download git from it's official website.</h2>
<h2 class="ml-5"">By default, the <span>'git cmd'</span> and <span>'git bash'</span> will be installed along with git.</h2>
<h2>4. Go to the folder in which you have copied your files.</h2>
<h2>5. Open the command prompt or terminal there and navigate to that folder.</h2>
<h2>6. Install python virtual environment using the command - </h2>
<div class=" mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>pip install pipenv</span>
</div>
<h2>7. You can check the git version using the command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>git --version</span>
</div>
<h2>8. Before pushing the files to <span>heroku</span>, we need to make some changes in the django and react
settings.</h2>
<h2 class="text-center">Please make sure your directory structure is as follows : </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:280px;">
<h3 class="my-2" style="color: red; margin-left: 10%;">Folder_name</h3>
<ul style="margin-left: 10%;">
<li>django_project</li>
</ul>
</div>
<h2>9. Create <span>virtualenv</span> using command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>virtualenv env</span>
</div>
<h2>10. Activate the <span>virtualenv</span> using command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>env\Scripts\activate</span>
</div>
<h2>11. Now your directory structure should be like : </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:280px;">
<h3 class="my-2" style="color: red; margin-left: 10%;">Folder_name</h3>
<ul style="margin-left: 10%;">
<li>django_project</li>
<li>django_app</li>
<li><span style="background-color: yellow;">env</span></li>
<li>public</li>
<li>src</li>
<li>.gitignore</li>
<li>db.sqlite3</li>
<li>manage.py</li>
<li>package-lock.json</li>
<li>package.json</li>
</ul>
</div>
<h2>12. Login to heroku from the Command Line using the command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>heroku login</span>
</div>
<h2>13. Create your <span>web app</span> using the command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>heroku create </span><span style="background-color: yellow;">app_name</span>
</div>
<h2>14. Initialize your <span>git repository</span> using the command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>git init</span>
</div>
<h2>15. Add your <span>website</span> to <span>heroku</span> using the command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>heroku git:remote -a </span><span style="background-color: yellow;">app_name</span>
</div>
<h2>16. Now, we need <span>buildpacks</span> for <span>django</span> and <span>reactjs</span>.</h2>
<h2 class="ml-5">First we will create the <span>buildpack</span> for <span>django</span> using the command -
</h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>heroku buildpacks:set heroku/python</span>
</div>
<h2>17. Then, we'll create the <span>buildpack</span> for <span>reactjs</span> using the command - </h2>
<div class="mx-auto div-diff" style="border: 1px solid blue; background-color: white; padding: 10px;">
<span>heroku buildpacks:add --index 1 heroku/nodejs</span>
</div>
<h2>18. We need <span>whitenoise</span> to serve static files, so install it inside the virtualenv in the
terminal using the
command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>pip install whitenoise</span>
</div>
<h2>19. We need <span>gunicorn</span> to run our server, so install it inside the virtualenv in the terminal
using the
command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>pip install gunicorn</span>
</div>
<h2 class="text-center">Mention all the requirement packages in <span>requirements.txt</span> file using the
command - </h2>
<div class="mx-auto d-block mt-3 px-3 pt-3 special-width"
style="border: 1px solid blue; background-color: white;">
<pre style="font-size: large; color: green;">pip freeze > requirements.txt</pre>
</div>
<h2>20. Edit the <span>settings.py</span> file as shown below : </h2>
<div class="mx-auto d-block mt-3 px-3 pt-3 special-width"
style="border: 1px solid blue; background-color: white;">
<pre
style="font-size: large; color: green;">ALLOWED_HOSTS = ['<span style="background-color: yellow;">app_name</span>.herokuapp.com', 'localhost']</pre>
</div>
<div class="mx-auto d-block mt-3 pr-3 pt-3 special-width"
style="border: 1px solid blue; background-color: white;">
<pre style="font-size: large; color: green;">
INSTALLED_APPS = [
#...
<span style="background-color: yellow;">'whitenoise.runserver_nostatic',</span>
'django.contrib.staticfiles',
<span style="background-color: yellow;">'django_app'</span>
]</pre>
</div>
<div class="mx-auto d-block mt-3 pr-3 pt-3 special-width"
style="border: 1px solid blue; background-color: white;">
<pre style="font-size: large; color: green;">
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
<span style="background-color: yellow;">'whitenoise.middleware.WhiteNoiseMiddleware',</span>
# ...
]</pre>
</div>
<div class="mx-auto d-block mt-3 pr-3 pt-3 special-width"
style="border: 1px solid blue; background-color: white;">
<pre style="font-size: large; color: green;">
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'build/static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'</pre>
</div>
<div class="mx-auto d-block mt-3 pr-3 pt-3 special-width"
style="border: 1px solid blue; background-color: white;">
<pre style="font-size: large; color: green;">
TEMPLATES = [
{
#...
'DIRS': [os.path.join(BASE_DIR, '<span style="background-color: yellow;">build</span>')],
'APP_DIRS': True,
#...
},
]</pre>
</div>
<h2>21. Create a file <span>Procfile</span> in the root folder directory and edit it as follows :</h2>
<div class="mx-auto d-block mt-3 pr-5 pt-3 special-width"
style="border: 1px solid blue; background-color: white;">
<pre style="font-size: large; color: green;">
release: python manage.py migrate
web: gunicorn <span style="background-color: yellow;">django_project</span>.wsgi</pre>
</div>
<h2>22. Create a file <span>runtime.txt</span> in the root folder directory and mention the python version in it
as
follows :</h2>
<div class="mx-auto d-block mt-3 px-3 pt-3"
style="border: 1px solid blue; background-color: white; width: fit-content;">
<pre style="font-size: large; color: green;">python-3.8.2</pre>
</div>
<h2 class="text-center">Now your directory structure should be like : </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:280px;">
<h3 class="my-2" style="color: red; margin-left: 10%;">Folder_name</h3>
<ul style="margin-left: 10%;">
<li>django_project</li>
<li>django_app</li>
<li>env</li>
<li>public</li>
<li>src</li>
<li>.gitignore</li>
<li>db.sqlite3</li>
<li>manage.py</li>
<li>package-lock.json</li>
<li>package.json</li>
<li><span style="background-color: yellow;">Procfile</span></li>
<li><span style="background-color: yellow;">runtime.txt</span></li>
</ul>
</div>
<h2>23. Now edit the <span>package.json</span> file as follows :</h2>
<div class="mx-auto d-block mt-3 pr-3 pt-3 special-width"
style="border: 1px solid blue; background-color: white;">
<pre style="font-size: large; color: green;">
{
#...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"postinstall": "npm run build"
},
#...
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"engines": {
"node": "<span style="background-color: yellow;">Your node version</span>",
"npm": "<span style="background-color: yellow;">Your npm version</span>"
}
}</pre>
</div>
<h2>24. Don't forget to mention the <span>virtualenv name</span> in <span>.gitignore</span> file.</h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span style="background-color: yellow;">env</span><br>
<span>\node_modules</span>
</div>
<h2>25. Run the command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>git add .</span>
</div>
<h2>26. <span>Commit</span> to git using the command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>git commit -am "Commit message"</span>
</div>
<h2>27. Now push your files to <span>heroku</span> using the command - </h2>
<div class="mx-auto" style="border: 1px solid blue; background-color: white; width:fit-content; padding: 10px;">
<span>git push heroku master</span>
</div>
<h2>28. And it's done....</h2>
<h2>29. Your website has been created <span>successfully</span>.</h2>
<hr color="blue">
<h1 class="text-center" style="color: blue; font-size: 50px; font-weight: 400;">Thank you</h1>
<hr color="blue">
<div class="text-center">
<h2 style="color: blue;">By...</h2>
<h2>Saurav Kumar Sharma</h2>
</div>
</body>
</html>