1

我正在尝试设置 uWSGI 来为我的 django 应用程序提供服务,但我遇到了路径问题,或者至少我认为这是一个路径问题。症状是当我向应用程序发出 curl 请求时,我得到的回溯表明找不到基本的 python/django 模块。前任:

File "/opt/example/venvs/example/lib/python2.7/site-packages/django/test/_doctest.py", line 104, in <module> import unittest, difflib, pdb, tempfile
File "/usr/lib/python2.7/pdb.py", line 59, in <module>
class Pdb(bdb.Bdb, cmd.Cmd): AttributeError: 'module' object has no attribute 'Cmd'

我查看了其他与 SO 相关的问题,但似乎都不是我的问题。

这是我的设置:

  • django==1.5.1
  • uwsgi==1.9.15(从 pip 安装)
  • 蟒蛇是2.7
  • Ubuntu 12.04 LTS
  • nginx = 0.8.54 (但我不相信这是相关的,因为我还没有将 uwsgi 连接到 nginx )

我在一个 vagrant 创建的 virtualbox 中运行,由 chef 和 fab 完成所有典型的配置。

这是我 ssh 进入机器后的虚拟机路径:

PATH="/opt/example/venvs/example/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/vagrant_ruby/bin"

这是 .wsgi 文件(相当标准,除了我为调试添加的打印路径):

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "example.settings")

import sys
print sys.path

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

这是 uwsgi ini 文件(注意我必须添加的所有各种 pythonpath):

[uwsgi]
#socket=/tmp/hs.com.sock
virtualenv=/opt/example/venv
chdir=/opt/example/apps/example/src
module=example.wsgi:application
master=true
workers=8
pidfile=/opt/example/apps/example/src/example/uwsgi-master.pid
max-requests=5000
#plugins=python
enable-threads=true
processes=2
no-site=true
http=:8003
#env=DJANGO_SETTINGS_MODULE=example.settings
pythonpath=..
pythonpath=/vagrant/src
pythonpath=/opt/example/apps/example/src/example
pythonpath=/opt/example/venvs/example/lib/python2.7
pythonpath=/opt/example/venvs/example/lib/python2.7/plat-linux2
pythonpath=/opt/example/venvs/example/lib/python2.7/lib-tk
pythonpath=/opt/example/venvs/example/lib/python2.7/lib-old
pythonpath=/opt/example/venvs/example/lib/python2.7/lib-dynload
pythonpath=/opt/example/venvs/example/lib/python2.7/distutils
#pythonpath=/opt/example/venvs/example/lib/python2.7/encodings
#pythonpath=/opt/example/venvs/example/lib/python2.7/config
pythonpath=/usr/lib/python2.7/
pythonpath=/usr/lib/python2.7/lib-dynload/
pythonpath=/usr/lib/python2.7/distutils/
pythonpath=/usr/lib/python2.7/plat-linux2/
pythonpath=/usr/lib/python2.7/lib-tk/
#pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages/django/
#pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages/django/core/
pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages/django/core/management
#pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages/django/core/management/commands
pythonpath=/opt/example/venvs/example/local/lib/python2.7/site-packages
pythonpath=/opt/example/venvs/example/lib/python2.7
pythonpath=/opt/example/venvs/example/lib/python2.7/site-packages
pythonpath=/vagrant/src/example

里面有这么多 pythonpaths 的原因是应用程序无法找到非常基本的模块,所以我不断添加路径来解决它。这适用于几个错误,但是 1)我找不到上面的 Cmd attributerror 的正确路径和 2)添加这些单独的路径中的每一个都感觉不对。

uwsgi 输出到 'uwsgi --ini inifile'

