Project

General

Profile

Actions

MAUSDjangoApache » History » Revision 15

« Previous | Revision 15/29 (diff) | Next »
Jackson, Mike, 06 March 2012 13:43


Deploying Django under Apache

Deploy Python 2.7 and related packages

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.

For full information on deploying Python 2.7, see http://www.python.org/getit/releases/2.7/

To install Python 2.7 via building from it's source code:

  • Log in as super-user e.g. using sudo su - or su.
  • Get Python 2.7 source distribution and unpack it,
    $ wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz
    $ mv Python-2.7.2.tgz Python-2.7.2.tar.gz
    $ gunzip Python-2.7.2.tar.gz 
    $ tar -xf Python-2.7.2.tar 
    $ cd Python-2.7.2
    
  • Configure,
    $ ./configure
    
  • Build,
    $ make
    
  • Install,
    $ make install
    

Deploy Python setup tools

Now, install the Python setup tools. For full information7, see http://pypi.python.org/pypi/setuptools.

  • Log in as super-user e.g. using sudo su - or su.
  • Get the setup tools egg,
    $ wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg#md5=fe1f997bc722265116870bc7919059ea
    
  • Execute the egg,
    $ sh setuptools-0.6c11-py2.7.egg 
    

This will install the Python easy_install tool.

Deploy ImageMagick

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

To install,

  • Log in as super-user e.g. using sudo su - or su.
  • Get and unpack ImageMagick,
    $ wget http://www.imagemagick.org/download/ImageMagick.tar.gz
    $ gunzip ImageMagick.tar.gz
    $ tar -xf ImageMagick.tar
    $ cd ImageMagick-6.7.4-0/
    
  • Configure,
    $ ./configure
    
  • Make,
    $ make
    
  • Install,
    $ make install
    
    • This places libraries in /usr/local/lib.
  • Cache the libraries,
    $ ldconfig /usr/local/lib
    
    • This avoids problems with libraries not being found by Python's magickwand library.
  • Check,
    $ /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    
    

Deploy Django, MagickWand and Pyton Image Library

Now install these dependencies, using easy_install:

$ easy_install django
$ easy_install magickwand
$ easy_install pil

Check,
$ python
$ import magickwand
$ from magickwand.image import Image
...
  • If you get an error like,
    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
    
  • then run ldconfig as described above.

Apache 2.2 deployment

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

For full deployment information, see

