Latest Version: 0.9.6.2
  Dashboard > Pylons Cookbook > ... > Deployment > Production deployment using mod_python
  Pylons Cookbook Log In | Sign Up   View a printable version of the current page.  
  Production deployment using mod_python
Added by Graham Higgins, last edited by Fanda Vacek on Sep 09, 2008  (view change)
Labels: 
(None)

Name Space Section Page Version Status Curator Reviewed Author(s)
Production deployment using mod_python Pylons CookBook   Production deployment using mod_python 1.0 Draft Graham Higgins False  

Production deployment using mod_python

Introduction

Mod_Python is an Apache module that embeds the Python interpreter within the Apache web server. The result is an increased performance when running scripts. Benchmarks show that it can be up to five times faster than FastCGI.

Prerequisites

mod_python

Make sure that mod_python is installed. If it is not already installed you can download it from apache.org. The installation process is fairly simple and is explained in detail over here. It basically boils down to: download, ./configure, make_install, edit httpd.conf and restart your server. You will need at least
Apache 2.0.40

On Ubuntu (and probably Debian too) you can just do:

sudo apt-get install libapache2-mod-python2.4

WSGI Gateway

Mod_Python does not come with WSGI support by default. We will need a gateway which translates all WSGI calls to something the mod_python handler can understand. You can find a Mod_Python <-> WSGI gateway over here. Just download it and put in in your .../python2.4/site-packages/mod_python/ directory as wsgi.py.

You will need to modify the wsgi.py file as descibed at http://tinyurl.com/gogtq so that the script also serves applications at the site root correctly.

Setting it Up

You can make your pylons app talk to Mod_Python by simply putting a .htaccess in your project dir:

1
2
3
4
5
SetHandler mod_python
PythonHandler mod_python.wsgi
PythonPath "['/home/httpd/vhosts/nichol.as/httpdocs/imagesearch'] + sys.path"
PythonOption wsgi.application startup::app
PythonOption SCRIPT_NAME /imagesearch

Make sure that you specify the correct SCRIPT_NAME which should be the name of your Pylons Project and that your PythonPath points to the correct directory.

After that create a small file called startup.py also in your project directory:

1
2
from paste.deploy import loadapp
app = loadapp("config:/home/httpd/vhosts/nichol.as/httpdocs/imagesearch/deployement.ini")

Et voila.

Things To Keep in Mind

There are a few things you have to pay special attention to:

  1. Debugging under Mod_Python is hard because Mod_Python does a fair bit of caching. Even with !PythonDebug set to On it won't show changes in the code immediately because of the multiple processes/threads its running. I therefore recommend that when you still want to work on your code, just run

    paster serve --reload development.ini

  2. The interactive debugging middleware won't work with "multi-process" mod_python. Thus, set "debug = False" in your project configuration file.
  3. For optimal performance you should put the Apache directives in your httpd.conf and not in your .htaccess.
  4. When first migrating from the Paste HTTP server, you may encounter the following problems:
    1. Because the Apache web server is run by a different user than the Paste HTTP server, and you may longer have write (or even read) access to files used by your application and any middleware (for example, sessions).
    2. Because the Apache web server has a different "current working directory" than the Paste HTTP server, files made in relative locations won't reference the same files as before, and generally the new file locations will not allow the creation and writing to of those files.
    3. Because there are multiple mod_python instances in the Apache server (usually one per application per process), you can no longer store information between requests in the pylons.g variable, as that variable is specific to that particular mod_python instance. Rather use sessions.

Troubleshooting

Working with mod_python can be a painful experience when things go wrong or wont work. I hope the following tips will save you time when things do go wrong.

  1. Always keep an eye on your Apache error log.
  2. Make sure that Mod_Python actually works
  3. When your site works only sometimes. Stop your httpd server (make sure it has really stopped) and after that start it again.
  4. When you notice segmentation faults in your error log there are most probably 2 causes:
    • Incompatible expat libraries being used by Python and Apache. (pyexpat is being used by paste). You can read more about this here
    • A known conflict due to version conflicts between mod_python, PHP and the Python MySQL backend. You can read more about this at the mod_python faq.

An Example (by wyatt at bycycle dot org)

I have a Pylons project at /home/bycycle/TripPlanner/evil/tripplanner. I want the project to run at "http://myhost/tpevil".

