【问题标题】:Internal Server Error for Django with uwsgi / nginx on Debian8.7在 Debian8.7 上使用 uwsgi / nginx 的 Django 内部服务器错误
【发布时间】:2017-08-31 09:33:31
【问题描述】:

我是服务器技术的初学者,我正在努力将 Django 应用程序部署到 Debian8 服务器。 我一直在关注:this tutorial,但我无法启动 uWSGI 服务,所以我关注了this recommendation 并切换到systemd。 然后我可以在端口:8000 上以开发模式运行我的应用程序,但无法在浏览器中加载它。

日志说:no python application found, check your startup logs for errors

在日志中的此注释上方,它抱怨无法加载 whitenoise 模块:

File "./testdjango/wsgi.py", line 35, in <module>
    from whitenoise.django import DjangoWhiteNoise

这意味着(如果我没记错的话)它不会加载虚拟环境。

更新

如果我运行 uwsgi 终端命令,它就不再起作用了 uwsgi --http :8000 --chdir /home/djangouser/testdjango --module testdjango.wsgi:application --env DJANGO_SETTINGS_MODULE=testdjango.settings --home /home/djangouser/Env/myapp_env

我不知道为什么会发生这种情况,如果有任何帮助或指导,我将不胜感激。

我在此处发布此问题之前进行了搜索,事实上,过去三天我一直在尝试许多不同的解决方案,但均未成功。因此,我的文件现在似乎很乱。对此我深表歉意。

我使用 django 1.8,virtualenvvrapper,sudo 用户名为 djangouser,服务器上的文件夹结构如下:

-home
  |
  |--djangouser
    |
    |--Env
    |   |--myapp_env
    |
    |--testdjango
       |
       |-- app
       |-- testdjango
       |     |
       |     |-- wsgi.py
       |     |-- settings.py
       |
       |-- uwsgi.ini
       |-- manage.py

/etc/uwsgi/sites/testdjango.ini文件:

[uwsgi]
project = testdjango
base = /home/djangouser

chdir = %(base)/%(project)
home = %(base)/Env/myapp_env
module = %(project).wsgi:application
env = DJANGO_SETTINGS_MODULE=%(project).settings

master = true
processes = 5

socket = %(base)/%(project)/%(project).sock
chmod-socket = 666
vacuum = true

/etc/systemd/system/uwsgi.service文件:

[Unit]
Description=uWSGI Emperor service
After=syslog.target

[Service]
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/sites

Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

/etc/nginx/sites-available/testdjango文件:

server {
    listen 80;
    server_name xx.xx.xx.xx myapp.com www.myapp.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/djangouser/testdjango/static/;
    }

    location /media  {
        root /home/djangouser/testdjango/media/;
    }

    location / {
        include         uwsgi_params;
        uwsgi_pass      unix:/home/djangouser/testdjango/testdjango.sock;
    }
}

wsgi.py文件:

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

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

更新 2

uwsgi --http :8000 --chdir /home/djangouser/testdjango --module testdjango.wsgi:application --env DJANGO_SETTINGS_MODULE=testdjango.settings --home /home/djangouser/Env/myapp_env
*** Starting uWSGI 2.0.15 (64bit) on [Wed Apr  5 14:48:34 2017] ***
compiled with version: 4.9.2 on 04 April 2017 17:12:38
os: Linux-4.9.7-x86_64-linode80 #2 SMP Thu Feb 2 15:43:55 EST 2017
nodename: xxonexx
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /home/djangouser/Env/myapp_env/lib/python2.7/site-packages
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/djangouser/testdjango
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3934
your memory page size is 4096 bytes
detected max file descriptor number: 65536
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 5293)
uwsgi socket 0 bound to TCP address 127.0.0.1:36933 (port auto-assigned) fd 3
Python version: 2.7.9 (default, Jun 29 2016, 13:11:10)  [GCC 4.9.2]
Set PythonHome to /home/djangouser/Env/myapp_env
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x12036b0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72760 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
HI!
Djangomode is: 
None
Traceback (most recent call last):
  File "./testdjango/wsgi.py", line 38, in <module>
    from whitenoise.django import DjangoWhiteNoise
  File "/home/djangouser/Env/myapp_env/local/lib/python2.7/site-packages/whitenoise/django.py", line 8, in <module>
    from django.contrib.staticfiles.storage import staticfiles_storage
  File "/home/djangouser/Env/myapp_env/local/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 12, in <module>
    from django.core.cache import (
  File "/home/djangouser/Env/myapp_env/local/lib/python2.7/site-packages/django/core/cache/__init__.py", line 34, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "/home/djangouser/Env/myapp_env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/home/djangouser/Env/myapp_env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/djangouser/Env/myapp_env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 92, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "./testdjango/settings.py", line 59, in <module>
    ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS').split(',')
AttributeError: 'NoneType' object has no attribute 'split'
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 5292, cores: 1)