Following the "overview for the impatient"...

  • Log in as root user,
    $ sudo su -
    
  • Download,
    $ lynx http://httpd.apache.org/download.cgi
    
    • Alternatively:
      $ wget http://mirrors.ukfast.co.uk/sites/ftp.apache.org//httpd/httpd-2.2.22.tar.gz
      
  • Extract,
    $ gzip -d httpd-2.2.22.tar.gz 
    $ tar xvf httpd-2.2.22.tar
    $ cd httpd-2.2.22
    
  • Configure,
    $ ./configure
    
    • This uses a default deployment directory of /usr/local/apache2.
  • Compile,
    $ make 
    
  • Install,
    $ make install 
    
  • Start-up server,
    $ /usr/local/apache2/bin/apachectl -k start
    
    • If you get an error like,
      (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
      
    • Then edit /usr/local/apache2/conf/httpd.conf,
      $ xemacs /usr/local/apache2/conf/httpd.conf 
      
    • and change,
      Listen 80 
      
    • to a port that is not in use e.g.
      Listen 9090
      
    • and start Apache,
      $ /usr/local/apache2/bin/apachectl -k start
      
  • 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

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

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

  • Download latest release (July 2010)
    $ wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz
    
  • Extract,
    $ gunzip mod_wsgi-3.3.tar.gz 
    $ tar -xf mod_wsgi-3.3.tar
    $ cd mod_wsgi-3.3
    
  • 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:
    $ export LD_LIBRARY_PATH=/home/michaelj/maus-bzr/maus/third_party/install/lib/
    
  • Configure,
    $ ./configure
    
    • If you get an error like:
      /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
      
    • 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:
         --with-python=/home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7
      
    • If you get an error like:
      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
      
    • Then explicitly provide the path to Apache's apxs command,
      $ ./configure --with-apxs=/usr/local/apache2/bin/apxs
      
    • Combining the above gives,
      $ ./configure --with-apxs=/usr/local/apache2/bin/apxs
        --with-python=/home/michaelj/maus-bzr/maus/third_party/install/bin/python2.7
      
  • Compile,
    $ make
    
  • Install,
    $ make install
    
  • A mod_wsgi.so library is installed in /usr/local/apache2/modules.
  • Configure Apache, edit /usr/local/apache2/conf/httpd.conf,
    $ xemacs /usr/local/apache2/conf/httpd.conf 
    
  • and add,
    LoadModule wsgi_module modules/mod_wsgi.so
    
  • 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
    • Edit /usr/local/apache2/bin/envvars and add a line to set the Python library path e.g.
      LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH" 
      
    • Ensure that the permissions in this path allow group and world read and execute permission (755) e.g.:
      $ ls -l /home
      drwxr-xr-x 26 michaelj epcc 4096 Mar  6 10:08 michaelj
      
    • If not then set these,
      $ chmod go+rx /home/michaelj
      
  • Restart Apache,
    $ /usr/local/apache2/bin/apachectl restart
    
  • If all is well you should see, in /usr/local/apache2/logs/error_log a line like,
    [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
    
  • If you see an error like,
    [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
    
    • then you have to set the LD_LIBRARY_PATH as above.
  • If you see an error like,
    Could not find platform independent libraries <prefix>
    Could not find platform dependent libraries <exec_prefix>
    Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
    ImportError: No module named site
    
    • then it is likely you did not set the file permissions for the directory in which Python is as described above.
  • Clean up,
    $ make clean
    

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

  • List server information,
    $ /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" 
    ...
    
    • Important information is the version and prefork settings,
  • List compiled-in modules,
    $ /usr/local/apache2/bin/httpd  -l
    Compiled in modules:
    ...
      prefork.c
    
    • You should see prefork.c.
  • List dynamically-loaded modules,
    $ /usr/local/apache2/bin/httpd  -M
     mpm_prefork_module (static)
     ...
     wsgi_module (shared)
     ...
    
    • You should see wsgi_module.
    • If this fails with,
      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
      
    • Then set your LD_LIBRARY_PATH as above and try again,
      $ export LD_LIBRARY_PATH="/home/michaelj/maus-bzr/maus/third_party/install/lib/:$LD_LIBRARY_PATH" 
      $ /usr/local/apache2/bin/httpd -M
      Loaded Modules:
      ...
      
  • List modules linked to by mod_wsgi:
    $ 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)
    ...
    
    • You should see the link to your Python library.

Deploying the MAUS Django web front-end

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

  • Check-out the MAUS Django code,
    $ bzr branch -v lp:~michaelj-h/maus-apps/devel maus-apps
    
  • Set-up environment,
    $ cd maus-apps
    $ ./configure
    $ ./install.sh
    
  • Copy a couple of sample images,
    $ cp images/* media/raw
    
  • Log in as root user,
    $ sudo su -
    
  • Edit Apache 2.2 configuration,
    $ xemacs -nw /usr/local/apache2/conf/httpd.conf
    

    Add,
    WSGIScriptAlias / ${MAUS_WEB_DIR}/apache/django.wsgi  
    
    # Location of django.wsgi 
    <Directory ${MAUS_WEB_DIR}/apache> 
    Order deny,allow  
    Allow from all  
    </Directory> 
    
  • where:
    • / specifies the root URL - so all URL requests will be directed to Django.
    • /... - location of WSGI file within the MAUS Django project.
  • 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):
    $ xemacs -nw /usr/local/apache2/bin/envvars
    
  • Add, changing to use your own paths,
    source /home/michaelj/maus-bzr/maus/env.sh 
    source /home/michaelj/maus-apps/env.sh
    
  • Ensure that the permissions in to your paths allow group and world read and execute permission (755) e.g.:
    $ ls -l /home
    drwxr-xr-x 26 michaelj epcc 4096 Mar  6 10:08 michaelj
    
    • If not then set these,
      $ chmod go+rx /home/michaelj
      
  • Restart Apache 2.2,
    $ /usr/local/apache2/bin/apachectl restart
    
  • Test,

Updated by Jackson, Mike over 11 years ago · 15 revisions