[Answered ]-My virtualenv is not taking into account ? [apache, mod_wsgi and django]

1👍

Your apache.conf looks reasonable… here is one of mine as a comparison

WSGIScriptAlias / ${SRV}/www/example/wsgi.py
WSGIProcessGroup www.example.no
WSGIDaemonProcess www.example.no \
    display-name=example \
    threads=50 \
    maximum-requests=10000 \
    umask=0002 \
    inactivity-timeout=3600 \
    home=${SRV}/www/example \
    python-path=${SRV}/www:${SRV}/src:${SRV}/venv/dev/lib/python2.7/site-packages \
    python-eggs=${SRV}/.python-eggs

the ${SRV} comes from mod_define which I’ve manually compiled for Apache 2.2, but should be included in 2.4(?)

You should follow the steps listed here: https://code.google.com/p/modwsgi/wiki/VirtualEnvironments. It is important to note, however, that even when using a virtualenv you need to use the same Python version as mod_wsgi was built with (and it looks like you’re trying to use Py3 on a Py2 configuration).

I’ve modified the wsgi.py file to read config values from site.ini:

from ConfigParser import ConfigParser
import sys, os

# server variables
SITE_ROOT = os.path.split(__file__)[0]
WWW = os.path.split(SITE_ROOT)[0]

config = ConfigParser()
config.read([os.path.join(DK_SITE_ROOT, 'site.ini')])

# site properties
SITE_NAME = config.get('site', 'sitename')
DNS = config.get('site', 'dns')
SRV = config.get('server', 'srv')
# virtualenv variables
VENV_NAME = "dev"   # only

# derived settings
DJANGO_SETTINGS_MODULE = "%s.settings" % SITE_NAME
VIRTUAL_ENV = "%s/venv/%s" % (SRV, VENV_NAME)

# activate virtualenv
# (warning: https://code.google.com/p/modwsgi/wiki/CheckingYourInstallation)
_activate = "%s/%s/activate_this.py" % (
    VIRTUAL_ENV,
    'Scripts' if sys.platform == 'win32' else 'bin'
)
if sys.version_info >= (3, 0):
    exec(compile(open(_activate, 'rb').read(), _activate, 'exec'))
else:
    execfile(_activate, dict(__file__=_activate))

# make site directory importable
sys.path.insert(0, WWWDIR)

# set all GLOBAL vars as environment variables
for _varname in [k for k in globals().keys() if k == k.upper()]:
    _val = globals()[_varname]
    if type(_val) == str:
        os.environ[_varname] = _val

# This application object is used by the development server
# as well as any WSGI server configured to use this file. 
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

(it’s been a while since I tested this with Py3, but it works great with Py2).

1👍

I normally configure the virtualenv import in my wsgi.py file and it works. Just by adding the lines:

site.addsitedir('/home/{my_user}/.virtualenvs/{virtualenv}/local/lib/python2.7/site-packages')
activate_env=os.path.expanduser("/home/{my_user}/.virtualenvs/{virtualenv}/bin/activate_this.py")
try:
    execfile(virtualenv, dict(__file__=virtualenv))
except:
    pass

And that’s the configuration for my virtualhost:

<VirtualHost yourhost>
     ServerName server.name.com
     WSGIScriptAlias / /path/to/wsgi.py
     Alias /static/ /path/to/static/
     Alias /media/ /path/to/media/
     DocumentRoot /path/to/project

     ErrorLog /var/log/apache2/project_error.log
</VirtualHost>

Something more detailed here.

👤argaen

Leave a comment