【问题讨论】:

  • 你能给我们看看你的 uwsgi 日志吗?另外,如果您愿意再次从头开始设置所有内容,我写了a quick guide 以供我自己将来参考。只需将任何地方的“moco”更改为您自己的项目名称即可。应该在任何干净的 ubuntu/debian 安装中工作并构建一个完全干净的 uwsgi / nginx / django / psql 堆栈。
  • 此外,您有 99% 的机会正在处理 PATH 问题或缺少适当的权限。
  • 谢谢@andre-terra,我刚刚发布了日志。它只是不加载 venvwrapper 环境。我同意PATH。如果我现在不修复 uwsgi 的当前状态,看来我将不得不再次从头开始。在这种情况下,我一定会听从你的指示。
  • 看来here 可能是解决方案的关键。我不确定如何运行“venv-uwsgi”

标签: python django ubuntu nginx uwsgi


【解决方案1】:

在生产中,你需要在settings.py中编辑ALLOWED_HOSTS

The official docs 显示以下示例:

ALLOWED_HOSTS = [
    '.example.com',  # Allow domain and subdomains
    '.example.com.',  # Also allow FQDN and subdomains
]

您可能应该遵循 /etc/nginx/sites-available/testdjango 文件开头的所有内容。

您在开发中看不到此设置的原因是该设置会自动填充“localhost”,因此它永远不会为空。

【讨论】:

  • 问题是我在 postactivate 文件中使用 thi 变量作为环境变量:export ALLOWED_HOSTS="example.com" 我还有一堆我需要的其他环境变量。这就是为什么用 uwsgi 加载 venv 至关重要,这就是我失败的地方:)
  • 意思是说我在settings.py中使用os.getenv ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS').split(',')
  • @user2241956 您是否尝试将venv 设置添加到指向虚拟环境所在文件夹的uwsgi .ini?
  • 我刚刚尝试过,但没有帮助。如果我尝试从项目根目录运行uwsgi --ini uwsgi.ini,我会遇到同样的错误
  • 只有当我通过workon myapp_env手动启动虚拟环境时才有效
【解决方案2】:

好的,经过四天的努力,我找到了问题所在。 在第三次重新开始这个问题并检查服务器上的错误消息后,我发现我应该将我的环境变量导入到 uwsgi ini 文件中。 之后,uwsgi.log 中的错误消失了,应用开始按预期运行。

我真的希望这个答案能在将来节省一些时间。

【讨论】:

  • 你能举个例子说明你是怎么做到的吗
  • @Sevenearths 是的,当然。我将 venv vars 添加到 /etc/uwsgi/sites/mysite.ini,如下所示:env = DJANGO_SETTINGS_MODULE=mysite.settings env = ALLOWED_HOSTS=mysite.com env = DJANGO_MODE=Production
猜你喜欢
  • 1970-01-01
  • 2015-07-21
  • 2019-02-05
  • 2015-09-22
  • 2013-07-07
  • 2017-12-06
  • 2014-02-05
  • 1970-01-01
  • 2014-07-31
相关资源
最近更新 更多