Here is what I put in myhost site configuration (i.e., /etc/apache2/sites-available/myhost):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<Location /tpevil>
	SetHandler mod_python
	PythonHandler mod_python.wsgi
	PythonPath "['/home/bycycle/TripPlanner/evil/tripplanner'] + sys.path"
	PythonOption wsgi.application startup::app
	PythonOption SCRIPT_NAME /tpevil
<Location>

# Tell Apache where the static files live
Alias /tpevil/public /home/bycycle/TripPlanner/evil/tripplanner/tripplanner/public

# Disable the mod_python handler for static files
<Location /tpevil/public>
	SetHandler None
</Location>

Here is what my startup.py looks like:

1
2
from paste.deploy import loadapp
app = loadapp("config:/home/bycycle/TripPlanner/evil/tripplanner/development.ini")

I had to change the permissions of the top-level data directory because Apache needs to be able to write there:

# Go to the top level of your Pylons project (where development.ini is)
cd /home/bycycle/TripPlanner/evil/tripplanner
# www-data is whatever the name is that Apache runs under
sudo chown -R bycycle:www-data data
chmod -R g+wx data

Restart the server and try it out:

sudo /etc/init.d/apache2 stop
sudo /etc/init.d/apache2 start

An Alternate Mod_Python / WSGI Bridge (by elic at bellsouth dot net)

A project I was working on ran into some limitations with the mod_python / wsgi bridge that is linked to above, and I ended up rewriting it significantly. Thought I'd share it with the rest of the Pylons community by making it available for download here.

A quick list of the improvements includes: support for use in multiple virtual hosts, thread safety,
speed improvements, and wsgi.file_wrapper support using mod_python.sendfile().

In addition, while (hopefully) retaining drop-in compatibility with the orginal bridge, I've added some paste-specific keywords to help out
with pylons deployment. While more documentation is available within the source, here's a modified site configuration for Wyatt's example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<Location /tpevil>
	SetHandler mod_python
	PythonHandler mod_python.wsgi

	#this path is added to sys.path if not already present...
	PythonOption wsgi.application_path /home/bycycle/TripPlanner/evil/tripplanner

	#this file is passed to paste.deploy.loadapp()
	#and is relative to the wsgi.application_path...
	PythonOption wsgi.paste_application development.ini

	PythonOption SCRIPT_NAME /tpevil
</Location>

# Tell Apache where the static files live
Alias /tpevil/public /home/bycycle/TripPlanner/evil/tripplanner/tripplanner/public

# Disable the mod_python handler for static files
<Location /tpevil/public>
	SetHandler None
</Location>

NOTE: a separate startup.py is NOT REQUIRED when using this bridge.

If anyone has bug reports or suggestions for this bridge, please email me.

Yet another approach (by fanda.vacek at volny dot cz)

After hopeless day I have found, that Pylons is already delivered with a mod_python <-> SWGI bridge and all what I have to do is to write next lines to my .htaccess file.

1
2
3
4
SetHandler python-program
PythonHandler paste.modpython
PythonPath "['/path/to/your/pylons/application'] + sys.path"
PythonOption paste.ini /path/to/your/pylons/application/deployment.ini

NOTE: Do not forget to enable .htaccess in your Apache config file. (comment AllowOverride None line in a Apache configuration).

You people are aware that all of these solutions are incredibly complex to implement. Right?

Posted by Rob Conner at May 05, 2007 23:48 | Permalink

In general, most solutions involving Apache and mod_python turn out to be incredibly complex to implement?

Compared to our current non-pylons mod_python setup, this is equally complex.

Posted by Mike Verdone at Oct 03, 2007 21:38 | Permalink

Just for those people who are discouraged by this comment, deploying mod_python WSGI applications (I don't use Pylons as such) is not difficult. I used the steps mentioned here, and it worked perfectly.

Ali

Posted by Ali Afshar at Oct 24, 2007 10:44 | Permalink

Another WSGI Handler for mod_python http://trac.gerf.org/pse/browser/trunk/wsgi_handler.py

Posted by Amr Mostafa at Sep 24, 2007 15:46 | Permalink

Sorry, should have linked to the homepage which has usage instructions: http://trac.gerf.org/pse/wiki/WSGIHandler

Posted by Amr Mostafa at Sep 24, 2007 15:48 | Permalink
Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top