-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdjango-deployment.html
More file actions
150 lines (150 loc) · 7.68 KB
/
Copy pathdjango-deployment.html
File metadata and controls
150 lines (150 loc) · 7.68 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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>SLP: Django: Deployment</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
/* The extra [class] is a hack that increases specificity enough to
override a similar rule in reveal.js */
ul.task-list[class]{list-style: none;}
ul.task-list li input[type="checkbox"] {
font-size: inherit;
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<link rel="stylesheet" href="../markdown.css" />
</head>
<body>
<main>
<h1 id="slp-django-deployment">SLP: Django: Deployment</h1>
<p><a href="index.html">Go up to the main SLP documents page</a> (<a
href="index.md">md</a>)</p>
<p>This page is for how to <em>deploy</em> your Django apps on an Ubuntu
server – specifically, Ubuntu 14.04. This is not needed for your local
development, as you can run <code>python manage.py runserver</code> to
test it out locally, or test it out on the course server provided.
However, if you <strong><em>do</em></strong> want to get it running
through Apache, read on…</p>
<h2 id="installing-packages">Installing packages</h2>
<p>To install Django on your own system, under Ubuntu 14.04, just enter
<code>sudo apt-get install python-django python-mysqldb</code>, and it
will do the rest. As of the writing of this tutorial (Aug 31, 2014),
this installs Python version 2.7.6 and Django version 1.6.1.</p>
<p>About installing the wsgi module, and for python 2/3…</p>
<h2 id="configuring-apache">Configuring Apache</h2>
<p>This assumes Apache version 2.4; if you are using version 2.2, then
the syntax for this is quite different (some of those differences are
described below).</p>
<p>This part specifically deals with configuring <em>multiple</em>
django apps on the same web server, as that is what is needed for this
course. For <em>each</em> django app, the following 8 lines need to be
added to <code>/etc/apache2/sites-available/000-default.conf</code>,
right above the end <code></VirtualHost></code> line:</p>
<pre><code>Alias /django/user/static /home/slp/user/mysite/static
<Directory /home/slp/user/mysite/static>
Require all granted
</Directory>
WSGIScriptAlias /django/user /home/slp/user/mysite/mysite/wsgi.py
WSGIDaemonProcess user python-path=/home/slp/user/mysite
<Location /django/user>
WSGIProcessGroup user
</Location>
<Directory /home/slp/user/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory></code></pre>
<p>A bunch of notes:</p>
<ul>
<li><code>user</code> is the username of whomever is running the Django
app, and <code>mysite</code> is the name of the project.</li>
<li>The first four lines deal with the <code>static</code> directory –
since there are multiple Django apps, there will be multiple static
directories. See the <a href="django-getting-started.html">Django
getting started</a> (<a href="django-getting-started.md">md</a>) page
for how to configure a given Django project to use the correct static
directory. Using a static directory that is directly below the main
project directory (as opposed to in an app directory) will likely
require one to run <code>python manage.py collectstatic</code>, as
described on the <a href="django-getting-started.html">Django getting
started</a> (<a href="django-getting-started.md">md</a>) page.</li>
<li>The <a
href="https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIScriptAlias">WSGIScriptAlias</a>
line indicates the URL (after the server) links to the particular Django
app. As we want them in sub-directories, we have the URL part be
<code>/django/user</code>. Note that we do <em>not</em> have to create
the <code>/django/</code> directory in the HTML document root (likely
<code>/var/www/html</code>).</li>
<li>The <a
href="https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIProcessGroup">WSGIProcessGroup</a>
creates a process group, which allows one (or more) apps to run as a
specific user. The <code>user</code> part of that line needs to be a
valid user name on the system</li>
<li>The <a
href="https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess">WSGIDaemonProcess</a>
line indicates that this particular Django app will run under the
<code>user</code> user, and the path to the django app is provided. The
<code>user</code> part of that line needs to match the one in the
WSGIProcessGroup.</li>
<li>The <code>Require all granted</code> line allows the viewing of the
Django app. If you are using Apache 2.2 (or earlier), replace
<code>Require all granted</code> with two lines:
<code>Order deny,allow</code> and then <code>Allow from all</code>.</li>
<li>If using virtualenv, such as described in the <a
href="virtualenv-setup.html">Python’s virtualenv setup</a> (<a
href="virtualenv-setup.md">md</a>) page, then the
<code>WSGIDaemonProcess</code> line will differ, and will look like:
<code>WSGIDaemonProcess user python-path=/home/slp/user/mysite python-home=/path/to/virtualenv/lib/python3.5/site-packages</code>
<ul>
<li>Note that the virtualenv directory should end in
<code>lib/python3.5/site-packages</code></li>
<li>The wsgi-admin.cpp program, described below, creates this with the
<code>-path</code> flag</li>
</ul></li>
</ul>
<p>Sine there are going to be many users in a system, it will be easier
to put this in a separate file (say, called
<code>/etc/apache2/django.conf</code>), and insert a
<code>Include django.conf</code> line in
<code>/etc/apache2/sites-available/000-default.conf</code> (also right
above the <code></VirtualHost></code> line).</p>
<p>If you are enabling these through SSL, there is a slight change. When
Apache restarts (or reloads), the default (i.e., non-SSL) site is
brought up first, followed by the SSL site. As the daemon process has
already been declared in 000-default.conf (for the non-SSL site), it
will cause an error to declare it again for the SSL site. Thus, the SSL
version should not have the <code>WSGIDaemonProcess</code> line; all the
other lines remain the same. This implies that the file included from
default-ssl.conf will be a different one than the one included from
000-default.ssl.</p>
<h2 id="managing-multiple-users">Managing multiple users</h2>
<p>The issue with the WSGI module is that if <strong>any one</strong> of
the WSGI files it is configured with does not exist, then all of the
WSGI modules don’t work. This means you can’t pre-generate the list of
WSGI links for a class, as none of them will work until everybody has it
set up, which is not viable.</p>
<p>To work around this, you can see the <a
href="../utils/wsgi-admin/wsgi-admin.cpp.html">wsgi-admin.cpp</a> (<a
href="../utils/wsgi-admin/wsgi-admin.cpp">src</a>), which will allow
students to register and remove their WSGI apps. It will update the
necessary Apache configuration files, and then reload the web server. It
can also be used just to do the regeneration and reloading – and if a
file is not found (or is not valid), then it is excluded from the
configuration files. Installation is tricky, but the process is
described in the comments at the top of that file. The directions for
how to use it can be found on the <a
href="django-getting-started.html">Django getting started</a> (<a
href="django-getting-started.md">md</a>) page.</p>
</main>
</body>
</html>