Project

General

Profile

MAUSDjangoApache » History » Version 17

Jackson, Mike, 06 March 2012 14:26

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