apache服务器没有使用具有WSGI设置的正确virtualenv

时间:2013-04-26 09:39:01

标签: django apache python-2.7 wsgi django-wsgi

我遇到了与django wsgi脚本相关的问题。我一直在为我的两个应用程序使用两个virtualenv,我已经在我的本地服务器上使用不同的端口部署了这两个应用程序。第一个Appplication的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 个答案:

答案 0 :(得分:4)

这是在类似情况下帮助我的原因。

我的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()

可能这不是最好的方法,但它对我有用。

我记得我用谷歌搜索并发现了许多不同的解决方案,这里有一些相关的链接:

希望有所帮助。