Project

General

Profile

MAUSDjangoApache » History » Version 7

Jackson, Mike, 10 February 2012 14:00

1 1 Jackson, Mike
h1. Deploying Django under Apache
2
3
{{>toc}}
4
5
h2. Apache 2.2 deployment
6
7
Apache 2.2 (http://httpd.apache.org/) is an HTTP server.
8
9
For full deployment information, see
10
11
* http://httpd.apache.org/docs/current/install.html
12
13
Following the "overview for the impatient"...
14
15
* Log in as root user,
16
<pre>
17
$ sudo su -
18
</pre>
19
* Download,
20
<pre>
21
$ lynx http://httpd.apache.org/download.cgi
22
</pre>
23
** Alternatively:
24
<pre>
25
$ wget http://mirrors.ukfast.co.uk/sites/ftp.apache.org//httpd/httpd-2.2.22.tar.gz
26
</pre>
27
* Extract,
28
<pre>
29
$ gzip -d httpd-2.2.22.tar.gz 
30
$ tar xvf httpd-2.2.22.tar
31
$ cd httpd-2.2.22
32
</pre>
33
* Configure,
34
<pre>
35
$ ./configure
36
</pre>
37
** This uses a default deployment directory of @/usr/local/apache2@.
38
* Compile,
39
<pre>
40
$ make 
41
</pre>
42
* Install,
43
<pre>
44
$ make install 
45
</pre>
46
* Start-up server,
47
<pre>
48
$ /usr/local/apache2/bin/apachectl -k start
49
</pre>
50
** If you get an error like,
51
<pre>
52
(98)Address already in use: make_sock: could not bind to address [::]:80
53
(98)Address already in use: make_sock: could not bind to address 0.0.0.0:80
54
no listening sockets available, shutting down
55
Unable to open logs
56
</pre>
57
** Then edit @/usr/local/apache2/conf/httpd.conf@,
58
<pre>
59
$ xemacs /usr/local/apache2/conf/httpd.conf 
60
</pre>
61
** and change,
62
<pre>
63
Listen 80 
64
</pre>
65
** to a port that is not in use e.g.
66
<pre>
67
Listen 9090
68
</pre>
69
** and start Apache,
70
<pre>
71
$ /usr/local/apache2/bin/apachectl -k start
72
</pre>
73
* Browse to http://localhost:8080 (or whatever port you specified above) and you should see @It works!"@
74
75
Useful files:
76
77
* Configuration: @/usr/local/apache2/conf/httpd.conf@
78
* Error log: @/usr/local/apache2/logs/error_log@
79
80
h2. mod_wsgi deployment
81
82
mod_wsgi (http://code.google.com/p/modwsgi/) is an Apache module allowing the hosting of Python applications which suppport Python's WSGI (Web Server Gateway Interface).
83
84
For full deployment information, see
85
86
* http://code.google.com/p/modwsgi/wiki/InstallationInstructions
87
* http://code.google.com/p/modwsgi/wiki/InstallationIssues
88
89
Now, following the quick installation guide (http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide),
90
91
* Download latest release (July 2010)
92
<pre>
93
$ wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
94
</pre>
95
* Extract,
96
<pre>
97
$ gunzip mod_wsgi-3.3.tar.gz 
98
$ tar -xf mod_wsgi-3.3.tar
99
$ cd mod_wsgi-3.3
100
</pre>
101
* Set Python library path, if using a non-default Python distribution. The author uses MAUS Python, so set:
102
<pre>
103
$ export LD_LIBRARY_PATH=/home/michaelj/maus-bzr/maus/third_party/install/lib/
104
</pre>
105
* Configure,
106
<pre>
107
$ ./configure
108
</pre>
109
** If you get an error like:
110
<pre>
111
/home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7: error while loading 
112
shared libraries: libpython2.7.so.1.0: cannot open shared object file: 
113
No such file or directory
114
</pre>
115
** Then set the @LD_LIBRARY_PATH@ as above.
116
** If using a non-default Python distribution, then add a @--with-python@ flag pointing to the Python executable. The author uses MAUS Python so set:
117
<pre>
118
   --with-python=/home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7
119
</pre>
120
** If you get an error like:
121
<pre>
122
checking for apxs2... no
123
checking for apxs... no
124
checking Apache version... ./configure: line 1704: apxs: command not found
125
./configure: line 1704: apxs: command not found
126
./configure: line 1705: apxs: command not found
127
./configure: line 1708: /: is a directory
128
129
checking for python... /home/michaelj/maus-bzr/maus/third_party/install/bin/python
130
./configure: line 1877: apxs: command not found
131
configure: creating ./config.status
132
config.status: error: cannot find input file: Makefile.in
133
</pre>
134
** Then explicitly provide the path to Apache's @apxs@ command,
135
<pre>
136
$ ./configure --with-apxs=/usr/local/apache2/bin/apxs
137
</pre>
138
** Combining the above gives,
139
<pre>
140
$ ./configure --with-apxs=/usr/local/apache2/bin/apxs
141
  --with-python=/home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7
142
</pre>
143
* Compile,
144
<pre>
145
$ make
146
</pre>
147
* Install,
148
<pre>
149
$ make install
150
</pre>
151
* A @mod_wsgi.so@ library is installed in @/usr/local/apache2/modules@.
152
* Configure Apache, edit @/usr/local/apache2/conf/httpd.conf@,
153
<pre>
154
$ xemacs /usr/local/apache2/conf/httpd.conf 
155
</pre>
156
* and add,
157
<pre>
158
LoadModule wsgi_module modules/mod_wsgi.so
159
</pre>
160
* If you use a non-standard Python location and set @LD_LIBRARY_PATH@ above, then edit @/usr/local/apache2/bin/envvars@ and add a line to set the Python library path e.g.
161
<pre>
162
LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH" 
163
</pre>
164
* Restart Apache,
165
<pre>
166
$ /usr/local/apache2/bin/apachectl restart
167
</pre>
168
* If all is well you should see, in @/usr/local/apache2/logs/error_log@ a line like,
169
<pre>
170
[Tue Feb 07 18:50:37 2012] [notice] Apache/2.2.22 (Unix) mod_wsgi/3.3 
171
Python/2.7.2 configured -- resuming normal operations
172
</pre>
173
* If you see an error like,
174
<pre>
175
[Tue Feb 07 18:46:40 2012] [notice] SIGHUP received.  Attempting to restart
176
httpd: Syntax error on line 55 of /usr/local/apache2/conf/httpd.conf: Cannot 
177
load /usr/local/apache2/modules/mod_wsgi.so into server: 
178
libpython2.7.so.1.0: cannot open shared object file: No such file or directory
179
</pre>
180
** then you have to set the @LD_LIBRARY_PATH@ as above.
181
* Clean up,
182
<pre>
183
$ make clean
184
</pre>
185
186
To check your installation (following http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation),
187
188
* List server information,
189
<pre>
190
$ /usr/local/apache2/bin/httpd  -V
191
Server version: Apache/2.2.22 (Unix)
192
...
193
Server MPM:     Prefork
194
  threaded:     no
195
    forked:     yes (variable process count)
196
Server compiled with....
197
 -D APACHE_MPM_DIR="server/mpm/prefork"
198
...
199
</pre>
200
** Important information is the version and prefork settings,
201
* List compiled-in modules,
202
<pre>
203
$ /usr/local/apache2/bin/httpd  -l
204
Compiled in modules:
205
...
206
  prefork.c
207
</pre>
208
** You should see @prefork.c@.
209
* List dynamically-loaded modules,
210
<pre>
211
$ /usr/local/apache2/bin/httpd  -M
212
 mpm_prefork_module (static)
213
 ...
214
 wsgi_module (shared)
215
 ...
216
</pre>
217
** You should see @wsgi_module@.
218
** If this fails with,
219
<pre>
220
httpd: Syntax error on line 55 of /usr/local/apache2/conf/httpd.conf: 
221
Cannot load /usr/local/apache2/modules/mod_wsgi.so into server: 
222
libpython2.7.so.1.0: cannot open shared object file: No such file or directory
223
</pre>
224
** Then set your @LD_LIBRARY_PATH@ as above and try again,
225
<pre>
226
$ export LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH"
227
$ /usr/local/apache2/bin/httpd -M
228
Loaded Modules:
229
...
230
</pre>
231
* List modules linked to by @mod_wsgi@:
232
<pre>
233
$ ldd /usr/local/apache2/modules/mod_wsgi.so
234
...
235
libpython2.7.so.1.0 => /home/michaelj/maus-bzr/maus/third_party/install/lib/libpython2.7.so.1.0 
236
(0x00110000)
237
...
238
</pre>
239
** You should see the link to your Python library.
240 2 Jackson, Mike
241
h2. Deploying ImageMagick
242
243
The ImageMagick library is used to convert between image types in the MAUS web-front end.
244
245
To install,
246
247
* Log in as root user,
248
<pre>
249
$ sudo su -
250
</pre>
251
* Get and unpack ImageMagick,
252
<pre>
253
$ wget http://www.imagemagick.org/download/ImageMagick.tar.gz
254
$ gunzip ImageMagick.tar.gz
255
$ tar -xf ImageMagick.tar
256
$ cd ImageMagick-6.7.4-0/
257
</pre>
258
* Configure,
259
<pre>
260
$ ./configure
261
</pre>
262
* Make,
263
<pre>
264
$ make
265
</pre>
266
* Install,
267
<pre>
268
$ make install
269
</pre>
270
** This places libraries in @/usr/local/lib@.
271
* Cache the libraries,
272
<pre>
273
$ ldconfig /usr/local/lib
274
</pre>
275
** This avoids problems with libraries not being found by Python's magickwand library.
276
* Check,
277
<pre>
278
$ /usr/local/bin/convert -h
279
Version: ImageMagick 6.7.4-0 2011-12-13 Q16 http://www.imagemagick.org
280
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
281
Features: OpenMP    
282
</pre>
283
284
To install Python magickwand
285
286
* Setup the MAUS environment:
287
<pre>
288
$ source env.sh
289
</pre>
290
* Install,
291
<pre>
292
$ easy_install magickwand
293
</pre>
294
Check,
295
<pre>
296
$ python
297
$ import magickwand
298
$ from magickwand.image import Image
299
...
300
</pre>
301
* If you get an error like,
302
<pre>
303
Traceback (most recent call last):
304
  File "<stdin>", line 1, in <module>
305
  File "build/bdist.linux-i686/egg/magickwand/image.py", line 2, in <module>
306
  File "build/bdist.linux-i686/egg/magickwand/api/__init__.py", line 1, in <module>
307
    #
308
  File "build/bdist.linux-i686/egg/magickwand/api/lib.py", line 11, in <module>
309
  File "/home/michaelj/maus-bzr/maus/third_party/install/lib/python2.7/ctypes/__init__.py", 
310
  line 353, in __init__ self._handle = _dlopen(self._name, mode)
311
OSError: libMagickWand.so.5: cannot open shared object file: 
312
 No such file or directory
313
</pre>
314
* then run @ldconfig@ as described above.
315
316
h2. Deploying the MAUS Django web front-end
317
318
For more on Django deployment under Apache 2.2/mod_wsgi, see
319
320
* https://docs.djangoproject.com/en/1.3/howto/deployment/modwsgi/
321
* http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
322
323
* Check-out the MAUS Django code,
324
<pre>
325 5 Jackson, Mike
$ bzr branch -v lp:~michaelj-h/maus-apps/devel maus-apps
326 2 Jackson, Mike
</pre>
327 3 Jackson, Mike
* Set-up environment,
328
<pre>
329 6 Jackson, Mike
$ cd maus-apps
330 3 Jackson, Mike
$ source env.sh
331 4 Jackson, Mike
$ ./install.sh
332 1 Jackson, Mike
</pre>
333 7 Jackson, Mike
* Edit Django WSGI configuration file @apache/django.wsgi@
334 6 Jackson, Mike
** Change @/home/michaelj@ in the above to the location of your @maus-apps@ check-out.
335
* Change permissions on the @media@ and @apache@ directories, so the @apache@ user that Apache 2.2 runs under can read configuration and update images,
336 1 Jackson, Mike
<pre>
337 6 Jackson, Mike
$ chmod -R go+rwx apache/
338
$ chmod -R go+rwx media/
339 1 Jackson, Mike
</pre>
340 3 Jackson, Mike
* Copy a couple of sample images,
341
<pre>
342 6 Jackson, Mike
$ cp images/* media/raw
343 3 Jackson, Mike
</pre>
344
345 2 Jackson, Mike
* Log in as root user,
346
<pre>
347
$ sudo su -
348
</pre>
349
* Edit Apache 2.2 configuration,
350
<pre>
351
$ xemacs -nw /usr/local/apache2/conf/httpd.conf
352
</pre>
353
Add,
354
<pre>
355
WSGIScriptAlias / /home/michaelj/maus-apps/apache/django.wsgi
356
# Location of django.wsgi
357
<Directory /home/michaelj/maus-apps/apache>
358
Order deny,allow 
359
Allow from all 
360
</Directory>
361
</pre>
362
* where:
363
** @/@ specifies the root URL - so all URL requests will be directed to Django.
364
** @/...@ - location of WSGI file within the MAUS Django project.
365
* Change @/home/michaelj@ in the above to the location of your @maus-apps@ check-out.
366
* Edit Apache 2.2 environment, so it has access to the MAUS environment (in particular all the Python libraries that have been easy_installed):
367
<pre>
368
$ xemacs -nw /usr/local/apache2/bin/envvars
369
</pre>
370
* Add
371
<pre>
372
source /home/michaelj/maus-bzr/maus/env.sh 
373
</pre>
374
<pre>
375
$ xemacs -nw /usr/local/apache2/bin/envvars
376
</pre>
377
* Restart Apache 2.2,
378
<pre>
379
$ /usr/local/apache2/bin/apachectl restart
380
</pre>
381
* Test,
382
** Visit http://localhost:8080/maus/, remembering to change the port number if you use a custom port.