Problem: I want to serve my Plyons app from the same server, on the same port as my other PHP based projects. All the tutorials tell me to start my pylons app by running “paster serve –reload development.ini”, but as I read that I thought “Apache is my webserver, I don’t need two” 🙂
My Solution: Using mod_wsgi to run my pylons app.
Simply edit Apache’s configuration file for the server/domain in question and add this line to the
ServerName myhelloworldapp.com WSGIScriptAlias / /var/www/myhelloworldapp.com/index.wsgi #... the rest of your vhost config
Notice the index.wsgi – this is going to be what loads your pylons app. I choose ‘index.wsgi’, but feel free to use any name you like.
The contents of index.wsgi simply tell Python where your app is, adds its base-path and then starts it:
APP_BASE = "/var/www/myhelloworldapp.com/helloworld/" APP_CONFIG = "/var/www/myhelloworldapp.com/helloworld/development.ini" sys.path.append(APP_BASE) os.environ['PYTHON_EGG_CACHE'] = '/usr/local/pylons/python-eggs' from paste.deploy import loadapp application = loadapp("config:" + APP_CONFIG)
That’s really all there is to it! I’m eager to get more into Python web development. I’ve had my head in PHP development for too long!
Why not just enable mod_python and set an “AddHandler”? Performance! mod_python is kind of inefficient with the way it runs Python apps. As I understand it (please correct me if I’m wrong), it spawns a whole new Python thread for each Apache instance – this can consume memory and CPU like crazy!