2

I am deploying a web2py application, and I will be serving it with Apache via mod_wsgi. I have the following WSGI directives in my virtual host configuration:

  WSGIDaemonProcess web2py user=www-data group=www-data \
                           display-name=%{GROUP}
  WSGIProcessGroup web2py
  WSGIScriptAlias / /var/www/web2py/wsgihandler.py

This is working, but it is taking the system wide python installation. As a result, some packages are not found (since they are only present in my virtualenv). I would like to tell this particular virtual host (or the whole Apache, if there is no other way), to use the python installation in my virtual environment (/home/myuser/.virtualenvs/python2.7.2/bin).

Is it possible to configure this for Apache? Or better, just for my virtual host? I would like to cause as little effect as possible to the rest of the system (specifically, I do not want to modify the default python version used system wide)

blueFast
  • 41,341
  • 63
  • 198
  • 344

1 Answers1

3

In your wsgihandler.py add this at the top...

activate_this = '/path/to/virtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this)
import sys
sys.path.insert(0, '/path/to/web2py_dir')
Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
  • Will this use the python in the virtual env, or will this just use the packages in the virtualenv? My system python is 2.6, and my virtualenv python is 2.7. My web2py application needs 2.7. In activate_this.py the following comment can be read: "This can be used when you must use an existing Python interpreter, not the virtualenv bin/python" – blueFast Aug 01 '12 at 12:52
  • First, you need to be sure `mod_wsgi` is compiled for your Python version. After you have done so, this configuration suggestion will work. – Mike Pennington Aug 01 '12 at 13:49
  • Why so? mod_wsgi will use the system python, or the one which is in its path, won't it? Surely I do not need to compile mod_wsgi for each different python version that I have installed in my system ... Besides, I am not compiling mod_wsgi: I am installing it from the Ubuntu repositories. – blueFast Aug 01 '12 at 15:15
  • RE: why `mod_wsgi` is specific to the python version. Please read Graham Dumpleton's [answer here](http://stackoverflow.com/a/8662381/667301). Also read [the mod_wsgi wiki entry about VirtualEnv](http://code.google.com/p/modwsgi/wiki/VirtualEnvironments): :Note that the version of Python from which this baseline environment is created must be the same version of Python that mod_wsgi was compiled for. It is not possible to mix environments based on different major/minor versions of Python." – Mike Pennington Aug 01 '12 at 15:17
  • 1
    Just for reference: I ended up recompiling mod_wsgi. For that I needed to recompile python with --enable-shared. Besides, I needed to make the libpython shared library available system wide with ldconfig – blueFast Aug 02 '12 at 14:36