uWSGI + Django + Nginx - 运行时错误

Posted

技术标签:

【中文标题】uWSGI + Django + Nginx - 运行时错误【英文标题】:uWSGI + Django + Nginx - Runtime Error 【发布时间】:2018-08-11 23:34:21 【问题描述】:

肯定需要关于 *** 的第一篇文章 + 帮助:

我正在尝试设置一个 Ubuntu 16.04 服务器来托管我的 Django 应用程序,但是我遇到了一个奇怪的 uWSGI 错误;每当我跑步时:

uwsgi --socket test.sock --module api.wsgi:app --chmod-socket=66

我明白了:

Python version: 3.5.2 (default, Nov 23 2017, 16:37:01)  [GCC 5.4.0 20160609]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x8025d0
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 890, in _find_spec
AttributeError: 'ConfigurationImporter' object has no attribute 'find_spec'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./api/wsgi.py", line 14, in <module>
    from configurations.wsgi import get_wsgi_application
  File "/usr/local/lib/python3.5/dist-packages/configurations/wsgi.py", line 14, in <module>
    application = get_wsgi_application()
  File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 22, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 954, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 892, in _find_spec
  File "<frozen importlib._bootstrap>", line 873, in _find_spec_legacy
  File "/usr/local/lib/python3.5/dist-packages/configurations/importer.py", line 132, in find_module
    imp.find_module(module, path))
  File "/usr/lib/python3.5/imp.py", line 270, in find_module
    "not ".format(type(name)))
RuntimeError: 'list' must be None or a list, not <class 'str'>
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** uWSGI is running in multiple interpreter mode ***

这是我的 wsgi.py 文件:

import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Production')

from configurations.wsgi import get_wsgi_application

application = get_wsgi_application()

这是我的 uwsgi.ini 文件:

[uwsgi]

project = mysite
base = /home/ubuntu

chdir = %(base)/%(project)
home = %(base)/%(project)/env
module = %(project).wsgi:application

vacuum = true


# process-related settings
master = true
processes = 8
socket = /run/uwsgi/mysite.sock
chmod-socket = 666
max-requests = 50000

logto = /var/log/uwsgi/app/logs.log

为了更好的衡量,这是我的 nginx 文件:

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django 
    server unix:///home/ubuntu/mysite/test.sock; # for a file socket
    #server 127.0.0.1:8001; # for a web port socket (we'll use this first)


# configuration of the server
server 
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name mysite.io; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  
        alias /home/ubuntu/mysite/static;  # your Django project's media files - amend as required
    

    location /static 
        alias /home/ubuntu/mysite/media; # your Django project's static files - amend as required
    

    # Finally, send all non-media requests to the Django server.
    location / 
        uwsgi_pass  django;
        include     uwsgi_params; # the uwsgi_params file you installed
    

我真的不知道是什么原因造成的。我现在尝试使用 Gunicorn,它显示了同样的错误。非常感谢任何帮助! :)

【问题讨论】:

欢迎来到 Stack Overflow! Stack Overflow 不是一个讨论论坛,它是一个问答网站,您可以在其中提出可以回答而不是讨论的特定编程问题。请阅读How do I ask a good question? 和What topics can I ask about here?,然后编辑您的问题以符合站点指南。诸如此类的离题问题通常会关闭,但如果经过编辑以提出可回答问题,则可以再次重新打开。谢谢。 【参考方案1】:

在 uwsgi.py 你有:

application = get_wsgi_application()

你正在运行带有参数的uwsgi:

uwsgi --socket test.sock --module api.wsgi:app --chmod-socket=66

尝试用api.wsgi:application 替换api.wsgi:app,看看它是否运行。

【讨论】:

以上是关于uWSGI + Django + Nginx - 运行时错误的主要内容,如果未能解决你的问题,请参考以下文章

通过 Nginx 的 uwsgi + django - uwsgi 设置/生成?

转载nginx+uwsgi+django

Django+Nginx+uWSGI = 504 网关超时

uWSGI + Nginx + Django 部署

Django + Uwsgi + Nginx 实现生产环境部署

Django + Uwsgi + Nginx 实现生产环境部署