Project

General

Profile

MAUSDjangoApache » History » Version 14

Jackson, Mike, 06 March 2012 13:43

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