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

Simple internal server error because running uwsgi keeps on deleting the .sock file

我正在尝试按照 these steps.

部署我的第一个 Django 应用程序

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

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 也 returns

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.pydolphin/app/app/wsgi.py ,但即使这样尝试也会失败:uwsgi --socket dolphin.sock --module /var/www/dolphin/app/app/wsgi.py

我想可能是因为我这样称呼它'app' post,但是它使用了Flask, not 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是正确的路径,虽然它看起来应该是。

文件夹结构:

海豚:

文件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.pydolphin/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