Project

General

Profile

MAUSDjangoApache » History » Version 13

Jackson, Mike, 06 March 2012 13:24

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 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:
102 1 Jackson, Mike
<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 13 Jackson, Mike
* 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 
161 12 Jackson, Mike
** Edit @/usr/local/apache2/bin/envvars@ and add a line to set the Python library path e.g.
162 1 Jackson, Mike
<pre>
163
LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH" 
164
</pre>
165 12 Jackson, Mike
** Ensure that the permissions in this path allow group and world read and execute permission (755) e.g.:
166
<pre>
167
$ ls -l /home
168
drwxr-xr-x 26 michaelj epcc 4096 Mar  6 10:08 michaelj
169
</pre>
170
** If not then set these,
171
<pre>
172
$ chmod go+rx /home/michaelj
173
</pre>
174 1 Jackson, Mike
* Restart Apache,
175
<pre>
176
$ /usr/local/apache2/bin/apachectl restart
177
</pre>
178
* If all is well you should see, in @/usr/local/apache2/logs/error_log@ a line like,
179
<pre>
180
[Tue Feb 07 18:50:37 2012] [notice] Apache/2.2.22 (Unix) mod_wsgi/3.3 
181
Python/2.7.2 configured -- resuming normal operations
182
</pre>
183
* If you see an error like,
184
<pre>
185
[Tue Feb 07 18:46:40 2012] [notice] SIGHUP received.  Attempting to restart
186
httpd: Syntax error on line 55 of /usr/local/apache2/conf/httpd.conf: Cannot 
187
load /usr/local/apache2/modules/mod_wsgi.so into server: 
188
libpython2.7.so.1.0: cannot open shared object file: No such file or directory
189
</pre>
190
** then you have to set the @LD_LIBRARY_PATH@ as above.
191 12 Jackson, Mike
* If you see an error like,
192
<pre>
193
Could not find platform independent libraries <prefix>
194
Could not find platform dependent libraries <exec_prefix>
195
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
196
ImportError: No module named site
197
</pre>
198
** then it is likely you did not set the file permissions for the directory in which Python is as described above.
199 1 Jackson, Mike
* Clean up,
200
<pre>
201
$ make clean
202
</pre>
203
204
To check your installation (following http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation),
205
206
* List server information,
207
<pre>
208
$ /usr/local/apache2/bin/httpd  -V
209
Server version: Apache/2.2.22 (Unix)
210
...
211
Server MPM:     Prefork
212
  threaded:     no
213
    forked:     yes (variable process count)
214
Server compiled with....
215
 -D APACHE_MPM_DIR="server/mpm/prefork"
216
...
217
</pre>
218
** Important information is the version and prefork settings,
219
* List compiled-in modules,
220
<pre>
221
$ /usr/local/apache2/bin/httpd  -l
222
Compiled in modules:
223
...
224
  prefork.c
225
</pre>
226
** You should see @prefork.c@.
227
* List dynamically-loaded modules,
228
<pre>
229
$ /usr/local/apache2/bin/httpd  -M
230
 mpm_prefork_module (static)
231
 ...
232
 wsgi_module (shared)
233
 ...
234
</pre>
235
** You should see @wsgi_module@.
236
** If this fails with,
237
<pre>
238
httpd: Syntax error on line 55 of /usr/local/apache2/conf/httpd.conf: 
239
Cannot load /usr/local/apache2/modules/mod_wsgi.so into server: 
240
libpython2.7.so.1.0: cannot open shared object file: No such file or directory
241
</pre>
242
** Then set your @LD_LIBRARY_PATH@ as above and try again,
243
<pre>
244
$ export LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH"
245
$ /usr/local/apache2/bin/httpd -M
246
Loaded Modules:
247
...
248
</pre>
249
* List modules linked to by @mod_wsgi@:
250
<pre>
251
$ ldd /usr/local/apache2/modules/mod_wsgi.so
252
...
253
libpython2.7.so.1.0 => /home/michaelj/maus-bzr/maus/third_party/install/lib/libpython2.7.so.1.0 
254
(0x00110000)
255
...
256
</pre>
257
** You should see the link to your Python library.
258 2 Jackson, Mike
259
h2. Deploying ImageMagick
260
261
The ImageMagick library is used to convert between image types in the MAUS web-front end.
262
263
To install,
264
265
* Log in as root user,
266
<pre>
267
$ sudo su -
268
</pre>
269
* Get and unpack ImageMagick,
270
<pre>
271
$ wget http://www.imagemagick.org/download/ImageMagick.tar.gz
272
$ gunzip ImageMagick.tar.gz
273
$ tar -xf ImageMagick.tar
274
$ cd ImageMagick-6.7.4-0/
275
</pre>
276
* Configure,
277
<pre>
278
$ ./configure
279
</pre>
280
* Make,
281
<pre>
282
$ make
283
</pre>
284
* Install,
285
<pre>
286
$ make install
287
</pre>
288
** This places libraries in @/usr/local/lib@.
289
* Cache the libraries,
290
<pre>
291
$ ldconfig /usr/local/lib
292
</pre>
293
** This avoids problems with libraries not being found by Python's magickwand library.
294
* Check,
295
<pre>
296
$ /usr/local/bin/convert -h
297
Version: ImageMagick 6.7.4-0 2011-12-13 Q16 http://www.imagemagick.org
298
Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC
299
Features: OpenMP    
300
</pre>
301
302
To install Python magickwand
303
304
* Setup the MAUS environment:
305
<pre>
306
$ source env.sh
307
</pre>
308
* Install,
309
<pre>
310
$ easy_install magickwand
311
</pre>
312
Check,
313
<pre>
314
$ python
315
$ import magickwand
316
$ from magickwand.image import Image
317
...
318
</pre>
319
* If you get an error like,
320
<pre>
321
Traceback (most recent call last):
322
  File "<stdin>", line 1, in <module>
