In this article, I’m going to walk through the installation and use of mod_wsgi for hosting Python web-apps.
Note: as of the posting of this article, mod_wsgi is up to version 2.5. The instructions should remain the same, just with the updated version number.
mod_wsgi is an Apache 2.0 module by Graham Dumpleton that provides a layer that WSGI enabled Python applications can talk to. Web frameworks like Pylons, TurboGears 2.0, and Django support the WSGI interface. It’s essentially an alternative to mod_python / CherryPy / Paster.
I’m going to use virtualenv during this setup to create a special python environment for packages common to all of our WSGI apps, so if you aren’t already familiar with it, check out my previous virtualenv post.
The following assumes that your Apache directory is /usr/local/apache/ and that you have already installed:
apache >= 2.0 python >= 2.3 virtualenv gcc
Step 1: Install mod_wsgi
$ cd ~/my_setup_dir $ wget http://modwsgi.googlecode.com/files/mod_wsgi-2.3.tar.gz $ tar -xvzf mod_wsgi-2.3.tar.gz $ cd mod_wsgi-2.3 $ ./configure $ make $ su # make install # exit
Should give output that resembles:
/usr/local/apache/bin/apxs -i -S LIBEXECDIR=/usr/local/apache/modules -n 'mod_wsgi' mod_wsgi.la
/usr/local/apache/build/instdso.sh SH_LIBTOOL='/usr/local/apache/build/libtool' mod_wsgi.la /usr/local/apache/modules
/usr/local/apache/build/libtool --mode=install cp mod_wsgi.la /usr/local/apache/modules/
cp .libs/mod_wsgi.so /usr/local/apache/modules/mod_wsgi.so
cp .libs/mod_wsgi.lai /usr/local/apache/modules/mod_wsgi.la
cp .libs/mod_wsgi.a /usr/local/apache/modules/mod_wsgi.a
chmod 644 /usr/local/apache/modules/mod_wsgi.a
ranlib /usr/local/apache/modules/mod_wsgi.a
PATH="$PATH:/sbin" ldconfig -n /usr/local/apache/modules
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/apache/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/local/apache/modules/mod_wsgi.soIf the chain breaks at any time with an error like:
unable to execute gcc: Permission denied error: Setup script exited with error: command 'gcc' failed with exit status 1
Check out my previous post about compiler permissions.
Step 2: Create Python Virtualenv Environment for mod_wsgi applications
$ cd /wherever/you/keep/shared/libraries/ $ virtualenv --no-site-packages wsgi-baseline-python
To install python packages that only affect your WSGI applications, just activate the virtual environment and install away!
$ source /wherever/you/keep/shared/libraries/wsgi-baseline-python/bin/activate $ easy_install awesome-py
Step 3: Configure Apache to use mod_wsgi with our virgin baseline environment
We need to add the following lines to our Apache config.
LoadModule wsgi_module modules/mod_wsgi.so AddHandler wsgi-script .wsgi WSGIPythonHome /wherever/you/keep/shared/libraries/python-wsgi-baseline
If you’re into hand-tuning httpd.conf, you can just add them where appropriate. If you use WHM/Cpanel, add those lines in: WHM -> Server Configuration -> Apache Setup -> Include Editor -> Pre VirtualHost Include
Save those changes, then restart Apache.
If this step gives you sass, with an error containing the string:
Syntax error on line 1 of /usr/local/apache/conf/includes/pre_virtualhost_2.conf: API module structure "wsgi_module" in file /usr/lib/httpd/modules/mod_wsgi.so is garbled
It could be a confusion between multiple Apache versions on your system when you compiled mod_wsgi. Check out the fix at Dustin Davis’ (Django) tutorial.
Step 4: Test!
To run a (relatively) simple test to make sure mod_wsgi is working, you can follow the directions at the mod_wsgi Quick Configuration Guide.
References:
- Dustin Davis’ great tutorial on setting up Django/mod_wsgi using cpanel.
- Graham Dumpleton’s excellent how-tos, gotchas, and docs, the Virtual Environments one in particular.
TurboGears 2 (TG2) Says:
[...] this article (the spiritual successor of the mod_wsgi article), I’m going to walk through the installation and configuration of a TurboGears 2.0 [...]