-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
521 lines (485 loc) · 18.9 KB
/
index.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-176066190-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-176066190-1');
</script>
<meta charset="UTF-8">
<title>Django Deployment Cheat Sheet</title>
<link rel="stylesheet" href="https://bootswatch.com/4/yeti/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<script src="assets/prism.js"></script>
<link rel="stylesheet" href="assets/prism.css"/>
</head>
<body>
<div class="container">
<div class="alert alert-dismissible alert-info mt-3">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>This is a Personal Project and is written for my own need</strong>
</div>
<div class="row">
<div class="col-md-6">
<form class="mt-3">
<fieldset>
<legend>Server Deployment Cheat Sheet</legend>
<div id="inputWithServerIPandUser">
<div class="form-group">
<label for="InputServerIP">Server address</label>
<input type="text" class="form-control" id="serverIP" aria-describedby="serverIPHelp"
placeholder="Enter Server IP">
<small id="serverIPHelp" class="form-text text-muted">Your Server Public IP</small>
</div>
<div class="form-group">
<label for="InputServerUser">Server User</label>
<input type="text" class="form-control" id="serverUser" aria-describedby="serverUserHelp"
placeholder="Enter Server User: Ubuntu Default" value="ubuntu" disabled>
<small id="serverUserHelp" class="form-text text-muted">Your Server User</small>
</div>
</div>
<div id="inputWithServerAlias">
<div class="form-group">
<label for="InputServerAlias">Server Alias</label>
<input type="text" class="form-control" id="serverAlias" aria-describedby="serverAliasHelp"
placeholder="Enter Server Alias">
<small id="serverAliasHelp" class="form-text text-muted">Alias for your user as stated on
<span style="">~/.ssh/config</span></small>
</div>
</div>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" id="toggleSwitch">
<label class="custom-control-label" for="toggleSwitch" id="toggleSwitchHelp"></label>
</div>
<button type="button" class="btn btn-primary mt-2" id="btnGenerateScriptDjango">Generate for
Django
</button>
<button type="button" class="btn btn-primary mt-2" id="btnGenerateScriptStatic">Generate for
Static
</button>
</fieldset>
</form>
</div>
<div class="col-md-6" id="createAlias">
<form class="mt-3">
<fieldset>
<legend>SSH Alias Generator</legend>
<div id="inputServerAlias">
<div class="form-group">
<label for="inputServerAlias">Host Identity</label>
<input type="text" class="form-control" id="hostIdentify" aria-describedby="serverHostAlias"
placeholder="Enter Server Alias">
<small id="serverHostAlias" class="form-text text-muted">Any Name which you like :D </small>
</div>
</div>
<div id="inputWithKeyFileLocation">
<div class="form-group">
<label for="InputServerIP">Key File Location</label>
<input type="text" class="form-control" id="keyFileLocation" aria-describedby="keyFileHelp"
placeholder="Enter key File location">
<small id="keyFileHelp" class="form-text text-muted">Your key file path eg:
~/.ssh/keys/server.pem</small>
</div>
</div>
<button type="button" class="btn btn-primary" data-toggle="modal" id="aliasGeneratingModel"
data-target="#modalForGeneratingServerAlias">
Generate Alias Config
</button>
<button type="button" class="btn btn-primary ml-4" id="btnGenerateBashFile">
Download .sh
</button>
</fieldset>
</form>
</div>
</div>
</div>
<div class="container" id="serverDeploymentScriptForDjango" hidden>
<div class="row">
<div class="col-md-7 mt-4">
<h2>Install Postgres on Server</h2>
<div class="helps">
<a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-18-04">
How To Install and Use PostgreSQL on Ubuntu 18.04 [digitalocean.com]
</a>
<a href="#" onclick="postgressHelponConsole()">Show Cheat..</a>
</div>
<pre>
<code class="language-shell">
sudo apt update
sudo apt install postgresql postgresql-contrib</code>
</pre>
</div>
<div class="col-md-5 mt-4">
<h2>On Your Machine </h2>
<div class="helps">
<a href="https://help.github.com/en/github/using-git/adding-a-remote">
Adding a Remote on Git
</a> [github.com]
</div>
<pre>
<code class="language-shell" id="setupGitRemoteOnClientDjango">
git remote add server ubuntu:/home/ubuntu/repo.git/ &&git push server --all</code>
</pre>
</div>
<div class="col-md-12">
<h2>Install Git Nginx and other Tools </h2>
<div class="helps">
</div>
<pre>
<code class="language-shell">
sudo apt-get install -y nginx git make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils python-setuptools</code>
</pre>
</div>
<div class="col-md-12">
<h2>Setting Up folders and Remote Repo </h2>
<div class="helps">
</div>
<pre>
<code class="language-shell">
mkdir repo.git app conf logs media static
cd repo.git</code></pre>
<pre><code class="language-shell">
# create a bare repo
git init --bare
git --bare update-server-info
git config core.bare false
git config receive.denycurrentbranch ignore
git config core.worktree /home/ubuntu/app/</code></pre>
<pre><code class="language-shell">
cat > hooks/post-receive <<EOF
#!/bin/sh
git checkout -f
EOF</code></pre>
<pre><code class="language-shell">
chmod +x hooks/post-receive
</code></pre>
</div>
<div class="col-md-12">
<h2>Setting Up Virtual Env and Install Server Dependency</h2>
<div class="helps">
</div>
<pre>
<code class="language-shell">
sudo apt-get install python3-venv libcairo2
sudo apt-get install build-essential libssl-dev libffi-dev
sudo apt-get install python3-pip python3-setuptools python3-wheel
sudo apt-get install libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 shared-mime-info
sudo apt-get install python3-dev python3-cffi
python3 -m venv env</code>
</pre>
<h3>Activate Virtual Env</h3>
<pre>
<code class="language-shell">
source env/bin/activate</code>
</pre>
<h3>Install Circus and Chaussette</h3>
<pre>
<code class="language-shell">
pip install circus
pip install chaussette
cd conf
nano circus.ini</code>
</pre>
<h3>add this to circus.ini</h3>
<pre>
<code class="language-shell">
[watcher:webapp]
cmd = chaussette --fd $(circus.sockets.webapp) config.wsgi.application # django setting location
uid=ubuntu
endpoint_owner=ubuntu
numprocesses = 3
use_sockets = True
copy_env = True
copy_path = True
virtualenv = /home/ubuntu/env/
stdout_stream.class = FileStream
stdout_stream.filename = /home/ubuntu/logs/app.log
stderr_stream.class = FileStream
stderr_stream.filename = /home/ubuntu/logs/app_err.log
stdout_stream.max_bytes = 1073741824
stdout_stream.backup_count = 3
stderr_stream.max_bytes = 1073741824
stderr_stream.backup_count = 3
[socket:webapp]
host = 127.0.0.1
port = 8085
[env:webapp]
PYTHONPATH=/home/ubuntu/app/</code>
</pre>
<h3>Run and demonize circus.ini</h3>
<pre>
<code class="language-shell">
circusd --daemon circus.ini
circusctl reloadconfig
circusctl reload</code>
</pre>
</div>
<div class="col-md-12">
<h2>Add Nginx Config </h2>
<div class="helps">
</div>
<pre>
<code class="language-shell">
nano nginx.conf # current location is ~/conf/</code>
</pre>
<pre>
<code class="language-shell">
upstream django {
server 127.0.0.1:8085; # for a web port socket see circus.ini [socket:webapp]
}
# configuration of the server
server {
# the port your site will be served on
listen 80;
# the domain name it will serve for
# server_name demo.example.com;
charset utf-8;
client_max_body_size 75M;
location /static/ {
alias /home/ubuntu/static/;
}
location /media/ {
alias /home/ubuntu/media/;
}
location / {
proxy_pass http://django;
proxy_pass_header Server;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
# proxy_set_header X-Forwarded-Proto https;
}
}</code></pre>
<h3>Add nginx config to /etc/nginx/nginx.conf</h3>
<pre>
<code class="language-shell">
nano /etc/nginx/nginx.conf
# include your nginx conf
# include /home/ubuntu/conf/nginx.conf;
</code></pre>
<h3>Test config and restart the server</h3>
<pre>
<code class="language-shell">
sudo nginx -t # test should be successful
sudo systemctl restart nginx # restart the server
</code></pre>
<hr>
<h1>Extras</h1>
<hr>
<h3>For HTTPS Part</h3>
<pre>
<code class="language-shell">
sudo apt-get install python3-certbot-nginx
sudo certbot --nginx
</code></pre>
<h3>Update nginx Conf </h3>
<pre>
<code class="language-shell">
# include proxy_set_header X-Forwarded-Proto https so your new configuration will look like
location / {
........
........
proxy_set_header X-Forwarded-Proto https;
}
</code></pre>
<h3>HTTPS Settings For Django</h3>
<pre>
<code class="language-shell">
USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
</code></pre>
</div>
</div>
</div>
<div class="container" id="serverDeploymentScriptForStatic" hidden>
<div class="row">
<div class="col-md-7 mt-4">
<h2>Install Nginx and git on Server</h2>
<div class="helps">
<a href="https://www.nginx.com/resources/glossary/nginx/">What is NGINX?</a> [nginx.com]
</div>
<pre>
<code class="language-shell">
sudo apt update
sudo apt-get install -y nginx git</code>
</pre>
</div>
<div class="col-md-5 mt-4">
<h2>On Your Machine </h2>
<div class="helps">
<a href="https://help.github.com/en/github/using-git/adding-a-remote">
Adding a Remote on Git
</a> [github.com]
</div>
<pre>
<code class="language-shell" id="setupGitRemoteOnClientStatic">
git remote add server ubuntu:/home/ubuntu/repo.git/ &&git push server --all</code>
</pre>
</div>
<div class="col-md-12">
<h2>Setting Up folders and Remote Repo </h2>
<div class="helps">
</div>
<pre>
<code class="language-shell">
mkdir repo.git app conf
cd repo.git</code></pre>
<pre><code class="language-shell">
# create a bare repo
git init --bare
git --bare update-server-info
git config core.bare false
git config receive.denycurrentbranch ignore
git config core.worktree /home/ubuntu/app/</code></pre>
<pre><code class="language-shell">
cat > hooks/post-receive <<EOF
#!/bin/sh
git checkout -f
EOF</code></pre>
<pre><code class="language-shell">
chmod +x hooks/post-receive
</code></pre>
</div>
<div class="col-md-12">
<h2>Add Nginx Config </h2>
<div class="helps">
</div>
<pre>
<code class="language-shell">
nano nginx.conf # in conf/ directory</code>
</pre>
<pre>
<code class="language-shell">
server {
listen 80;
client_max_body_size 64M;
# set the correct host(s) for your site
# server_name example.com;
keepalive_timeout 5;
# path for static files
root /home/ubuntu/app;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}</code></pre>
<h3>Add nginx config to /etc/nginx/nginx.conf</h3>
<pre>
<code class="language-shell">
nano /etc/nginx/nginx.conf
# include your nginx conf
# include /home/ubuntu/conf/nginx.conf;
</code></pre>
<h3>Test config and restart the server</h3>
<pre>
<code class="language-shell">
sudo nginx -t # test should be successful
sudo systemctl restart nginx # restart the server
</code></pre>
</div>
</div>
</div>
<!-- Modal for alias generation -->
<div class="modal fade" id="modalForGeneratingServerAlias" tabindex="-1"
role="dialog" aria-labelledby="modalForGeneratingServerAlias"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Sever Alias</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<pre>
<code class="language-shell" id="serverAliasCode">
</code>
</pre>
</div>
</div>
</div>
</div>
</body>
<script>
let configureWithServerIPHelp = 'Toggle to Configure with Server IP and User instead'
let configureWithServerAlias = 'Toggle to Configure With Server Alias Instead'
$(function () {
$('#toggleSwitchHelp').text(configureWithServerIPHelp)
$('#inputWithServerIPandUser').attr('hidden', true)
$('#createAlias').attr('hidden', true)
$("#aliasGeneratingModel").click(function () {
let serverIp = $('#serverIP').val()
let serverUser = $('#serverUser').val()
let keyLocation = $('#keyFileLocation').val()
let hostIdentify = $('#hostIdentify').val()
let aliasGeneratingCode = "\n" +
"nano ~/.ssh/config\n" +
"# append the following\n" +
"\n" +
`Host ${hostIdentify} \n` +
` HostName ${serverIp}\n` +
` User ${serverUser}\n` +
` IdentityFile ${keyLocation}`
$("#modalForGeneratingServerAlias #serverAliasCode").text(aliasGeneratingCode);
Prism.highlightElement($('#serverAliasCode')[0]);
})
$("#btnGenerateBashFile").click(function () {
// TODO: remove this duplicate code
let serverIp = $('#serverIP').val()
let serverUser = $('#serverUser').val()
let keyLocation = $('#keyFileLocation').val()
let hostIdentify = $('#hostIdentify').val()
if (serverUser && serverUser && keyLocation && hostIdentify) {
download(`${hostIdentify}.sh`, `ssh -i ${keyLocation} ${serverUser}@${serverIp}`)
} else {
alert("Fill up the Form Correctly Please")
}
})
})
$('#toggleSwitch').change(function () {
if ($(this).prop('checked') === true) {
$('#toggleSwitchHelp').text(configureWithServerAlias)
$('#inputWithServerAlias').attr('hidden', true)
$('#inputWithServerIPandUser').attr('hidden', false)
$('#createAlias').attr('hidden', false)
} else {
$('#toggleSwitchHelp').text(configureWithServerIPHelp)
$('#inputWithServerIPandUser').attr('hidden', true)
$('#createAlias').attr('hidden', true)
$('#inputWithServerAlias').attr('hidden', false)
}
})
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
</script>
<script>
function postgressHelponConsole(){
alert("See console. Happy Coding")
console.log(`1. sudo su postgres
2. psql
3. CREATE DATABASE database_name;
4. CREATE USER my_username WITH PASSWORD 'my_password';
5. GRANT ALL PRIVILEGES ON DATABASE "database_name" to my_username;
`)
}
</script>
<script src="assets/deployment.js"></script>
</html>