Tuesday, March 29, 2011

Python on Django on WSGI on Apache on XAMPP on OSX Snow Leopard on Monday

Sooner or later I had to try Python. And since XAMPP is still the best way to do webdev on OSX I had to get Python to play along. Sure there are XAMPP equivalents for Python but well, I already have 2 of Apache and MySQL running, I am not using one of them and I definitely dont need another pair.
As it turns out it was not that hard once I dropped the fuss and added the "easy" keyword to my googling for way to tame the critter.
This time I really found a walkthrough that was right all the way. You can check it here.
The main idea is that you have to compile a Python module for your XAMPP Apache and that's covered in the article pretty well.
Then you have to add the
AddModule wsgi_module modules/mod_wsgi.so
in the httpd.conf and the WSGIScriptAlias directive in your vhost.
At this point you should be able to go to your myvhost.tld in the browser and have the simple hello world wsgi script running.
Next I needed to get Django running. As I am using the PyDev Eclipse plugin when I created a new Django project "DjangoSandbox" I got the initial files created for me. This would be the equivalent of manually running
django-admin.py startproject DjangoSandbox
Now getting Django to run in Apache is a matter of who gives up first. Basically if you search for answers online you ll backtrack from Django's "howto WSGI" to WSGI's "how to Django" to WSGI's creator "An improved WSGI script for use with Django". I do recommend you read all that. It's stuff you should know. But the bottom line is Django had a dev server built in and although it makes dev really easy to get into it's troublesome when you move the code to production. That happens because production code will run on mod_wsgi, entirely different beast from the dev server.
At this point I have Django running in a vhost using the django.wsgi script from Graham Dumpleton. I had to compile the MySQL extension and also add
VERSIONER_PYTHON_PREFER_32_BIT=yes
in my .profile so I force Python to run in 32 bit mode.
My vhost looks like this:


    DocumentRoot "/Users/cosmin.cimpoi/Work/Projects/DjangoSandbox/work/code/www/httpdocs/webroot"
    ServerName djangosandbox.tld
    ErrorLog "logs/djangosandbox.tld-error_log"
    CustomLog "logs/djangosandbox.tld-access_log" common
    
    WSGIDaemonProcess djangosandbox.tld threads=15 user=cosmin.cimpoi group=staff
 WSGIProcessGroup djangosandbox.tld
 WSGIScriptAlias / "/Users/cosmin.cimpoi/Work/Projects/DjangoSandbox/work/code/www/httpdocs/webroot/django.wsgi"
 Alias /media/ /Library/Python/2.6/site-packages/django/contrib/admin/media/
    
    
     AllowOverride All
     Options Indexes FollowSymLinks 
     Order deny,allow
  Allow from all
 

the PyDev project layout:
and the django.wsgi:

import sys

appParentPath = '/Users/cosmin.cimpoi/Work/Projects/DjangoSandbox/work/code/www/httpdocs/src'
appPath = '/Users/cosmin.cimpoi/Work/Projects/DjangoSandbox/work/code/www/httpdocs/src/DjangoSandbox'
if appParentPath not in sys.path:
    sys.path.append(appParentPath)
if appPath not in sys.path:
    sys.path.append(appPath)

import settings

import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')

command.validate()

import django.conf
import django.utils

django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

So far the result in browser is great. I get a big fat It worked! :-)
Also I got South running just because migrations was one of the main things that what got me interested in Django in the first place.
So far Python + Django looks promising. Different from my fav combo of PHP + CakePHP. Good different and bad different. Bad because it's a such hassle to get it running and good because it loks and feels more polished and proper. I suspect that having it in production will be an even bigger hassle but I really think in the end it's well worth the effort. I definitely want to finish the sample Django app and move forward from there. In case I get into trouble I'll post about it as always :-)

Monday, March 14, 2011

monday money maker

Jason Fried got good at making money when he was young. Now he's great!