MAUSDjangoApache » History » Version 16
Jackson, Mike, 06 March 2012 13:52
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 | |||
137 | 14 | Jackson, Mike | |
138 | |||
139 | 1 | Jackson, Mike | h2. Apache 2.2 deployment |
140 | |||
141 | Apache 2.2 (http://httpd.apache.org/) is an HTTP server. |
||
142 | |||
143 | For full deployment information, see |
||
144 | |||
145 | * http://httpd.apache.org/docs/current/install.html |
||
146 | |||
147 | Following the "overview for the impatient"... |
||
148 | |||
149 | * Log in as root user, |
||
150 | <pre> |
||
151 | $ sudo su - |
||
152 | </pre> |
||
153 | * Download, |
||
154 | <pre> |
||
155 | $ lynx http://httpd.apache.org/download.cgi |
||
156 | </pre> |
||
157 | ** Alternatively: |
||
158 | <pre> |
||
159 | $ wget http://mirrors.ukfast.co.uk/sites/ftp.apache.org//httpd/httpd-2.2.22.tar.gz |
||
160 | </pre> |
||
161 | * Extract, |
||
162 | <pre> |
||
163 | $ gzip -d httpd-2.2.22.tar.gz |
||
164 | $ tar xvf httpd-2.2.22.tar |
||
165 | $ cd httpd-2.2.22 |
||
166 | </pre> |
||
167 | * Configure, |
||
168 | <pre> |
||
169 | $ ./configure |
||
170 | </pre> |
||
171 | ** This uses a default deployment directory of @/usr/local/apache2@. |
||
172 | * Compile, |
||
173 | <pre> |
||
174 | $ make |
||
175 | </pre> |
||
176 | * Install, |
||
177 | <pre> |
||
178 | $ make install |
||
179 | </pre> |
||
180 | * Start-up server, |
||
181 | <pre> |
||
182 | $ /usr/local/apache2/bin/apachectl -k start |
||
183 | </pre> |
||
184 | ** If you get an error like, |
||
185 | <pre> |
||
186 | (98)Address already in use: make_sock: could not bind to address [::]:80 |
||
187 | (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 |
||
188 | no listening sockets available, shutting down |
||
189 | Unable to open logs |
||
190 | </pre> |
||
191 | ** Then edit @/usr/local/apache2/conf/httpd.conf@, |
||
192 | <pre> |
||
193 | $ xemacs /usr/local/apache2/conf/httpd.conf |
||
194 | </pre> |
||
195 | ** and change, |
||
196 | <pre> |
||
197 | Listen 80 |
||
198 | </pre> |
||
199 | ** to a port that is not in use e.g. |
||
200 | <pre> |
||
201 | Listen 9090 |
||
202 | </pre> |
||
203 | ** and start Apache, |
||
204 | <pre> |
||
205 | $ /usr/local/apache2/bin/apachectl -k start |
||
206 | </pre> |
||
207 | * Browse to http://localhost:8080 (or whatever port you specified above) and you should see @It works!"@ |
||
208 | |||
209 | Useful files: |
||
210 | |||
211 | * Configuration: @/usr/local/apache2/conf/httpd.conf@ |
||
212 | * Error log: @/usr/local/apache2/logs/error_log@ |
||
213 | |||
214 | h2. mod_wsgi deployment |
||
215 | |||
216 | 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). |
||
217 | |||
218 | For full deployment information, see |
||
219 | |||
220 | * http://code.google.com/p/modwsgi/wiki/InstallationInstructions |
||
221 | * http://code.google.com/p/modwsgi/wiki/InstallationIssues |
||
222 | |||
223 | Now, following the quick installation guide (http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide), |
||
224 | |||
225 | * Download latest release (July 2010) |
||
226 | <pre> |
||
227 | $ wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz |
||
228 | </pre> |
||
229 | * Extract, |
||
230 | <pre> |
||
231 | $ gunzip mod_wsgi-3.3.tar.gz |
||
232 | $ tar -xf mod_wsgi-3.3.tar |
||
233 | $ cd mod_wsgi-3.3 |
||
234 | </pre> |
||
235 | 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: |
236 | 1 | Jackson, Mike | <pre> |
237 | $ export LD_LIBRARY_PATH=/home/michaelj/maus-bzr/maus/third_party/install/lib/ |
||
238 | </pre> |
||
239 | * Configure, |
||
240 | <pre> |
||
241 | $ ./configure |
||
242 | </pre> |
||
243 | ** If you get an error like: |
||
244 | <pre> |
||
245 | /home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7: error while loading |
||
246 | shared libraries: libpython2.7.so.1.0: cannot open shared object file: |
||
247 | No such file or directory |
||
248 | </pre> |
||
249 | ** Then set the @LD_LIBRARY_PATH@ as above. |
||
250 | ** 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: |
||
251 | <pre> |
||
252 | --with-python=/home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7 |
||
253 | </pre> |
||
254 | ** If you get an error like: |
||
255 | <pre> |
||
256 | checking for apxs2... no |
||
257 | checking for apxs... no |
||
258 | checking Apache version... ./configure: line 1704: apxs: command not found |
||
259 | ./configure: line 1704: apxs: command not found |
||
260 | ./configure: line 1705: apxs: command not found |
||
261 | ./configure: line 1708: /: is a directory |
||
262 | |||
263 | checking for python... /home/michaelj/maus-bzr/maus/third_party/install/bin/python |
||
264 | ./configure: line 1877: apxs: command not found |
||
265 | configure: creating ./config.status |
||
266 | config.status: error: cannot find input file: Makefile.in |
||
267 | </pre> |
||
268 | ** Then explicitly provide the path to Apache's @apxs@ command, |
||
269 | <pre> |
||
270 | $ ./configure --with-apxs=/usr/local/apache2/bin/apxs |
||
271 | </pre> |
||
272 | ** Combining the above gives, |
||
273 | <pre> |
||
274 | $ ./configure --with-apxs=/usr/local/apache2/bin/apxs |
||
275 | --with-python=/home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7 |
||
276 | </pre> |
||
277 | * Compile, |
||
278 | <pre> |
||
279 | $ make |
||
280 | </pre> |
||
281 | * Install, |
||
282 | <pre> |
||
283 | $ make install |
||
284 | </pre> |
||
285 | * A @mod_wsgi.so@ library is installed in @/usr/local/apache2/modules@. |
||
286 | * Configure Apache, edit @/usr/local/apache2/conf/httpd.conf@, |
||
287 | <pre> |
||
288 | $ xemacs /usr/local/apache2/conf/httpd.conf |
||
289 | </pre> |
||
290 | * and add, |
||
291 | <pre> |
||
292 | LoadModule wsgi_module modules/mod_wsgi.so |
||
293 | </pre> |
||
294 | * 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 |
||
295 | ** Edit @/usr/local/apache2/bin/envvars@ and add a line to set the Python library path e.g. |
||
296 | <pre> |
||
297 | LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH" |
||
298 | </pre> |
||
299 | ** Ensure that the permissions in this path allow group and world read and execute permission (755) e.g.: |
||
300 | <pre> |
||
301 | $ ls -l /home |
||
302 | drwxr-xr-x 26 michaelj epcc 4096 Mar 6 10:08 michaelj |
||
303 | </pre> |
||
304 | ** If not then set these, |
||
305 | <pre> |
||
306 | $ chmod go+rx /home/michaelj |
||
307 | </pre> |
||
308 | * Restart Apache, |
||
309 | <pre> |
||
310 | $ /usr/local/apache2/bin/apachectl restart |
||
311 | </pre> |
||
312 | * If all is well you should see, in @/usr/local/apache2/logs/error_log@ a line like, |
||
313 | <pre> |
||
314 | [Tue Feb 07 18:50:37 2012] [notice] Apache/2.2.22 (Unix) mod_wsgi/3.3 |
||
315 | Python/2.7.2 configured -- resuming normal operations |
||
316 | </pre> |
||
317 | * If you see an error like, |
||
318 | <pre> |
||
319 | [Tue Feb 07 18:46:40 2012] [notice] SIGHUP received. Attempting to restart |
||
320 | 2 | Jackson, Mike | httpd: Syntax error on line 55 of /usr/local/apache2/conf/httpd.conf: Cannot |
321 | load /usr/local/apache2/modules/mod_wsgi.so into server: |
||
322 | libpython2.7.so.1.0: cannot open shared object file: No such file or directory |
||
323 | </pre> |
||
324 | ** then you have to set the @LD_LIBRARY_PATH@ as above. |
||
325 | * If you see an error like, |
||
326 | <pre> |
||
327 | Could not find platform independent libraries <prefix> |
||
328 | Could not find platform dependent libraries <exec_prefix> |
||
329 | Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] |
||
330 | ImportError: No module named site |
||
331 | </pre> |
||
332 | ** then it is likely you did not set the file permissions for the directory in which Python is as described above. |
||
333 | * Clean up, |
||
334 | <pre> |
||
335 | $ make clean |
||
336 | </pre> |
||
337 | |||
338 | To check your installation (following http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation), |
||
339 | |||
340 | * List server information, |
||
341 | <pre> |
||
342 | $ /usr/local/apache2/bin/httpd -V |
||
343 | Server version: Apache/2.2.22 (Unix) |
||
344 | ... |
||
345 | Server MPM: Prefork |
||
346 | threaded: no |
||
347 | forked: yes (variable process count) |
||
348 | Server compiled with.... |
||
349 | -D APACHE_MPM_DIR="server/mpm/prefork" |
||
350 | ... |
||
351 | </pre> |
||
352 | ** Important information is the version and prefork settings, |
||
353 | * List compiled-in modules, |
||
354 | <pre> |
||
355 | $ /usr/local/apache2/bin/httpd -l |
||
356 | Compiled in modules: |
||
357 | ... |
||
358 | prefork.c |
||
359 | </pre> |
||
360 | ** You should see @prefork.c@. |
||
361 | * List dynamically-loaded modules, |
||
362 | <pre> |
||
363 | $ /usr/local/apache2/bin/httpd -M |
||
364 | mpm_prefork_module (static) |
||
365 | ... |
||
366 | wsgi_module (shared) |
||
367 | ... |
||
368 | </pre> |
||
369 | ** You should see @wsgi_module@. |
||
370 | ** If this fails with, |
||
371 | <pre> |
||
372 | httpd: Syntax error on line 55 of /usr/local/apache2/conf/httpd.conf: |
||
373 | Cannot load /usr/local/apache2/modules/mod_wsgi.so into server: |
||
374 | libpython2.7.so.1.0: cannot open shared object file: No such file or directory |
||
375 | </pre> |
||
376 | ** Then set your @LD_LIBRARY_PATH@ as above and try again, |
||
377 | <pre> |
||
378 | $ export LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH" |
||
379 | $ /usr/local/apache2/bin/httpd -M |
||
380 | Loaded Modules: |
||
381 | ... |
||
382 | </pre> |
||
383 | * List modules linked to by @mod_wsgi@: |
||
384 | <pre> |
||
385 | $ ldd /usr/local/apache2/modules/mod_wsgi.so |
||
386 | ... |
||
387 | libpython2.7.so.1.0 => /home/michaelj/maus-bzr/maus/third_party/install/lib/libpython2.7.so.1.0 |
||
388 | (0x00110000) |
||
389 | ... |
||
390 | </pre> |
||
391 | ** You should see the link to your Python library. |
||
392 | |||
393 | |||
394 | |||
395 | |||
396 | h2. Deploying the MAUS Django web front-end |
||
397 | |||
398 | For more on Django deployment under Apache 2.2/mod_wsgi, see |
||
399 | |||
400 | * https://docs.djangoproject.com/en/1.3/howto/deployment/modwsgi/ |
||
401 | * http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango |
||
402 | |||
403 | * Check-out the MAUS Django code, |
||
404 | <pre> |
||
405 | 5 | Jackson, Mike | $ bzr branch -v lp:~michaelj-h/maus-apps/devel maus-apps |
406 | 2 | Jackson, Mike | </pre> |
407 | 3 | Jackson, Mike | * Set-up environment, |
408 | <pre> |
||
409 | 6 | Jackson, Mike | $ cd maus-apps |
410 | 8 | Jackson, Mike | $ ./configure |
411 | 4 | Jackson, Mike | $ ./install.sh |
412 | 6 | Jackson, Mike | </pre> |
413 | 3 | Jackson, Mike | * Copy a couple of sample images, |
414 | <pre> |
||
415 | 6 | Jackson, Mike | $ cp images/* media/raw |
416 | 3 | Jackson, Mike | </pre> |
417 | |||
418 | 2 | Jackson, Mike | * Log in as root user, |
419 | <pre> |
||
420 | $ sudo su - |
||
421 | </pre> |
||
422 | * Edit Apache 2.2 configuration, |
||
423 | <pre> |
||
424 | $ xemacs -nw /usr/local/apache2/conf/httpd.conf |
||
425 | </pre> |
||
426 | 1 | Jackson, Mike | Add, |
427 | 2 | Jackson, Mike | <pre> |
428 | 8 | Jackson, Mike | WSGIScriptAlias / ${MAUS_WEB_DIR}/apache/django.wsgi |
429 | |||
430 | # Location of django.wsgi |
||
431 | <Directory ${MAUS_WEB_DIR}/apache> |
||
432 | Order deny,allow |
||
433 | Allow from all |
||
434 | </Directory> |
||
435 | 2 | Jackson, Mike | </pre> |
436 | * where: |
||
437 | 1 | Jackson, Mike | ** @/@ specifies the root URL - so all URL requests will be directed to Django. |
438 | 2 | Jackson, Mike | ** @/...@ - location of WSGI file within the MAUS Django project. |
439 | 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): |
440 | 2 | Jackson, Mike | <pre> |
441 | $ xemacs -nw /usr/local/apache2/bin/envvars |
||
442 | </pre> |
||
443 | 11 | Jackson, Mike | * Add, changing to use your own paths, |
444 | 2 | Jackson, Mike | <pre> |
445 | source /home/michaelj/maus-bzr/maus/env.sh |
||
446 | 1 | Jackson, Mike | source /home/michaelj/maus-apps/env.sh |
447 | 12 | Jackson, Mike | </pre> |
448 | * Ensure that the permissions in to your paths allow group and world read and execute permission (755) e.g.: |
||
449 | <pre> |
||
450 | $ ls -l /home |
||
451 | drwxr-xr-x 26 michaelj epcc 4096 Mar 6 10:08 michaelj |
||
452 | </pre> |
||
453 | ** If not then set these, |
||
454 | <pre> |
||
455 | $ chmod go+rx /home/michaelj |
||
456 | 2 | Jackson, Mike | </pre> |
457 | * Restart Apache 2.2, |
||
458 | <pre> |
||
459 | $ /usr/local/apache2/bin/apachectl restart |
||
460 | </pre> |
||
461 | * Test, |
||
462 | ** Visit http://localhost:8080/maus/, remembering to change the port number if you use a custom port. |