I'm running Ubuntu 9.04 (Jaunty) and I'll step through the following setup:
- python-virtualenv
- TurboGears2
- Apache2
- mod_wsgi
Why use python-virtualenv?
TurboGears2 is a fan of python2.5, Ubuntu is a fan of python2.6, and I'm a fan of not having to resolve version conflicts.
python --version sudo apt-get install python2.5 sudo apt-get install python-virtualenv # Here I set up a shared environment for all wsgi apps that may need python2.5 cd /usr/local/lib # or wherever you stick your shared libraries sudo virtualenv --python=python2.5 --no-site-packages python2.5-wsgi source python2.5-wsgi/bin/activate # use the new environment python --version deactivate # go back to the regular python environment
Install TG2 using the new environment
sudo apt-get install python2.5-dev sudo mkdir /var/webapps/ sudo chown `whoami`:`whoami` -R /var/webapps cd /var/webapps/ # Use and modify the virtual environment we just created sudo su - source /usr/local/lib/python2.5-wsgi/bin/activate easy_install -i \ http://www.turbogears.org/2.0/downloads/current/index \ tg.devtools deactivate exit # verify that it installed correctly by running help source /usr/local/lib/python2.5-wsgi/bin/activate paster --help
Creating the project using TG2
(still in the new environment)
paster quickstart Edumacate edumacate yes cd Edumacate
Create the project-specific virtual environment
(in /var/webapps/Edumacate)
(using python2.5-wsgi)
virtualenv --python=python2.5 py2.5-edumacate deactivate # now to the generic environment source py2.5-edumacate/bin/activate python --version
Test the project in the new environment
easy_install -i \ http://www.turbogears.org/2.0/downloads/current/index \ tg.devtools python setup.py develop paster serve development.ini & # visit http://localhost:8080/ in a browser on the local machine # (links2 is a great cli browser if your server is headless)
As a test that I did get two unique virtual environments with no extra libraries I'll install a new library in the default environment and see if I get it in either of the other two.
deactivate python >> import mutagen Traceback (most recent call last): File "", line 1, in ImportError: No module named mutagen # CTRL-D sudo apt-get install python-mutagen python >> import mutagen # CTRL-D source py2.5-edumacate/bin/activate python >> import mutagen Traceback (most recent call last): File "", line 1, in ImportError: No module named mutagen # CTRL-D
Good, it works (and doesn't work) as expected. (mutagen is an ID3 / audio tagging library that I plan on using in another project)
Configure Edumacate for Apache
You need to have a python egg cache directory that apache can write to
cd /var/webapps/Edumacate mkdir py2.5-edumacate/wsgi-cache sudo chown www-data:www-data py2.5-edumacate/wsgi-cache mkdir wsgi-config vim wsgi-config/wsgi-development.py
import os, sys, site
os.environ['PYTHON_EGG_CACHE'] = '/var/webapps/Edumacate/py2.5-edumacate/wsgi-cache'
site.addsitedir('/var/webapps/Edumacate/py2.5-edumacate/lib/python2.5/site-packages')
sys.path.insert(0, '/var/webapps/Edumacate')
sys.path.insert(1, '/var/webapps/Edumacate/py2.5-edumacate/lib/python2.5')
from paste.deploy import loadapp
application = loadapp('config:/var/webapps/Edumacate/edumacate/config/development.ini')
Configure Apache for Edumacate
sudo apt-get install libapache2-mod-wsgi sudo vim /etc/apache2/mods-enabled/wsgi.conf
# add the following line with the path to the environment you share among all of your TG2 webapps WSGIPythonHome /usr/local/lib/python2.5-wsgi
sudo touch /etc/apache2/sites-available/edumacate-dev sudo a2ensite edumacate-dev sudo vim /etc/apache2/sites-enabled/edumacate-dev
<VirtualHost *:80>
ServerName edumacate.coolaj86.info
# we'll make the root directory of the domain call the wsgi script
WSGIScriptAlias / /var/webapps/Edumacate/wsgi-config/wsgi-deployment.py
# You can tweak this next line to your heart's content
# based on the mod_wsgi documentation, but this should work for now.
#WSGIDaemonProcess myuser threads=1 display-name=%{GROUP}
#WSGIProcessGroup myuser
# make the wsgi script accessible
<Directory /var/webapps/Edumacate/wsgi-config>
Order allow,deny
Allow from all
</Directory >
# We'll need to create exceptions for all static content.
# Aliases to serve general project media
Alias /styles /var/webapps/Edumacate/edumacate/public/styles
Alias /admin /var/webapps/Edumacate/edumacate/public/admin
Alias /images /var/webapps/Edumacate/edumacate/public/images
Alias /scripts /var/webapps/Edumacate/edumacate/public/scripts
# Make all the static directories accessible
<Directory /var/webapps/Edumacate/edumacate/public>
Order allow,deny
Allow from all
</Directory >
</VirtualHost>
Test and Complete
sudo /etc/init.d/apache2 restart # Browse to http://edumacate.coolaj86.info
Resources:
http://simplestation.com/locomotion/mod_wsgi-with-apache-2-on-centos-4/
http://simplestation.com/locomotion/turbogears-2-tg2-with-mod_wsgi-and-virtual-environments/
