Django(mod_wsgi)处理WSGI脚本时发生异常

时间:2015-11-10 20:47:51

标签: python django apache ubuntu mod-wsgi

我是Apache和mod_wsgi的初学者。我理解Apache服务器的基本思想。我需要一个救生员,因为我几乎没有人要求帮助。

我的问题如下:

  1. 我可以运行python manage.py runserver 0.0.0.0:8000,我的应用程序在服务器上正常运行
  2. 但是,当我使用Apache服务器来服务我的网站时。事情并不顺利。
  3. 返回的两个主要错误是:

     ImproperlyConfigured: Error loading psycopg2 module: /opt/payupjack/payupjack-env/lib/python2.7/site-packages/psycopg2/_psycopg.so: wrong ELF class: ELFCLASS32
    

      ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
    

    文件夹结构

    payupjack
      |-payupjack-env (virtualenv for payupjack)
          |- (virtualenv stuffs...)
          |- PayupJack (My application)
               |- All the code required by this application 
    

    我尝试了什么

    1. 将用户所有权更改为' www-data'对于virtualenv的site-package文件夹中的文件夹django_openid

    2. 添加了www-data'用户来自“ubuntu'小组(以便'数据'共享相同的特权' ubuntu')

    3. 跑步' wsgi.py'单独,运行良好。

    4. 正常运行python manage.py runserver

    5. 尝试注释掉模块utils中导入consumer.py的行(在django_openid模块中),但我仍然得到同样的错误。这真的很奇怪,我不知道如何理解它。

    6. 在此问题的不同变体上遵循了许多Stackoverflow建议。到目前为止,没有任何工作。

    7. 额外信息

      完整的Apache错误日志返回以下内容。

      [Tue Nov 10 20:02:12.411960 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
      [Tue Nov 10 20:02:12.411987 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] Traceback (most recent call last):
      [Tue Nov 10 20:02:12.412002 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
      [Tue Nov 10 20:02:12.412091 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     self.load_middleware()
      [Tue Nov 10 20:02:12.412105 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
      [Tue Nov 10 20:02:12.412209 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     mod = import_module(mw_module)
      [Tue Nov 10 20:02:12.412224 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
      [Tue Nov 10 20:02:12.412262 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     __import__(name)
      [Tue Nov 10 20:02:12.412272 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django_openid/consumer.py", line 14, in <module>
      [Tue Nov 10 20:02:12.412390 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     from django.shortcuts import render_to_response
      [Tue Nov 10 20:02:12.412403 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/shortcuts/__init__.py", line 10, in <module>
      [Tue Nov 10 20:02:12.412467 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     from django.db.models.manager import Manager
      [Tue Nov 10 20:02:12.412477 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/db/__init__.py", line 78, in <module>
      [Tue Nov 10 20:02:12.412525 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     connection = connections[DEFAULT_DB_ALIAS]
      [Tue Nov 10 20:02:12.412535 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
      [Tue Nov 10 20:02:12.412595 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     backend = load_backend(db['ENGINE'])
      [Tue Nov 10 20:02:12.412615 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/db/utils.py", line 33, in load_backend
      [Tue Nov 10 20:02:12.412630 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     return import_module('.base', backend_name)
      [Tue Nov 10 20:02:12.412640 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
      [Tue Nov 10 20:02:12.412652 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     __import__(name)
      [Tue Nov 10 20:02:12.412659 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 24, in <module>
      [Tue Nov 10 20:02:12.412726 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632]     raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
      [Tue Nov 10 20:02:12.412743 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:12632] ImproperlyConfigured: Error loading psycopg2 module: /opt/payupjack/payupjack-env/lib/python2.7/site-packages/psycopg2/_psycopg.so: wrong ELF class: ELFCLASS32
      [Tue Nov 10 15:02:13.107340 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
      [Tue Nov 10 15:02:13.107383 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] Traceback (most recent call last):
      [Tue Nov 10 15:02:13.107402 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
      [Tue Nov 10 15:02:13.107426 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]     self.load_middleware()
      [Tue Nov 10 15:02:13.107436 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
      [Tue Nov 10 15:02:13.107449 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
      [Tue Nov 10 15:02:13.107467 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
      [Tue Nov 10 15:02:13.407277 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
      [Tue Nov 10 15:02:13.407306 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] Traceback (most recent call last):
      [Tue Nov 10 15:02:13.407322 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
      [Tue Nov 10 15:02:13.407343 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]     self.load_middleware()
      [Tue Nov 10 15:02:13.407352 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
      [Tue Nov 10 15:02:13.407364 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
      [Tue Nov 10 15:02:13.407381 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
      [Tue Nov 10 15:02:14.197060 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
      [Tue Nov 10 15:02:14.197098 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] Traceback (most recent call last):
      [Tue Nov 10 15:02:14.197114 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
      [Tue Nov 10 15:02:14.197136 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]     self.load_middleware()
      [Tue Nov 10 15:02:14.197144 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
      [Tue Nov 10 15:02:14.197157 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
      [Tue Nov 10 15:02:14.197174 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
      [Tue Nov 10 15:02:14.252928 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] mod_wsgi (pid=20821): Exception occurred processing WSGI script '/opt/payupjack/payupjack-env/PayupJack/wsgi.py'.
      [Tue Nov 10 15:02:14.252946 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] Traceback (most recent call last):
      [Tue Nov 10 15:02:14.252958 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
      [Tue Nov 10 15:02:14.252975 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]     self.load_middleware()
      [Tue Nov 10 15:02:14.252983 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]   File "/opt/payupjack/payupjack-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
      [Tue Nov 10 15:02:14.252994 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512]     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
      [Tue Nov 10 15:02:14.253006 2015] [:error] [pid 20821:tid 139665119233792] [remote 192.0.163.234:7512] ImproperlyConfigured: Error importing middleware django_openid.consumer: "cannot import name utils"
      

      wsgi.py的名称为:

      from django.core.handlers.wsgi import WSGIHandler
      
      
      import os
      
      import sys
      
      import site
      
      ALLDIRS = ['/opt/payupjack/payupjack-env/PayupJack']
      
      # redirect sys.stdout to sys.stderr for bad libraries like geopy that uses
      
      # print statements for optional import exceptions.
      
      sys.stdout = sys.stderr
      
      prev_sys_path = list(sys.path)
      
      print prev_sys_path
      
      site.addsitedir('/opt/payupjack/payupjack-env/lib/python2.7/site-packages')
      
      sys.path.append('/opt')
      sys.path.append('/opt/payupjack')
      sys.path.append('/opt/payupjack/payupjack-env')
      sys.path.append('/opt/payupjack/payupjack-env/PayupJack') 
      
      # Reorder sys.path so new directories at the front.
      
      new_sys_path = []
      
      for item in list(sys.path):
      
          if item not in prev_sys_path:
      
              new_sys_path.append(item)
      
              sys.path.remove(item)
      
      sys.path[:0] = new_sys_path
      
      activate_this = '/opt/payupjack/payupjack-env/bin/activate_this.py'
      
      execfile(activate_this, dict(__file__=activate_this))
      
      from os.path import abspath, dirname, join
      
      
      print  sys.path
      
      os.environ["DJANGO_SETTINGS_MODULE"] = "PayupJack.settings"
      
      sys.path.insert(0, join("/opt/payupjack/payupjack-env/PayupJack", "apps"))
      
      
      import djcelery
      
      djcelery.setup_loader()
      
      
      print 'Last step of WSGI'
      
      application = WSGIHandler()
      
      print 'Done final step. '
      

      这是我的Apache配置文件:

      WSGIPythonPath /opt/payupjack/payupjack_env/lib/python2.7
      
      <VirtualHost *:80>
      
              ServerAdmin webmaster@localhost
      
              WSGIDaemonProcess payupjack python-path=/opt/payupjack/payupjack-env:/opt/payupjack/payupjack-env/lib/python2.7/site-packages processes=1 threads=1
      
              WSGIProcessGroup payupjack
      
              WSGIApplicationGroup %{GLOBAL}
      
              WSGIPassAuthorization On
      
      
      
              WSGIScriptAlias / /opt/payupjack/payupjack-env/PayupJack/wsgi.py
      
              Alias /static/ /opt/payupjack/payupjack-env/PayupJack/static
      
              <Directory /opt/payupjack/payupjack-env/PayupJack>
      
                      Order deny,allow
      
                      #Allow from all
              Require all granted
              </Directory>
      
              ErrorLog /opt/payupjack/payupjack-env/logs/error.log
      
              CustomLog /opt/payupjack/payupjack-env/logs/access.log combined
      
      </VirtualHost>
      

      新信息

      ldd的{​​{1}}返回:

      mod_wsgi.so

      我也快速查找了我的整个硬盘,并且只发现了一个匹配上面的python库,位于:

      linux-vdso.so.1 =>  (0x00007fff815f2000)
              libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007ff5b3fec000)
              libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007ff5b3a88000)
              libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007ff5b36c2000)
              /lib64/ld-linux-x86-64.so.2 (0x00007ff5b443c000)
              libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007ff5b34a9000)
              libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007ff5b32a5000)
              libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007ff5b30a1000)
              libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007ff5b2d9b000)
      

1 个答案:

答案 0 :(得分:1)

问题是由于路径问题(感谢Graham指出如何检查psycopg2.so的位数和位置)。我的应用程序使用VirtualEnv。出于某些原因,我没有第一次正确设置VirtualEnv。因此,通过pip安装的所有软件包都保留在操作系统的python路径中,而不是VirtualEnv。

解决方案是删除操作系统Python路径上安装的所有软件包。然后,重新创建一个新的VirtualEnv并执行典型的pip install。确保所有软件包都安装在VirtualEnv路径中。一旦我这样做,一切都很美妙。