Project

General

Profile

MAUSDjangoApache » History » Revision 8

Revision 7 (Jackson, Mike, 10 February 2012 14:00) → Revision 8/29 (Jackson, Mike, 10 February 2012 15:37)

h1. Deploying Django under Apache 

 {{>toc}} 

 h2. Apache 2.2 deployment 

 Apache 2.2 (http://httpd.apache.org/) is an HTTP server. 

 For full deployment information, see 

 * http://httpd.apache.org/docs/current/install.html 

 Following the "overview for the impatient"... 

 * Log in as root user, 
 <pre> 
 $ sudo su - 
 </pre> 
 * Download, 
 <pre> 
 $ lynx http://httpd.apache.org/download.cgi 
 </pre> 
 ** Alternatively: 
 <pre> 
 $ wget http://mirrors.ukfast.co.uk/sites/ftp.apache.org//httpd/httpd-2.2.22.tar.gz 
 </pre> 
 * Extract, 
 <pre> 
 $ gzip -d httpd-2.2.22.tar.gz  
 $ tar xvf httpd-2.2.22.tar 
 $ cd httpd-2.2.22 
 </pre> 
 * Configure, 
 <pre> 
 $ ./configure 
 </pre> 
 ** This uses a default deployment directory of @/usr/local/apache2@. 
 * Compile, 
 <pre> 
 $ make  
 </pre> 
 * Install, 
 <pre> 
 $ make install  
 </pre> 
 * Start-up server, 
 <pre> 
 $ /usr/local/apache2/bin/apachectl -k start 
 </pre> 
 ** If you get an error like, 
 <pre> 
 (98)Address already in use: make_sock: could not bind to address [::]:80 
 (98)Address already in use: make_sock: could not bind to address 0.0.0.0:80 
 no listening sockets available, shutting down 
 Unable to open logs 
 </pre> 
 ** Then edit @/usr/local/apache2/conf/httpd.conf@, 
 <pre> 
 $ xemacs /usr/local/apache2/conf/httpd.conf  
 </pre> 
 ** and change, 
 <pre> 
 Listen 80  
 </pre> 
 ** to a port that is not in use e.g. 
 <pre> 
 Listen 9090 
 </pre> 
 ** and start Apache, 
 <pre> 
 $ /usr/local/apache2/bin/apachectl -k start 
 </pre> 
 * Browse to http://localhost:8080 (or whatever port you specified above) and you should see @It works!"@ 

 Useful files: 

 * Configuration: @/usr/local/apache2/conf/httpd.conf@ 
 * Error log: @/usr/local/apache2/logs/error_log@ 

 h2. mod_wsgi deployment 

 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/InstallationInstructions 
 * http://code.google.com/p/modwsgi/wiki/InstallationIssues 

 Now, following the quick installation guide (http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide), 

 * Download latest release (July 2010) 
 <pre> 
 $ wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz 
 </pre> 
 * Extract, 
 <pre> 
 $ gunzip mod_wsgi-3.3.tar.gz  
 $ tar -xf mod_wsgi-3.3.tar 
 $ cd mod_wsgi-3.3 
 </pre> 
 * Set Python library path, if using a non-default Python distribution. The author uses MAUS Python, so set: 
 <pre> 
 $ export LD_LIBRARY_PATH=/home/michaelj/maus-bzr/maus/third_party/install/lib/ 
 </pre> 
 * Configure, 
 <pre> 
 $ ./configure 
 </pre> 
 ** If you get an error like: 
 <pre> 
 /home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7: error while loading  
 shared libraries: libpython2.7.so.1.0: cannot open shared object file:  
 No such file or directory 
 </pre> 
 ** Then set the @LD_LIBRARY_PATH@ as above. 
 ** 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: 
 <pre> 
    --with-python=/home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7 
 </pre> 
 ** If you get an error like: 
 <pre> 
 checking for apxs2... no 
 checking for apxs... no 
 checking Apache version... ./configure: line 1704: apxs: command not found 
 ./configure: line 1704: apxs: command not found 
 ./configure: line 1705: apxs: command not found 
 ./configure: line 1708: /: is a directory 

 checking for python... /home/michaelj/maus-bzr/maus/third_party/install/bin/python 
 ./configure: line 1877: apxs: command not found 
 configure: creating ./config.status 
 config.status: error: cannot find input file: Makefile.in 
 </pre> 
 ** Then explicitly provide the path to Apache's @apxs@ command, 
 <pre> 
 $ ./configure --with-apxs=/usr/local/apache2/bin/apxs 
 </pre> 
 ** Combining the above gives, 
 <pre> 
 $ ./configure --with-apxs=/usr/local/apache2/bin/apxs 
   --with-python=/home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7 
 </pre> 
 * Compile, 
 <pre> 
 $ make 
 </pre> 
 * Install, 
 <pre> 
 $ make install 
 </pre> 
 * A @mod_wsgi.so@ library is installed in @/usr/local/apache2/modules@. 
 * Configure Apache, edit @/usr/local/apache2/conf/httpd.conf@, 
 <pre> 
 $ xemacs /usr/local/apache2/conf/httpd.conf  
 </pre> 
 * and add, 
 <pre> 
 LoadModule wsgi_module modules/mod_wsgi.so 
 </pre> 
 * 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. 
 <pre> 
 LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH"  
 </pre> 
 * Restart Apache, 
 <pre> 
 $ /usr/local/apache2/bin/apachectl restart 
 </pre> 
 * If all is well you should see, in @/usr/local/apache2/logs/error_log@ a line like, 
 <pre> 
 [Tue Feb 07 18:50:37 2012] [notice] Apache/2.2.22 (Unix) mod_wsgi/3.3  
 Python/2.7.2 configured -- resuming normal operations 
 </pre> 
 * If you see an error like, 
 <pre> 
 [Tue Feb 07 18:46:40 2012] [notice] SIGHUP received.    Attempting to restart 
 httpd: Syntax error on line 55 of /usr/local/apache2/conf/httpd.conf: Cannot  
 load /usr/local/apache2/modules/mod_wsgi.so into server:  
 libpython2.7.so.1.0: cannot open shared object file: No such file or directory 
 </pre> 
 ** then you have to set the @LD_LIBRARY_PATH@ as above. 
 * Clean up, 
 <pre> 
 $ make clean 
 </pre> 

 To check your installation (following http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation), 

 * List server information, 
 <pre> 
 $ /usr/local/apache2/bin/httpd    -V 
 Server version: Apache/2.2.22 (Unix) 
 ... 
 Server MPM:       Prefork 
   threaded:       no 
     forked:       yes (variable process count) 
 Server compiled with.... 
  -D APACHE_MPM_DIR="server/mpm/prefork" 
 ... 
 </pre> 
 ** Important information is the version and prefork settings, 
 * List compiled-in modules, 
 <pre> 
 $ /usr/local/apache2/bin/httpd    -l 
 Compiled in modules: 
 ... 
   prefork.c 
 </pre> 
 ** You should see @prefork.c@. 
 * List dynamically-loaded modules, 
 <pre> 
 $ /usr/local/apache2/bin/httpd    -M 
  mpm_prefork_module (static) 
  ... 
  wsgi_module (shared) 
  ... 
 </pre> 
 ** You should see @wsgi_module@. 
 ** If this fails with, 
 <pre> 
 httpd: Syntax error on line 55 of /usr/local/apache2/conf/httpd.conf:  
 Cannot load /usr/local/apache2/modules/mod_wsgi.so into server:  
 libpython2.7.so.1.0: cannot open shared object file: No such file or directory 
 </pre> 
 ** Then set your @LD_LIBRARY_PATH@ as above and try again, 
 <pre> 
 $ export LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH" 
 $ /usr/local/apache2/bin/httpd -M 
 Loaded Modules: 
 ... 
 </pre> 
 * List modules linked to by @mod_wsgi@: 
 <pre> 
 $ ldd /usr/local/apache2/modules/mod_wsgi.so 
 ... 
 libpython2.7.so.1.0 => /home/michaelj/maus-bzr/maus/third_party/install/lib/libpython2.7.so.1.0  
 (0x00110000) 
 ... 
 </pre> 
 ** You should see the link to your Python library. 

 h2. Deploying ImageMagick 

 The ImageMagick library is used to convert between image types in the MAUS web-front end. 

 To install, 

 * Log in as root user, 
 <pre> 
 $ sudo su - 
 </pre> 
 * Get and unpack ImageMagick, 
 <pre> 
 $ wget http://www.imagemagick.org/download/ImageMagick.tar.gz 
 $ gunzip ImageMagick.tar.gz 
 $ tar -xf ImageMagick.tar 
 $ cd ImageMagick-6.7.4-0/ 
 </pre> 
 * Configure, 
 <pre> 
 $ ./configure 
 </pre> 
 * Make, 
 <pre> 
 $ make 
 </pre> 
 * Install, 
 <pre> 
 $ make install 
 </pre> 
 ** This places libraries in @/usr/local/lib@. 
 * Cache the libraries, 
 <pre> 
 $ ldconfig /usr/local/lib 
 </pre> 
 ** This avoids problems with libraries not being found by Python's magickwand library. 
 * Check, 
 <pre> 
 $ /usr/local/bin/convert -h 
 Version: ImageMagick 6.7.4-0 2011-12-13 Q16 http://www.imagemagick.org 
 Copyright: Copyright (C) 1999-2011 ImageMagick Studio LLC 
 Features: OpenMP     
 </pre> 

 To install Python magickwand 

 * Setup the MAUS environment: 
 <pre> 
 $ source env.sh 
 </pre> 
 * Install, 
 <pre> 
 $ easy_install magickwand 
 </pre> 
 Check, 
 <pre> 
 $ python 
 $ import magickwand 
 $ from magickwand.image import Image 
 ... 
 </pre> 
 * If you get an error like, 
 <pre> 
 Traceback (most recent call last): 
   File "<stdin>", line 1, in <module> 
   File "build/bdist.linux-i686/egg/magickwand/image.py", line 2, in <module> 
   File "build/bdist.linux-i686/egg/magickwand/api/__init__.py", line 1, in <module> 
     # 
   File "build/bdist.linux-i686/egg/magickwand/api/lib.py", line 11, in <module> 
   File "/home/michaelj/maus-bzr/maus/third_party/install/lib/python2.7/ctypes/__init__.py",  
   line 353, in __init__ self._handle = _dlopen(self._name, mode) 
 OSError: libMagickWand.so.5: cannot open shared object file:  
  No such file or directory 
 </pre> 
 * then run @ldconfig@ as described above. 

 h2. Deploying the MAUS Django web front-end 

 For more on Django deployment under Apache 2.2/mod_wsgi, see 

 * https://docs.djangoproject.com/en/1.3/howto/deployment/modwsgi/ 
 * http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango 

 * Check-out the MAUS Django code, 
 <pre> 
 $ bzr branch -v lp:~michaelj-h/maus-apps/devel maus-apps 
 </pre> 
 * Set-up environment, 
 <pre> 
 $ cd maus-apps 
 $ ./configure 
 $ source env.sh 
 $ ./install.sh 
 </pre> 
 * Edit Django WSGI configuration file @apache/django.wsgi@ 
 ** Change @/home/michaelj@ in the above to the location of your @maus-apps@ check-out. 
 * Change permissions on the @media@ and @apache@ directories, so the @apache@ user that Apache 2.2 runs under can read configuration and update images, 
 <pre> 
 $ chmod -R go+rwx apache/ 
 $ chmod -R go+rwx media/ 
 </pre> 
 * Copy a couple of sample images, 
 <pre> 
 $ cp images/* media/raw 
 </pre> 

 * Log in as root user, 
 <pre> 
 $ sudo su - 
 </pre> 
 * Edit Apache 2.2 configuration, 
 <pre> 
 $ xemacs -nw /usr/local/apache2/conf/httpd.conf 
 </pre> 
 Add, 
 <pre> 
 WSGIScriptAlias / ${MAUS_WEB_DIR}/apache/django.wsgi   
 
 /home/michaelj/maus-apps/apache/django.wsgi 
 # Location of django.wsgi  
 
 <Directory ${MAUS_WEB_DIR}/apache>  
 /home/michaelj/maus-apps/apache> 
 Order deny,allow   
  
 Allow from all   
  
 </Directory>  
 
 </pre> 
 * where: 
 ** @/@ specifies the root URL - so all URL requests will be directed to Django. 
 ** @/...@ - location of WSGI file within the MAUS Django project. 
 * Change @/home/michaelj@ in the above to the location of your @maus-apps@ check-out. 
 * Edit Apache 2.2 environment, so it has access to the MAUS environment (in particular all the Python libraries that have been easy_installed): 
 <pre> 
 $ xemacs -nw /usr/local/apache2/bin/envvars 
 </pre> 
 * Add 
 <pre> 
 source /home/michaelj/maus-bzr/maus/env.sh  
 source /home/michaelj/maus-apps/env.sh </pre> 
 <pre> 
 $ xemacs -nw /usr/local/apache2/bin/envvars 
 </pre> 
 * Restart Apache 2.2, 
 <pre> 
 $ /usr/local/apache2/bin/apachectl restart 
 </pre> 
 * Test, 
 ** Visit http://localhost:8080/maus/, remembering to change the port number if you use a custom port.