apache server not using proper virtualenv with WSGI setting

旧街凉风 提交于 2019-12-10 11:27:46

问题


I am facing a problem related to django wsgi script. I have been using two virtualenv for my two application and I have deployed these two application on my local server with different port. Apache configuration file for first Appplication looks like:

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>

and Apache configuration for second application is almost same but instead of virtual-env1, I am using virtual-env2 and different port. But when I run my second application on the server I got this error.

**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']

My django.wsgi file loks like this:

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

I don't know why my server is looking inside the virtual-env1 instead of virtual-env2. Please help me as I am new to django and wsgi.


回答1:


Here's what helped me in similar situation.

My wsgi file looks like this:

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

May be this isn't the best approach, but it worked for me.

I remember that I've googled and found many different solutions, here's some related links:

  • Setting up Apache and Python WSGI to use VirtualEnv
  • Setting up django on apache (mod_wsgi, virtualenv)
  • Django Apache wsgi virtualenv import error

Hope that helps.



来源:https://stackoverflow.com/questions/16233300/apache-server-not-using-proper-virtualenv-with-wsgi-setting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!