django - 简单的内部服务器错误,因为运行 uwsgi 不断删除 .sock 文件

标签 django nginx uwsgi internal-server-error

我正在尝试按照 these steps 部署我的第一个 Django 应用程序.

我遇到的问题是,由于某种原因,在激活我的 venv< 后运行 uwsgi --emperor venv/vassals/--uid www-data --gid www-data/code> 似乎只是删除了根目录中的 .sock 文件。我认为这是导致问题的原因,因为查看 nginx /var/log/nginx/error.log 中的日志表明:

2021/10/11 22:09:57 [crit] 4281#4281: *6 connect() to unix:///var/www/dolphin/dolphin.sock failed (2: No such file or directory) while connecting to upstream, client: 81.102.82.13, server: example.com, request: "GET /favicon.ico HTTP/2.0", upstream: "uwsgi://unix:///var/www/dolphin/dolphin.sock:", host: "example.com", referrer: "https://example.com/

这是我在/etc/nginx/sites-available中的配置文件(symbolic linkedsites-enabled):

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

# The upstream component nginx needs to connect to
upstream django {
    server unix:///var/www/dolphin/dolphin.sock;
    #server unix:/var/www/dolphin/dolphin.sock;
}

# Configuration of the server
server {
    listen      443 ssl;
    server_name example.com;
    charset     utf-8;

    # Maximum upload size
    client_max_body_size 75M;

    # Django media and static files
    location /media  {
        alias /var/www/dolphin/app/media;
    }
    location /static {
        alias /var/www/dolphin/app/static;
    }

    # Send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /var/www/dolphin/uwsgi_params;
    }
}

我的dolphin_uwsgi.ini文件:

[uwsgi]

# Full path to Django project's root directory
chdir            = /var/www/dolphin/
# Django's wsgi file
module           = /var/www/dolphin/app/app/wsgi.py
# Full path to python virtual environment
home             = /var/www/dolphin/venv/

# Enable uwsgi master process
master          = true

# Maximum number of worker processes
processes       = 10

# The socket (use the full path to be safe
socket          = /var/www/dolphin/dolphin.sock

# Socket permissions
chmod-socket    = 666

# Clear environment on exit
vacuum          = true

# Daemonize uwsgi and write messages into the given log
daemonize       = /var/www/dolphin/uwsgi-emperor.log

每当由于某种原因在浏览器中加载站点时(即激活我的 venv 并运行 uwsgi --emperor venv/vassals/--uid www-data 后,我都会不断收到内部服务器错误--gid www-data ),我不知道为什么,因为教练似乎和我一样这样做,但这对他有用。

有解决办法吗?

如果它可能有帮助,这里是 settings.py 的一些可能相关的部分:

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

ALLOWED_HOSTS = ['example.com']

WSGI_APPLICATION = 'app.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'app/static')
]

MEDIA_URL = '/images/'

使用 uwsgi --socket dolphin.sock --module app/app/wsgi.py 生成套接字文件也会返回

ModuleNotFoundError: No module named 'app/app/wsgi unable to load app 0 (mountpoint='') (callable not found or import error) *** no app loaded. going in full dynamic mode

我认为这是因为应用程序名为 app - 我的 wsgi.py 文件结构是 dolphin/app/app/wsgi.py,但即使尝试此操作也会失败:uwsgi --socket dolphin.sock --module/var/www/dolphin/app/app/wsgi.py

我认为这可能是因为我像这篇文章一样将其称为“应用程序”,但它使用 Flask ,而不是 Django: Flask and uWSGI - unable to load app 0 (mountpoint='') (callable not found or import error)

说实话,我很困惑,不知道需要更改什么,也不知道要传递到此命令的路径:uwsgi --socket dolphin.sock --module app/app/wsgi - -chmod-socket=666 - 我不相信 app/app/wsgi 是正确的路径,尽管它看起来应该是。

文件夹结构:

海豚:

  • README.md
  • dolphin.sock(运行容易出现错误的命令后生成)
  • 需求.txt
  • uwsgi_params
  • 应用程序
    • db.sqlite3
    • 管理.py
    • 应用程序
      • init.py
      • pycache
      • asgi.py
      • 设置.py
      • url.py
      • wsgi.py
    • 媒体
    • 静态
    • 还有其他模块
  • dolphin_uwsgi.ini
  • uwsgi-emperor.log
  • venv

文件wsgi.py

"""
WSGI config for app project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')

application = get_wsgi_application()

uwsgi --socket dolphin.sock --module app/app/wsgi --chmod-socket=666 的完整输出:

machine: aarch64
clock source: unix
detected number of CPU cores: 4
current working directory: /var/www/dolphin
detected binary path: /var/www/dolphin/venv/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 3244
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to UNIX address dolphin.sock fd 3
Python version: 3.8.10 (default, Sep 28 2021, 16:10:42)  [GCC 9.3.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0xaaaaf5c10c10
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72904 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
ModuleNotFoundError: No module named 'app/app/wsgi'
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: 129638, cores: 1)

最佳答案

# full path to Django project's root directory
chdir            = /var/www/dolphin/
# Django's wsgi file
module           = /var/www/dolphin/app/app/wsgi.py

由于 manage.py 位于 dolphin/app 内,因此它是 Django 项目的根目录。

# full path to Django project's root directory
chdir            = /var/www/dolphin/app
# Django's wsgi file
wsgi-file        = app/wsgi.py

在命令行上:uwsgi --socket dolphin.sock --chdir app --wsgi-file app/wsgi.py

引用: Quickstart for Python/WSGI applications

关于django - 简单的内部服务器错误,因为运行 uwsgi 不断删除 .sock 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69533195/

相关文章:

redirect - 如何在 ssl 中重定向两个域?

http - 运行 uwsgi emperor 模式给出 nginx 错误

python - Django pymssql 迁移

python - Django RadioSelect 从模型中选择

python - 如何解决托管在 heroku 上的 django 项目的迁移问题?

python - 网络抓取有模式吗?

SSL 证书不受信任 - 中间证书

python - 使用 Flask、Nginx 和 uwsgi 获取 404

python - 在 Flask-UWSGI 应用程序中处理耗时的请求

docker - 无法在 Alpine 上安装 uwsgi