apache 服务器没有使用正确的 virtualenv 和 WSGI 设置
Posted
技术标签:
【中文标题】apache 服务器没有使用正确的 virtualenv 和 WSGI 设置【英文标题】:apache server not using proper virtualenv with WSGI setting 【发布时间】:2013-04-20 10:37:18 【问题描述】:我遇到了一个与 django wsgi 脚本相关的问题。我一直在为我的两个应用程序使用两个 virtualenv,并且我已经将这两个应用程序部署在具有不同端口的本地服务器上。第一个应用程序的 Apache 配置文件如下所示:
listen 8081
WSGIPythonPath /home/user/app1:/home/user/virtual-env1/lib/python2.7/site-packages
<VirtualHost mylocalip:8081>
ServerAdmin webmaster@localhost
ServerName www.app1.com
DocumentRoot /home/user/app1
<Directory /home/user/app1/static-root>
Options Indexes
Order Allow,Deny
Allow from all
IndexOptions FancyIndexing
</Directory>
<Directory /home/user/app1>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
WSGIScriptAlias / /home/user/app1/django.wsgi
WSGIPassAuthorization On
Alias "/static" /home/user/workspace/app1/static_root
</VirtualHost>
第二个应用程序的 Apache 配置几乎相同,但我使用的是 virtual-env2 和不同的端口,而不是 virtual-env1。但是当我在服务器上运行我的第二个应用程序时,我得到了这个错误。
**AttributeError at /**
'Settings' object has no attribute 'DB_FILES'
Request Method: GET
Request URL: http://mylocalip:8091/
Django Version: 1.4.3
Exception Type: AttributeError
Exception Value: 'Settings' object has no attribute 'DB_FILES'
Exception Location: /home/user/virtual-env1/lib/python2.7/site-packages/django/utils/functional.py in inner, line 185
Python Executable: /usr/bin/python
Python Version: 2.7.2
Python Path:
['/home/user/virtual-env1/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg',
'/home/user/virtual-env1/lib/python2.7/site-packages/pip-1.1-py2.7.egg',
'/home/user/app1',
'/home/user/virtual-env1/lib/python2.7/site-packages',
'/usr/local/lib/python2.7/dist-packages/pip-1.1-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
'/usr/lib/python2.7/dist-packages/ubuntuone-couch',
'/usr/lib/python2.7/dist-packages/ubuntuone-installer',
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol',
'/home/user/app2',
'/home']
我的 django.wsgi 文件如下所示:
import os, sys
apache_configuration = os.path.dirname (__file__)
project = os.path.dirname (apache_configuration)
workspace = os.path.dirname (project)
sys.path.append ("/home/user/app2")
sys.path.append (workspace)
os.environ ['DJANGO_SETTINGS_MODULE'] = 'app2.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler ()
我不知道为什么我的服务器正在查看 virtual-env1 而不是 virtual-env2。请帮助我,因为我是 django 和 wsgi 的新手。
【问题讨论】:
【参考方案1】:这是在类似情况下帮助我的方法。
我的 wsgi 文件如下所示:
import os
import sys
# activate venv
activate_this = 'full_path_to_activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
# insert project path to sys path
path = 'full_path_to_your_project'
if path not in sys.path:
sys.path.insert(0, path)
import django.core.handlers.wsgi
os.environ['DJANGO_SETTINGS_MODULE'] = 'your_settings'
application = django.core.handlers.wsgi.WSGIHandler()
可能这不是最好的方法,但它对我有用。
我记得我搜索过很多不同的解决方案,这里有一些相关链接:
Setting up Apache and Python WSGI to use VirtualEnv Setting up django on apache (mod_wsgi, virtualenv) Django Apache wsgi virtualenv import error希望对您有所帮助。
【讨论】:
:: 你能描述什么是'full_path_to_activate_this.py',因为我对 django 完全陌生activate_this
应该在 bin
目录下的 virtualenv 文件夹中。以上是关于apache 服务器没有使用正确的 virtualenv 和 WSGI 设置的主要内容,如果未能解决你的问题,请参考以下文章
apache 服务器没有使用正确的 virtualenv 和 WSGI 设置