我正试图让apache使用特定virtualenv
的python可执行文件来交付驻留在该virtualenv
中的django测试站点,但它拒绝使用除默认系统python之外的任何东西。
我正在运行RedHat的服务器上工作。Apache2和mod_wsgi与python 2.6.6一起安装。我有一个运行在python2.4和django1.2上的旧站点,我想升级它,但首先我需要在使用python2.0的virtualenv
设置中使它在这些版本中运行。除了我似乎无法让apache更改用于交付django站点的python可执行文件之外,我的每一步都在工作。出于测试目的,我只是使用django默认的"It Work!"初始页面,然后故意强制出现编程错误,让django错误页面告诉我使用哪个python可执行文件来传递它。
我的httpd.conf
文件的VirtualHost
部分是:
<VirtualHost *:443>
#WSGIScriptAlias / var/www/mysite/index.wsgi
#WSGIDaemonProcess python-path=/home/jnett/python24sites/lib/python2.4/site-packages
<Location "/python24sitetest">
SetHandler python-program
PythonHandler django.core.handlers.modpython
PythonOption django.root /python24sitetest
SetEnv DJANGO_SETTINGS_MODULE python24sitetest.settings
SetEnv PYTHON_EGG_CACHE "/tmp"
PythonPath "['/var/www/python24sitetest', '/var/www'] + sys.path"
PythonDebug On
PythonInterpreter python24sitetest
</Location>
</VirtualHost>
我已经尝试了WSGIDaemonProcess
、WSGIPythonPath
和WSGIPythonHome
的所有可能的组合/排列,但无论我尝试什么,apache ALWAYS都只使用系统默认的python 2.6。
我如何强制apache使用它所在的virtualenv
的特定python可执行文件只交付这个特定的django站点?
[EDIT]我还尝试将WSGIScriptAlias
添加到httpd.conf
(如上所述),其中文件index.wsgi
位于django项目的主目录中,包含
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/jnett/python24sites/lib/python2.4/site-packages')
# Add the app's directory to the PYTHONPATH
sys.path.append('//home/jnett/python24sites/projects/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
# Activate your virtual env
activate_env=os.path.expanduser("/home/jnett/python24sites/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
我仍然没有任何效果。
在您的配置中,您实际上使用的是mod_python(这有点过时,与mod_wsgi完全不同)。
要使用mod_wsgi部署Django,基本上可以归结为以下内容,根据Django文档:
WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com
<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Order allow, deny
Allow from alll
</Files>
</Directory>
此外,您应该将mod_wsgi设置为守护程序模式,以隔离您的环境:
WSGIDaemonProcess example.com python-path=/path/to/mysite.com:/path/to/venv/lib/python2.7/site-packages
WSGIProcessGroup example.com
要更改给定环境的Python可执行文件,请查看WSGIPythonExecutable指令。
mod_python和mod_wsgi都是为特定的python版本编译的。如果它们是为Python 2.6编译的,则不能强制它们使用Python 2.4安装
您需要做的是从操作系统中卸载mod_python和mod_wsgi,然后根据python 2.4从源代码中编译mod_wsgi并安装它。
请注意,最新的mod_wsgi版本可能不再使用这样一个旧的Python版本进行编译。如果最新的mod_wsgi版本没有正确编译或运行,请返回并尝试mod_wsgi3.5版本的源代码。