[uWSGI] getting INI configuration from xxx
*** Starting uWSGI 1.9.15 (32bit) on [Fri Sep 13 18:12:44 2013] ***
compiled with version: 4.6.3 on 13 September 2013 06:46:44
os: Linux-3.2.0-23-generic-pae #36-Ubuntu SMP Tue Apr 10 22:19:09 UTC 2012
nodename: xxx
machine: i686
clock source: unix
detected number of CPU cores: 1
current working directory: /vagrant/src
writing pidfile to /opt/example/apps/example/src/example/uwsgi-master.pid
detected binary path: /opt/example/venvs/example/bin/uwsgi
your processes number limit is 1824
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uWSGI http bound on localhost:8003 fd 4
uwsgi socket 0 bound to TCP address 127.0.0.1:34622 (port auto-assigned) fd 3
Python version: 2.7.3 (default, Apr 10 2013, 06:03:17)  [GCC 4.6.3]
Set PythonHome to /opt/example/venv
Python main interpreter initialized at 0xa044e58
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 192012 bytes (187 KB) for 2 cores
*** Operational MODE: preforking ***
added ../ to pythonpath.
added /vagrant/src/ to pythonpath.
added /opt/example/apps/example/src/example/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/plat-linux2 to pythonpath.
added /opt/example/venvs/example/lib/python2.7/lib-tk to pythonpath.
added /opt/example/venvs/example/lib/python2.7/lib-old to pythonpath.
added /opt/example/venvs/example/lib/python2.7/lib-dynload/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/distutils/ to pythonpath.
added /usr/lib/python2.7/ to pythonpath.
added /usr/lib/python2.7/lib-dynload/ to pythonpath.
added /usr/lib/python2.7/distutils/ to pythonpath.
added /usr/lib/python2.7/plat-linux2/ to pythonpath.
added /usr/lib/python2.7/lib-tk/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/site-packages/django/core/management/ to pythonpath.
added /opt/example/venvs/example/local/lib/python2.7/site-packages/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/ to pythonpath.
added /opt/example/venvs/example/lib/python2.7/site-packages/ to pythonpath.
added /vagrant/src/example/ to pythonpath.
['/vagrant/src/example/', '/opt/example/venvs/example/lib/python2.7/site-packages/', '/opt/example/venvs/example/lib/python2.7/', '/opt/example/venvs/example/local/lib/python2.7/site-packages/', '/opt/example/venvs/example/lib/python2.7/site-packages/django/core/management/', '/usr/lib/python2.7/lib-tk/', '/usr/lib/python2.7/plat-linux2/', '/usr/lib/python2.7/distutils/', '/usr/lib/python2.7/lib-dynload/', '/usr/lib/python2.7/', '/opt/example/venvs/example/lib/python2.7/distutils/', '/opt/example/venvs/example/lib/python2.7/lib-dynload/', '/opt/example/venvs/example/lib/python2.7/lib-old', '/opt/example/venvs/example/lib/python2.7/lib-tk', '/opt/example/venvs/example/lib/python2.7/plat-linux2', '/opt/example/venvs/example/lib/python2.7/', '/opt/example/apps/example/src/example/', '/vagrant/src/', '../', '.', '', '/opt/example/venv/lib/python2.7/', '/opt/example/venv/lib/python2.7/plat-linux2', '/opt/example/venv/lib/python2.7/lib-tk', '/opt/example/venv/lib/python2.7/lib-old', '/opt/example/venv/lib/python2.7/lib-dynload', '/vagrant/src/example']
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0xa044e58 pid: 2911 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 2911)
spawned uWSGI worker 1 (pid: 2912, cores: 1)
spawned uWSGI worker 2 (pid: 2913, cores: 1)
spawned uWSGI http 1 (pid: 2914)

从另一个终端我运行 curl -v localhost:8003 并得到我在上面发布的“AttributeError ...”。卷曲输出为:

curl: (52) Empty reply from server
* Closing connection #0

这是我尝试过的:

  1. 使用最新的 uWSGI 版本 1.9.15 和 LTS 版本 1.4.x
  2. 在 virtualenv 内部和外部安装 uWSGI
  3. django 的 runserver 可以很好地为我的应用程序提供服务,并且我可以看到由于 wsgi 文件中的 debug print sys.path 语句而打印出来的路径,它只是我在 wsgi.ini 文件中拥有的路径的一个子集
  4. 还从 wsgi.py 文件中打印出 runserver 和 uwsgi 环境中的环境变量,我看不到任何重要的不同之处
  5. uwsgi 能够毫无问题地提供基本的 test.py 应用程序文件,所以我相信这消除了任何安装/设置问题
  6. 尝试使用 sudo 运行 uwsgi

我还尝试了一个基本的 django 应用程序(直接从 startproject 创建),这个也失败了,具有相同的属性错误,并且需要相同的 pythonpaths 列表。

我一定缺少一些简单的设置或配置......?如果需要更多信息,请告诉我。

Edit1:这看起来不是特定于 uWSGI,而是与 virtualenv 设置和路径更相关。我安装了 gunicorn 并得到了完全相同的结果。

4

1 回答 1

0

我认为您的 uwsgi.ini 文件缺少一个“s”

virtualenv=/opt/example/venv
于 2013-09-18T06:51:19.553 回答