323
  File "build/bdist.linux-i686/egg/magickwand/image.py", line 2, in <module>
324
  File "build/bdist.linux-i686/egg/magickwand/api/__init__.py", line 1, in <module>
325
    #
326
  File "build/bdist.linux-i686/egg/magickwand/api/lib.py", line 11, in <module>
327
  File "/home/michaelj/maus-bzr/maus/third_party/install/lib/python2.7/ctypes/__init__.py", 
328
  line 353, in __init__ self._handle = _dlopen(self._name, mode)
329
OSError: libMagickWand.so.5: cannot open shared object file: 
330
 No such file or directory
331
</pre>
332
* then run @ldconfig@ as described above.
333
334
h2. Deploying the MAUS Django web front-end
335
336
For more on Django deployment under Apache 2.2/mod_wsgi, see
337
338
* https://docs.djangoproject.com/en/1.3/howto/deployment/modwsgi/
339
* http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
340
341
* Check-out the MAUS Django code,
342
<pre>
343 5 Jackson, Mike
$ bzr branch -v lp:~michaelj-h/maus-apps/devel maus-apps
344 2 Jackson, Mike
</pre>
345 3 Jackson, Mike
* Set-up environment,
346
<pre>
347 6 Jackson, Mike
$ cd maus-apps
348 8 Jackson, Mike
$ ./configure
349 4 Jackson, Mike
$ ./install.sh
350 6 Jackson, Mike
</pre>
351 3 Jackson, Mike
* Copy a couple of sample images,
352
<pre>
353 6 Jackson, Mike
$ cp images/* media/raw
354 3 Jackson, Mike
</pre>
355
356 2 Jackson, Mike
* Log in as root user,
357
<pre>
358
$ sudo su -
359
</pre>
360
* Edit Apache 2.2 configuration,
361
<pre>
362
$ xemacs -nw /usr/local/apache2/conf/httpd.conf
363
</pre>
364 1 Jackson, Mike
Add,
365 2 Jackson, Mike
<pre>
366 8 Jackson, Mike
WSGIScriptAlias / ${MAUS_WEB_DIR}/apache/django.wsgi  
367
 
368
# Location of django.wsgi 
369
<Directory ${MAUS_WEB_DIR}/apache> 
370
Order deny,allow  
371
Allow from all  
372
</Directory> 
373 2 Jackson, Mike
</pre>
374
* where:
375 1 Jackson, Mike
** @/@ specifies the root URL - so all URL requests will be directed to Django.
376 2 Jackson, Mike
** @/...@ - location of WSGI file within the MAUS Django project.
377 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):
378 2 Jackson, Mike
<pre>
379
$ xemacs -nw /usr/local/apache2/bin/envvars
380
</pre>
381 11 Jackson, Mike
* Add, changing to use your own paths,
382 2 Jackson, Mike
<pre>
383
source /home/michaelj/maus-bzr/maus/env.sh 
384 1 Jackson, Mike
source /home/michaelj/maus-apps/env.sh
385 12 Jackson, Mike
</pre>
386
* Ensure that the permissions in to your paths allow group and world read and execute permission (755) e.g.:
387
<pre>
388
$ ls -l /home
389
drwxr-xr-x 26 michaelj epcc 4096 Mar  6 10:08 michaelj
390
</pre>
391
** If not then set these,
392
<pre>
393
$ chmod go+rx /home/michaelj
394 2 Jackson, Mike
</pre>
395
* Restart Apache 2.2,
396
<pre>
397
$ /usr/local/apache2/bin/apachectl restart
398
</pre>
399
* Test,
400
** Visit http://localhost:8080/maus/, remembering to change the port number if you use a custom port.