forked from mattmakai/fullstackpython.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
693 lines (671 loc) · 41.2 KB
/
index.html
File metadata and controls
693 lines (671 loc) · 41.2 KB
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
<!doctype html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js" lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="language" content="English" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Full Stack Python</title>
<meta name="description" content="Full stack Python shows how an entire Python web application is built, from the server to the front end JavaScript.">
<meta name="author" content="Matt Makai">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="/css/c.css" />
<link rel="stylesheet" href="/css/bootstrap-responsive.min.css" />
<script src="/js/modernizr-2.0.6.min.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19910497-7']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body data-spy="scroll" data-target="#fsp-side-nav" >
<a href="https://github.com/makaimc/fullstackpython.github.com" class="github">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" />
</a>
<div class="container">
<div class="row">
<div class="span12">
<div class="logo-header-section">
<a href="/"><img src="../img/full-stack-python-logo-bw.png" height="42" width="42" class="logo-image" /></a>
<span class="logo-title"><a href="/">Full Stack Python</a></span>
</div>
</div>
</div>
<div class="row">
<div class="span2" id="fsp-side-nav">
<ul class="nav nav-list fsp-side-nav-ul affix">
<li><a href="#introduction">Introduction</a></li>
<li><a href="#servers">Servers</a></li>
<li><a href="#operating-system">Operating System</a></li>
<li><a href="#web-server">Web Server</a></li>
<li><a href="#database">Database</a></li>
<li><a href="#wsgi-server">WSGI Server</a></li>
<li><a href="#web-framework">Web Framework</a></li>
<li><a href="#static-content">Static Content</a></li>
<li><a href="#website-security">Website Security</a></li>
<li><a href="#caching">Caching</a></li>
<li><a href="#web-analytics">Web Analytics</a></li>
<li><a href="#monitoring">Monitoring</a></li>
<li><a href="#no-sql-datastore">NoSQL Data Stores</a></li>
<li><a href="#about-author">About</a></li>
<li><a href="#change-log">Change Log</a></li>
</ul>
</div>
<div class="span9 offset1">
<section id="introduction" class="tech-section">
<h1>Introduction</h1>
<p>You're knee deep in learning the <a class="reference external" href="http://www.python.org/">Python</a>
programming language. The syntax is beginning to make sense. The first
few "AHA!" moments are hitting you. Now you are excited for continued
direction to accelerate your learning.</p>
<p>This guide explains each piece of a Python web stack. If you want to deploy
a web application built with Django, Flask, Pyramid, or another
Python-based web framework, you've come to the right place.</p>
<p>There are a few other fantastic Python guides that you may want to read if
you're not yet ready to deploy your application.</p>
<p>For general Python learning, read Kenneth Reitz's
<a class="reference external" href="http://docs.python-guide.org/en/latest/">The Hitchhiker’s Guide to Python</a>.</p>
<p>To deploy a Python web application to the Heroku platform-as-a-service
(described below in this guide), check out
<a class="reference external" href="http://www.deploydjango.com/">Deploying Django</a>.</p>
<p>If you're not coming in with any software development background,
you may find my post on
<a class="reference external" href="http://www.mattmakai.com/learning-python-for-non-developers.html">Learning Python</a>
useful.</p>
<p>This guide has a different focus from the above resources. Here I
focus on explaining several ways of for how the Python <em>web application</em>
stack works from server infrastructure through JavaScript execution on a
user's browser.</p>
<p>People learning Python generally have at least one of the three following
goals:</p>
<ol class="arabic simple">
<li>Build web applications</li>
<li>Process, analyze, and visualize data</li>
<li>Script server administration</li>
</ol>
<p>This guide will be most useful to people building web applications and
scripting server administration.</p>
</section>
<section id="servers" class="tech-section">
<h1>Servers</h1>
<p>There are four options for setting up infrastructure to serve a
web application:</p>
<ol class="arabic simple">
<li>"Bare metal" servers</li>
<li>Virtualized servers</li>
<li>Infrastructure-as-a-service</li>
<li>Platform-as-a-service</li>
</ol>
<div class="section" id="bare-metal">
<h2>Bare metal</h2>
<p>The term <em>bare metal</em> refers to purchasing the actual hardware and hooking
it up to the Internet either through a business-class internet service
provider (ISP) or
<a class="reference external" href="http://webdesign.about.com/od/colocation/a/what_colocation.htm">co-locating the server</a> with other servers. A "business-class" ISP is necessary because
most residential Internet service agreements prohibt and will eventually
block traffic to web servers. You may be able to get away with low traffic
volume but if your site serves a lot of traffic it will catch an ISPs
filters.</p>
<p>The bare metal option offers the most control over the server configuration,
usually has the highest performance for the price, but also is the most
expensive upfront option and the highest ongoing maintenance. With bare
metal servers the ongoing operating cost is the electricity the server(s)
use as well as handling repairs when server components malfunction. You're
taking on manual labor working with hardware as well as the rest of the
software stack.</p>
<p>Buy actual hardware from a vendor either pre-built or as a collection of
components that you assemble yourself. Here is an
<a class="reference external" href="http://duartes.org/gustavo/blog/post/building-a-quad-core-server">example of a server buildout</a>. The post is a couple of years old but those are the
rough components you need to put together your own server. You can also buy
pre-configured servers from Dell or HP. Those servers tend to be in
smaller case form factors (called "blades") but are correspondingly more
expensive than putting off-the-shelf components together yourself.</p>
</div>
<div class="section" id="virtualized-servers">
<h2>Virtualized servers</h2>
<p>Virtual private servers (VPSs) are slices of hardware on top of a larger
bare metal server. Virtualization software such as
<a class="reference external" href="http://www.xen.org/">Xen</a> and
<a class="reference external" href="http://www.vmware.com/virtualization/what-is-virtualization.html">VMWare</a>
allow providers such as <a class="reference external" href="http://www.linode.com/">Linode</a> and
<a class="reference external" href="http://prgmr.com/xen/">prgmr</a> (as well as a many others) to provide
fractions of a full server that appear as their own instances. For example,
a server with an 8-core Xeon processor and 16 gigabytes of memory can be
sliced into 8 pieces with the equivalent of 1-core and 2 gigabytes of
memory.</p>
<p>The primary disadvantage of virtualized servers is that there is resource
overhead in the virtualization process. In addition, physical constraints
such as heavy I/O operations by a single virtualized instance on persistent
storage can cause performance bottlenecks for other virtualized instances on
the shared server. Choosing virtualized server hosting should be based on
your needs for urgency of service ticket requests and the frequency you
require for ongoing maintenance such as persistent storage backups.</p>
</div>
<div class="section" id="infrastructure-as-a-service">
<h2>Infrastructure-as-a-service</h2>
<p>Infrastructure-as-a-service (IaaS) overlaps with virtualized servers
because the resources are often presented in the same way. The
difference between virtualized servers and IaaS is the granularity of the
billing cycle. IaaS generally encourages a finer granularity based on minutes
or hours of server usage instead of on monthly billing cycles.</p>
<p>IaaS can be used in combination with virtualized servers to provide
dynamic upscaling for heavy traffic. When traffic is low then virtualized
servers can solely be used. This combination of resources reduces cost at
the expense of greater complexity in the dynamically scaled infrastructure.</p>
<p>The most common IaaS platforms are
<a class="reference external" href="http://aws.amazon.com/">Amazon Web Services</a> and
<a class="reference external" href="http://www.rackspace.com/cloud/">Rackspace Cloud</a>.</p>
<p>The disadvantage to IaaS platforms is the lock-in if you have to write
custom code to deploy, dynamically scale, and generally understand your
infrastructure. Every platform has its quirks. For example,
Amazon's standard <a class="reference external" href="http://aws.amazon.com/ebs/">Elastic Block Store</a> storage
infrastructure has at least an order of magnitude worse I/O throughput
than working with your local disk. Your application's database queries may
work great locally but then when you deploy the performance is inadequate.
Amazon has <a class="reference external" href="http://aws.amazon.com/about-aws/whats-new/2012/07/31/announcing-provisioned-iops-for-amazon-ebs/">higher throughput EBS instances</a>
but you will pay correspondingly more for them. EBS throughput is just
one of many quirks you need to understand before committing to an
IaaS platform.</p>
</div>
<div class="section" id="platform-as-a-service">
<h2>Platform-as-a-service</h2>
<p>A platform-as-a-service (PaaS) provides infrastructure and a software layer
on which a web application is deployed. The PaaS layer defines how the
application accesses resources such as computing time, files, and
external services. The PaaS provides a higher-level abstraction for working
with computing resources than deploying an application to a server or IaaS.</p>
<p>A PaaS makes deployment and operations easier because it forces the developer
to conform applications to the PaaS architecture.</p>
<p><a class="reference external" href="http://www.heroku.com/">Heroku</a>,
<a class="reference external" href="https://developers.google.com/appengine/">Google App Engine</a>,
<a class="reference external" href="https://gondor.io/">Gondor</a>, and
<a class="reference external" href="https://openshift.redhat.com/community/get-started/python">OpenShift</a> are
platform-as-services that support Python web applications. Each one requires
varying tradeoffs to deploy to their respective platforms.</p>
<img alt="Traditional LAMP server stack versus a Platform-as-a-Service stack" class="technical-diagram" src="../img/servers-versus-paas.png" />
<p>If you go the PaaS route, you can skip over the operating system and web
server sections because they are baked into PaaS offerings. PaaS offerings
generally start at the WSGI server layer.</p>
</div>
<div class="section" id="server-resources">
<h2>Server Resources</h2>
<p><a class="reference external" href="http://arstechnica.com/gadgets/2012/11/how-to-set-up-a-safe-and-secure-web-server/">How to set up a safe and secure Web server</a> (Ars Technica)</p>
<p><a class="reference external" href="http://www.kalzumeus.com/2010/12/12/staging-servers-source-control-deploy-workflows-and-other-stuff-nobody-teaches-you/">Staging Servers, Source Control & Deploy Workflows, And Other Stuff Nobody Teaches You</a> (Patrick McKenzie)</p>
<p><a class="reference external" href="http://rdegges.com/devops-django-part-4-choosing-heroku">Choosing Heroku</a>
(Randall Degges)</p>
<p><a class="reference external" href="http://blog.redfern.me/choosing-a-low-cost-vps/">Choosing a low cost VPS</a></p>
<p><a class="reference external" href="https://github.com/boto/boto">boto</a> is an amazing Python library for
working with Amazon Web Services</p>
</div>
</section>
<section id="operating-system" class="tech-section">
<h1>Operating System</h1>
<p>The operating system runs on top of the server or virtual server
and controls access to computing resources. The only operating
system I can recommend for production Python web stack deployments is
a flavor of Linux. Ubuntu Long Term Support (LTS) releases, Red Hat
Enterprise Linux, and CentOS are all viable options.</p>
<p><em>Side note</em>: Mac OS X is fine for development activities. Windows and Mac
OS X are not appropriate for most test and production deployments unless
there is a specific reason why you must use them in lieu of Linux.</p>
<div class="section" id="ubuntu">
<h2>Ubuntu</h2>
<p>Ubuntu is a Linux distribution packaged by the
<a class="reference external" href="http://www.canonical.com/">Canonical Ltd</a> company. Ubuntu uses the
Debian distribution as a base for packages, including the <a class="reference external" href="http://wiki.debian.org/Apt">aptitude package
manager</a>. For desktop versions of Ubuntu,
GNOME (until the 11.04 release) or Unity (11.10 through current)
is added to the distribution to provide a user interface.</p>
<p>Ubuntu <a class="reference external" href="https://wiki.ubuntu.com/LTS">Long Term Support (LTS)</a> releases
are the recommended versions to use for deployments. LTS versions receive
five years of post-release updates from Canonical. Every two years, Canonical
creates a new LTS release, which allows for an easy upgrade path as well
as flexibility in skipping every other LTS release if necessary.</p>
</div>
<div class="section" id="red-hat-and-centos">
<h2>Red Hat and CentOS</h2>
<p><a class="reference external" href="http://www.redhat.com/products/enterprise-linux/">Red Hat Enterprise Linux</a>
(RHEL) and <a class="reference external" href="http://www.centos.org/">Community ENTerprise Operating System</a>
(CentOS) are the same distribution. The only difference between the two
(other than the name) is that CentOS is an open source, liberally
licensed free derivative of RHEL.</p>
<p>RHEL and CentOS use a different package manager and command-line interface
from Debian-based Linux distributions: RPM Package Manager (RPM) and the
Yellowdog Updater, Modified (YUM). RPM has a specific .rpm file format
to handle the packaging and installation of libraries and applications. YUM
provides a command-line interface for interacting with the RPM system.</p>
</div>
<div class="section" id="operating-system-resources">
<h2>Operating System Resources</h2>
<p><a class="reference external" href="http://www.andrewault.net/2010/05/17/securing-an-ubuntu-server/">Securing an Ubuntu Server</a></p>
<p><a class="reference external" href="http://joshrendek.com/2013/01/securing-ubuntu/">Securing Ubuntu</a></p>
<p><a class="reference external" href="http://plusbryan.com/my-first-5-minutes-on-a-server-or-essential-security-for-linux-servers">First 5 Minutes on a Server</a></p>
<p><a class="reference external" href="http://spenserj.com/blog/2013/07/15/securing-a-linux-server/">Securing a Linux Server</a></p>
<p><a class="reference external" href="https://github.com/marshyski/quick-secure">quick-secure scripts</a> for
securing RedHat flavors of Linux on boot.</p>
</div>
</section>
<section id="web-server" class="tech-section">
<h1>Web Server</h1>
<p>A web server handles Hypertext Transfer Protocol (HTTP) requests from
clients. The client is usually a browser such as Internet Explorer, Firefox,
and Chrome, but it can also be a headless browser like
<a class="reference external" href="http://phantomjs.org/">phantomjs</a> (commonly used for testing), a
commandline utility like wget or curl, or a web crawler. The web server
processes the request and sends a status code and depending on the
status code a content response as well.</p>
<p>In a simple case, the client will request a static asset such as a picture
or JavaScript file. The file sits on the file system in a location the
web server is authorized to access and the web server sends the file
to the client with a 200 status code. If the client already requested the
file and the file has not changed, the web server will pass back a 304
"Not modified" response indicating the client already has the latest version
of that file.</p>
<img alt="Web server and web browser request-response cycles" class="technical-diagram" src="../img/web-browser-server-requests.png" />
<p>A web server sends files to a web browser based on the web browser's
request. In the first request, the browser accessed the
"www.fullstackpython.com"
address and the server responded with the index.html HTML-formatted file.
That HTML file contained references to other files, such as style.css and
script.js that the browser then requested from the server.</p>
<p>Sending static assets (such as CSS and JavaScript files) can eat up a
large amount of bandwidth which is why using a Content Delivery Network
(CDN) is important when possible (see the content delivery network
section for a more detailed explanation).</p>
<div class="section" id="web-server-resources">
<h2>Web Server Resources</h2>
<p><a class="reference external" href="http://library.linode.com/web-servers/apache/mod-wsgi/ubuntu-10.04-lucid">Apache and mod_wsgi on Ubuntu 10.04</a></p>
<p><a class="reference external" href="http://articles.slicehost.com/nginx">Nginx web server tutorials</a></p>
<p><a class="reference external" href="http://carrot.is/coding/nginx_introduction">Nginx for Developers: An Introduction</a></p>
<p><a class="reference external" href="http://tautt.com/best-nginx-configuration-for-security/">Nginx security configuration example</a></p>
<p><a class="reference external" href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">HTTP Status Codes</a></p>
<p><a class="reference external" href="http://blog.zachorr.com/nginx-setup/">Battle ready Nginx - an optimization guide</a></p>
</div>
</section>
<section id="database" class="tech-section">
<h1>Database</h1>
<p>A database is an abstraction on top of an operating system's file system to
ease creating, reading, updating, and deleting persistent data. The
database storage abstraction most commonly used in Python web development is
sets of relational tables. Alternative storage abstractions are explained in
the NoSQL section.</p>
<p>Relational databases store all data in a series of tables. Interconnections
between the tables are specified as <em>foreign keys</em>.</p>
<p>Databases storage implementations vary in complexity. SQLite, a database
included with Python, creates a single file for all data per database.
Other databases such as Oracle, PostgreSQL, and MySQL have more complicated
persistence schemes while offering additional advanced features that are
useful for web application data storage.</p>
<p><a class="reference external" href="http://www.postgresql.org/">PostgreSQL</a> and
<a class="reference external" href="http://www.mysql.com/">MySQL</a> are two of the most common open source
databases.</p>
<p><a class="reference external" href="http://www.sqlite.org/">SQLite</a> is a database that is stored in a single
file on disk. SQLite is built into Python but is only built for access
by a single connection at a time.</p>
<div class="section" id="database-third-party-services">
<h2>Database third-party services</h2>
<p>Numerous companies run scalable database servers as a hosted service.
Depending on the provider, there can be several advantages to using a
hosted database third-party service:</p>
<ol class="arabic simple">
<li>automated backups and recovery</li>
<li>tightened security configurations</li>
<li>easy vertical scaling</li>
</ol>
<p><a class="reference external" href="http://aws.amazon.com/rds/">Amazon Relational Database Service (RDS)</a>
provides pre-configured MySQL and PostgreSQL instances. The instances can
be scaled to larger or smaller configurations based on storage and performance
needs.</p>
</div>
<div class="section" id="database-connections-with-python">
<h2>Database connections with Python</h2>
<p>To work with a relational database using Python, you need to use a code
library. The most common libraries for relational databases are:</p>
<p><a class="reference external" href="http://initd.org/psycopg/">psycopg2</a> for PostgreSQL</p>
<p><a class="reference external" href="https://pypi.python.org/pypi/MySQL-python/1.2.4">MySQLdb</a> for MySQL</p>
<p><a class="reference external" href="http://cx-oracle.sourceforge.net/">cx_Oracle</a> for Oracle</p>
<p>SQLite support is built into Python 2.7+ and therefore a separate library
is not necessary.</p>
</div>
<div class="section" id="database-resources">
<h2>Database resources</h2>
<p><a class="reference external" href="http://db-engines.com/en/ranking">DB-Engines</a> ranks the most popular
database management systems.</p>
<p><a class="reference external" href="http://postgresweekly.com/">PostgreSQL Weekly</a> is a weekly newsletter of
PostgreSQL content from around the web.</p>
</div>
</section>
<section id="wsgi-server" class="tech-section">
<h1>WSGI Server</h1>
<p>A <a class="reference external" href="http://wsgi.readthedocs.org/en/latest/">Web Server Gateway Interface</a>
(WSGI) server implements the web server side of the WSGI interface for
running Python web applications. The WSGI standard v1.0 is specified in
<a class="reference external" href="http://www.python.org/dev/peps/pep-0333/">PEP 0333</a>. As of September 2010,
WSGI v1.0 is superseded by
<a class="reference external" href="http://www.python.org/dev/peps/pep-3333/">PEP 3333</a>, which defines the
v1.0.1 WSGI standard.</p>
<img alt="WSGI Server <-> Web server <-> Browser" class="technical-diagram" src="../img/web-browser-server-wsgi.png" />
<p>Requests from the browser that are not for static assets (this is specified
in the web server's configuration which requests are for static assets and
which are not) are passed to the WSGI server. Once the request is
processed and generated by the WSGI server, the response is passed
back through the web server and onto the browser.</p>
<div class="section" id="wsgi-resources">
<h2>WSGI Resources</h2>
<p><a class="reference external" href="http://www.python.org/dev/peps/pep-0333/">PEP 0333 WSGI v1.0</a>
and
<a class="reference external" href="http://www.python.org/dev/peps/pep-3333/">PEP 3333 WSGI v1.0.1</a>
specifications.</p>
<p><a class="reference external" href="http://gunicorn.org/">Green Unicorn</a>,
<a class="reference external" href="http://code.google.com/p/modwsgi/">mod_wsgi</a>,
<a class="reference external" href="https://github.com/unbit/uwsgi-docs">uWSGI</a>, and
<a class="reference external" href="http://www.gevent.org/">gevent</a> are common WSGI server implementations.</p>
<p><a class="reference external" href="http://www.apreche.net/complete-single-server-django-stack-tutorial/">Complete single server Django stack tutorial</a> is thorough and informative for
non-paas hosting choices.</p>
</div>
</section>
<section id="web-framework" class="tech-section">
<h1>Web Framework</h1>
<p>A web application framework is a collection of libraries that
provide functionality to accomplish common operations for the web. These
common operations include:</p>
<ol class="arabic simple">
<li>URL routing</li>
<li>HTML, XML, JSON, and other output format templating</li>
<li>Database manipulation</li>
<li>Security against Cross-site request forgery (CSRF) and other attacks</li>
</ol>
<p>Not all web frameworks include functionality for all of the above
functionality. Frameworks fall somewhere between simply executing a
single use case and attempting to be everything to every developer with
increased complexity.</p>
<p>For example, the Django web application framework includes an
Object-Relational Mapping (ORM) layer that abstracts relational database
read, write, query, and delete operations. However, Django's ORM
does not work (without modification) on non-relational databases such
<a class="reference external" href="http://www.mongodb.org/">MongoDB</a> and <a class="reference external" href="http://docs.basho.com/">Riak</a>.
Other web frameworks such as Flask and Pyramid are generally easier to
use with non-relational databases by incorporating external Python libraries.</p>
<div class="section" id="web-framework-monitoring">
<h2>Web Framework Monitoring</h2>
<p>Logging is a common mechanism for monitoring web applications written with a
web framework. Runtime exceptions that prevent code from running are
important to log to investigate and fix the source of the problems.
Informational and debugging logging also helps to understand how the
application is performing even if code is working as intended.</p>
<p>Logging is often grouped into several categories:</p>
<ol class="arabic simple">
<li>Information</li>
<li>Debug</li>
<li>Warning</li>
<li>Error</li>
</ol>
<p>Logging errors that occur while a web framework is running is crucial to
understanding how your application is performing.
<a class="reference external" href="http://raven.readthedocs.org/en/latest/">Raven</a> is a Python client for the
<a class="reference external" href="https://github.com/getsentry/sentry">Sentry</a> exception logging and
aggregation application. Raven provides the way to send exceptions to
Sentry, which should be deployed on a separate server from your production
infrastructure. Raven can also be used by Python scripts to send other
log data to Sentry for aggregation. Sentry provides a clean web application
interface for viewing the exceptions. Sentry can also be configured with a
mail plugin to send emails when exceptions occur.</p>
</div>
<div class="section" id="web-framework-resources">
<h2>Web Framework Resources</h2>
<p><a class="reference external" href="http://www.djangoproject.com/">Django</a>,
<a class="reference external" href="http://flask.pocoo.org/">Flask</a>,
<a class="reference external" href="http://bottlepy.org/docs/dev/">Bottle</a>,
<a class="reference external" href="http://www.pylonsproject.org/">Pyramid</a>, and
<a class="reference external" href="http://webpy.org/">web.py</a> are the most common Python web frameworks.</p>
</div>
</section>
<section id="static-content" class="tech-section">
<h1>Static Content</h1>
<p>Some content on a website does not change and therefore should be served
up either directly through the web server or a content delivery network (CDN).
Examples include JavaScript, image, and CSS files.</p>
<p>A CDN is a third party that serves your static files.
<a class="reference external" href="http://aws.amazon.com/cloudfront/">Amazon CloudFront</a>,
<a class="reference external" href="http://www.akamai.com/">Akamai</a>, and
<a class="reference external" href="http://www.rackspace.com/cloud/public/files/">Rackspace Cloud Files</a>
are examples of CDNs. The purpose of a CDN is to remove the load of static
file requests from web servers that are handling dynamic web content. For
example, if you have an nginx server that handles both static files and
acts as a front for a Green Unicorn WSGI server on a 512 megabyte
virtual private server, the nginx server will run into resource
constraints under heavy traffic. A CDN can remove the need to serve static
assets from that nginx server so it can purely act as a pass through for
requests to the Green Unicorn WSGI server.</p>
<p>CDNs distribute request load globally by using data centers in different
locations.</p>
<div class="section" id="static-content-resources">
<h2>Static Content Resources</h2>
<p><a class="reference external" href="http://blog.doismellburning.co.uk/2012/07/14/using-amazon-s3-to-host-your-django-static-files/">Using Amazon S3 to host your Django static files</a></p>
<p><a class="reference external" href="http://www.hanselman.com/blog/CDNsFailButYourScriptsDontHaveToFallbackFromCDNToLocalJQuery.aspx">CDNs fail, but your scripts don't have to</a></p>
<p><a class="reference external" href="http://django-storages.readthedocs.org/en/latest/">django-storages</a> is
a Django library for managing static and media files on services such as
Amazon S3 and other content delivery networks.</p>
</div>
</section>
<section id="website-security" class="tech-section">
<h1>Website Security</h1>
<p>Website security must be incorporated into building every level of the web
stack. However, I include a separate section here for topics that deserve
a comprehensive review such as firewalls, SSL certificates, and public key
authorization.</p>
<div class="section" id="key-terms">
<h2>Key Terms</h2>
<p><em>Authorization</em> - specifying access rights and permissions to server and
application resources. For example, a non-logged in user can view a landing
page but only a logged in user can access an application's "account" screen.</p>
</div>
<div class="section" id="website-security-resources">
<h2>Website Security Resources</h2>
<p><a class="reference external" href="http://spenserj.com/blog/2013/07/15/securing-a-linux-server/">Securing a Linux Server</a></p>
<p><a class="reference external" href="http://arstechnica.com/security/2013/02/securing-your-website-a-tough-job-but-someones-got-to-do-it/">Securing Your Website</a></p>
<p><a class="reference external" href="http://blog.hartleybrody.com/https-certificates/">How HTTPS Secures Connections: What Every Web Dev Should Know</a></p>
</div>
</section>
<section id="caching" class="tech-section">
<h1>Caching</h1>
<p>Caching can reduce load on servers by storing the results of common
operations and serving the precomputed answers to clients.</p>
<p>For example, instead of retrieving data from database tables that rarely
change, you can store the values in-memory. Retrieving values from an
in-memory location is far faster than retrieving them from a database (which
stores them on a persistent disk like a hard drive). When the cached values
change the system can invalidate the cache and re-retrieve the updated values
for future requests.</p>
<p>A cache can be created for multiple layers of the stack.</p>
<div class="section" id="caching-resources">
<h2>Caching Resources</h2>
<p><a class="reference external" href="http://memcached.org/">memcached</a> is a common in-memory caching system.</p>
<p><a class="reference external" href="http://redis.io/">Redis</a> is a key-value in-memory data store that can
easily be configured for caching with libraries such as
<a class="reference external" href="https://github.com/sebleier/django-redis-cache">django-redis-cache</a>.</p>
</div>
</section>
<section id="web-analytics" class="tech-section">
<h1>Web Analytics</h1>
<p>Web analytics involves collecting and analyzing data generated by visitors'
web browsers while they are viewing webpages. The resulting data provides
insight into how visitors use the site.</p>
<div class="section" id="web-analytics-third-party-services">
<h2>Web Analytics Third Party Services</h2>
<p><a class="reference external" href="http://www.google.com/analytics/">Google Analytics</a> is a widely used
free analytics tool for website traffic.</p>
<p><a class="reference external" href="https://mixpanel.com/">MixPanel</a>'s analytics platform focuses on mobile
and sales funnel metrics. A developer builds what data points need to be
collected into the server side or client side code. MixPanel captures that
data and provides metrics and visualizations based on the data.</p>
<p><a class="reference external" href="https://www.kissmetrics.com/">KISSmetrics</a>' analytics provides context
for who is visiting a website and what actions they are taking while on
the site.</p>
<p><a class="reference external" href="http://www.crazyegg.com/">CrazyEgg</a> is tool for understanding a
user's focus while using a website based on heatmaps generated from mouse
movements.</p>
</div>
<div class="section" id="web-analytics-resources">
<h2>Web Analytics Resources</h2>
<p><a class="reference external" href="http://blog.arkency.com/2012/12/google-analytics-for-developers/">Google Analytics for Developers</a></p>
</div>
</section>
<section id="monitoring" class="tech-section">
<h1>Monitoring</h1>
<p>Capturing and analyzing data about your production environment is critical
to proactively deal with stability, performance, and errors in a web
application.</p>
<p>There are several important resources to monitor on the operating system
and network level of a web stack.</p>
<ol class="arabic simple">
<li>CPU utilization</li>
<li>Memory utilization</li>
<li>Persistence storage consumed versus free</li>
<li>Network bandwidth and latency</li>
</ol>
<p>Application level monitoring encompasses several aspects. The amount of time
and resources dedicated to each aspect will vary based on whether an
application is read-heavy, write-heavy, or subject to rapid swings in traffic.</p>
<ol class="arabic simple">
<li>Application warnings and errors (500-level HTTP errors)</li>
<li>Application code performance</li>
<li>Template rendering time</li>
<li>Browser rendering time for the application</li>
<li>Database querying performance</li>
</ol>
<div class="section" id="monitoring-third-party-services">
<h2>Monitoring Third Party Services</h2>
<p><a class="reference external" href="http://newrelic.com/">New Relic</a>. Application and database monitoring as
well as plug ins for capturing and analyzing additional data about tools in
your stack.</p>
<p><a class="reference external" href="http://copperegg.com/">CopperEgg</a> is lower-level monitoring on server and
infrastructure. It's popular with DevOps shops that are making changes to
their production environments and want immediate feedback on the results
of those modifications.</p>
<p><a class="reference external" href="http://status.io/">Status.io</a> focuses on uptime and response metrics
transparency for your users.</p>
<p><a class="reference external" href="http://www.pagerduty.com/">PagerDuty</a> alerts a designated person or group
if there are stability, performance, or uptime issues with an application.</p>
</div>
<div class="section" id="open-source-projects">
<h2>Open Source Projects</h2>
<p><a class="reference external" href="https://github.com/etsy/statsd/">statsd</a> is a node.js network daemon that
listens for metrics and aggregates them for transfer into another service
such as Graphite.</p>
<p><a class="reference external" href="https://graphite.readthedocs.org/en/latest/overview.html">Graphite</a> stores
time-series data and displays them in graphs through a web application.</p>
</div>
<div class="section" id="monitoring-resources">
<h2>Monitoring Resources</h2>
<p><a class="reference external" href="http://www.paperplanes.de/2011/1/5/the_virtues_of_monitoring.html">The Virtues of Monitoring</a></p>
<p><a class="reference external" href="http://blog.docker.io/2013/07/effortless-monitoring-with-collectd-graphite-and-docker/">Effortless Monitoring with collectd, Graphite, and Docker</a></p>
</div>
</section>
<section id="no-sql-datastore" class="tech-section">
<h1>NoSQL Data Stores</h1>
<p>Relational databases store the vast majority of web application
persistent data. However, there are several alternative classifications of
storage representations.</p>
<ol class="arabic simple">
<li>Key-value pair</li>
<li>Document-oriented</li>
<li>Column-family table</li>
<li>Graph</li>
</ol>
<p>These persistent data storage representations are commonly used to augment,
rather than completely replace, relational databases.</p>
<div class="section" id="document-oriented">
<h2>Document-Oriented</h2>
<p>A document-oriented database provides a semi-structured representation for
nested data.</p>
</div>
<div class="section" id="key-value-pair">
<h2>Key-Value Pair</h2>
<p>Key-value pair data stores are based
on <a class="reference external" href="http://en.wikipedia.org/wiki/Hash_table">hash map</a> data structures.</p>
</div>
<div class="section" id="graph">
<h2>Graph</h2>
<p>A graph database represent and store data in three aspects: nodes, edges,
and properties.</p>
<p>A <em>node</em> is an entity, such as a person or business.</p>
<p>An <em>edge</em> is the relationship between two entities. For example, an
edge could represent that a node for a person entity is an employee of a
business entity.</p>
<p>A <em>property</em> represents information about nodes. For example, an entity
representing a person could have a property of "female" or "male".</p>
<p><a class="reference external" href="http://www.neo4j.org/">Neo4j</a> is one of the most widely used graph
databases and runs on the Java Virtual Machine stack.</p>
</div>
<div class="section" id="nosql-third-party-services">
<h2>NoSQL third-party services</h2>
<p><a class="reference external" href="http://www.mongohq.com/home">MongoHQ</a> provides MongoDB as a service. It's
easy to set up with either a standard LAMP stack or on Heroku.</p>
</div>
<div class="section" id="nosql-data-stores-resources">
<h2>NoSQL data stores resources</h2>
<p><a class="reference external" href="http://www.nosqlweekly.com/">NoSQL Weekly</a> is a free curated email
newsletter that aggregates articles, tutorials, and videos about
non-relational data stores.</p>
</div>
</section>
<section id="about-author" class="tech-section">
<h1>About</h1>
<p>I'm <a class="reference external" href="http://www.mattmakai.com/">Matt Makai</a> and I solve problems by
developing software. I often use Python and occasionally work with
languages in the Java Virtual Machine (JVM) stack.</p>
<p>In 2013 I traveled to 30 cities in the United States to visit with
tech companies, attend and speak at tech meetups, and blog from an experienced
software developer's perspective about the current state of technology in the
United States.</p>
<p>Read more about my trip on my
<a class="reference external" href="http://www.codingacrossamerica.com/">Coding Across America</a> website.</p>
<p>If there are typos, inaccurate statements, or general areas for improvement
that you see in this guide, please issue a
<a class="reference external" href="https://github.com/makaimc/fullstackpython.github.com/pull/new/gh-pages">pull request on this website's Github repository</a>. Thank you!</p>
<p>You can reach me by email at <a class="reference external" href="mailto:matthew.makai@gmail.com">matthew.makai@gmail.com</a> or send me a
<a class="reference external" href="https://twitter.com/makaimc">message me on Twitter</a>.</p>
</section>
<section id="change-log" class="tech-section">
<h1>Change Log</h1>
<div class="section" id="id1">
<h2>2013</h2>
<p>Dec: Changed CDN section to static content. Transitioning diagrams from
Paper app drawings to Balsamiq mockups exported to PNG files. Added Python
database connectors to database section.</p>
<p>Nov: Modified color scheme. Updated caching and introduction section,
added section on NoSQL data stores.</p>
<p>Oct: Creating separate monitoring section.</p>
<p>Aug: Adding more resources for web servers and other categories.</p>
<p>Jun: Updating styling, adding new content, switching around sections.</p>
<p>Jan: Fleshed out web server, OS, and server sections, particularly IaaS
and PaaS topics, added initial "hand drawn" diagram placeholders for better
diagrams later.</p>
</div>
<div class="section" id="id2">
<h2>2012</h2>
<p>Dec: Initial incomplete release on fullstackpython.com, created
introduction, CDN, web frameworks, and database sections with stubs for
other areas.</p>
</div>
</section>
</div>
</div>
<hr/>
<div class="footer pull-right">
<a href="http://www.mattmakai.com/" class="underline">Matt Makai</a> 2013
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="/js/bootstrap.min.js"></script>
</body>
</html>