Archive for July, 2009

Python Projects in Users’ Home Directories with wsgi

posted on July 8th, 2009 by Greg Allard in Greg's Comments on the Internet

Thanks for taking at look at this. I’ll test it without SetHandler None when I get a chance. It is probably something I was keeping around from when I was using mod_python. I’m guessing I need to keep LocationMatch in there so that the request doesn’t go to the wsgi file though, right?

Read more comments by Greg Allard

Python Projects in Users’ Home Directories with wsgi

posted on July 8th, 2009 by pyroman in Greg's Comments on the Internet

I tried doing it that way first, but the wsgi directives aren’t allowed in an .htaccess file. It would have been much better to have a user place a few lines in their .htaccess file for python projects, but I didn’t find a way of doing that.

Read more comments by Greg Allard

greg_allard: I wish @djangocon wasn’t at the same time as @progpowerusa. Looks like I will miss another #djangocon

posted on July 8th, 2009 by Twitter / greg_allard in Greg's Tweets

greg_allard: I wish @djangocon wasn’t at the same time as @progpowerusa. Looks like I will miss another #djangocon

greg_allard: rt @ivylees Nifty product Flair4all (removable wall stickers) made a nice release on Presskit’n http://ivylees.com/qYSmL

posted on July 8th, 2009 by Twitter / greg_allard in Greg's Tweets

greg_allard: rt @ivylees Nifty product Flair4all (removable wall stickers) made a nice release on Presskit’n http://ivylees.com/qYSmL

greg_allard: new code spatter post. Python Projects in Users’ Home Directories with wsgi http://bit.ly/JHXsq

posted on July 8th, 2009 by Twitter / greg_allard in Greg's Tweets

greg_allard: new code spatter post. Python Projects in Users’ Home Directories with wsgi http://bit.ly/JHXsq

Python Projects in Users’ Home Directories with wsgi

posted on July 8th, 2009 by PyromanX in Greg's Bookmarks on Delicious

Letting users put static files and php files in a public_html folder in their home directory has been a common convention for some time. I created a way for users to have a public_python folder that will allow for python projects.

Python Projects in Users’ Home Directories with wsgi

posted on July 8th, 2009 by PyromanX in Greg's Bookmarks on Delicious

Letting users put static files and php files in a public_html folder in their home directory has been a common convention for some time. I created a way for users to have a public_python folder that will allow for python projects.

Python Projects in Users’ Home Directories with wsgi

posted on July 8th, 2009 by Greg Allard in Greg's Posts on Code Spatter

Letting users put static files and php files in a public_html folder in their home directory has been a common convention for some time. I created a way for users to have a public_python folder that will allow for python projects.

In the apache configuration files I created some regular expression patterns that will look for a wsgi file based on the url requested. To serve this url: http://domain/~user/p/myproject, the server will look for this wsgi file: /home/user/public_python/myproject/deploy/myproject.wsgi

It is set up to run wsgi in daemon mode so that each user can touch their own wsgi file to restart their project instead of needing to reload the apache config and inconvenience everyone.

This is the code I added to the apache configuration (in a virtual host, other configs might be different):

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/~(\w+)/p/(\w+)/(.*)
RewriteRule . - [E=python_project_name:%2]
 
WSGIScriptAliasMatch ^/~(\w+)/p/(\w+)  /home/$1/public_python/$2/deploy/$2.wsgi
WSGIDaemonProcess wsgi_processes.%{ENV:python_project_name}
processes=2 threads=15
WSGIProcessGroup wsgi_processes.%{ENV:python_project_name}
 
AliasMatch ^/~(\w+)/p/(\w+)/files(.*) /home/$1/public_python/$2/files$3
<LocationMatch ^/~(\w+)/p/(\w+)/files(.*)>
       SetHandler none
</LocationMatch>
 
AliasMatch ^/~(\w+)/p/(\w+)/media(.*) /home/$1/public_python/$2/media$3
<LocationMatch ^/~(\w+)/p/(\w+)/media(.*)>
       SetHandler none
</LocationMatch>

This will also serve two directories statically for images, css, and javascript. For one of them, I always make a symbolic link to the django admin media and tell my settings file to use that.

ln -s /path/to/django/contrib/admin/media media

To use this for a django project

This is a sample wsgi file to use for a django project. Username and project_name will need to be replaced. I’m also adding an apps folder to the path following
the style I mention in my reusable apps post.

import os
import sys
 
sys.path = ['/home/username/public_python/', '/home/username/public_python/project_name/apps'] + sys.path
from django.core.handlers.wsgi import WSGIHandler
 
os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'
application = WSGIHandler()

I’ve been using this for a couple weeks and it’s working great for me. If you use it, I’d like to know how it works out for you. Let me know in the comments.

Related posts:

  1. How to Add Locations to Python Path for Reusable Django Apps In my previous post I talk about reusable apps, but…
  2. Getting Basecamp API Working with Python I found one library that was linked everywhere, but it…
  3. Setting up Apache2, mod_python, MySQL, and Django on Debian Lenny or Ubuntu Hardy Heron Both Debian and Ubuntu make it really simple to get…

greg_allard: @montylounge I’m glad you liked the post. I’ve been doing the deleted flag forever and django is awesome for making it easy to hide those

posted on July 7th, 2009 by Twitter / greg_allard in Greg's Tweets

greg_allard: @montylounge I’m glad you liked the post. I’ve been doing the deleted flag forever and django is awesome for making it easy to hide those

DivmodReverend – Divmod – Trac

posted on July 6th, 2009 by PyromanX in Greg's Bookmarks on Delicious