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

I'm trying to deploy my first Django application by following these steps.

The problem I have is that, for some reason, running uwsgi --emperor venv/vassals/ --uid www-data --gid www-data after activating my venv just seems to delete the .sock file in my root directory. I assume this is causing the problem because looking at the nginx logs at /var/log/nginx/error.log suggest so:

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/

Here is my configuration file in /etc/nginx/sites-available (symbolic linked to sites-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;
    }
}

My dolphin_uwsgi.ini file:

[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

I keep on getting an internal server error whenever loading the site in a browser for some reason (that is, after activating my venv and running uwsgi --emperor venv/vassals/ --uid www-data --gid www-data ) and I don't know why, as the instructor seems to do it the same way as I am, but it works for him.

Is there a solution?

In case it might help, here's some of the potentially relevant parts of 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/'

Generating the socket file with uwsgi --socket dolphin.sock --module app/app/wsgi.py also 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

I think it's because the application is called app - my file structure to wsgi.py is dolphin/app/app/wsgi.py, but even trying this fails: uwsgi --socket dolphin.sock --module /var/www/dolphin/app/app/wsgi.py

I think it might be because I called it 'app' like this post, but that uses Flask, not Django: Flask and uWSGI - unable to load app 0 (mountpoint='') (callable not found or import error)

I'm pretty confused to be honest and don't know what I need to change, nor what path to pass into this command: uwsgi --socket dolphin.sock --module app/app/wsgi --chmod-socket=666 - I'm not confident that app/app/wsgi is the correct path although it seemingly should be.

Folder structure:

dolphin:

  • README.md
  • dolphin.sock (generated after running that bug-prone command)
  • requirements.txt
  • uwsgi_params
  • app
    • db.sqlite3
    • manage.py
    • app
      • init.py
      • pycache
      • asgi.py
      • settings.py
      • urls.py
      • wsgi.py
    • media
    • static
    • also other modules
  • dolphin_uwsgi.ini
  • uwsgi-emperor.log
  • venv

File 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()

Full output of 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)


Solution 1:[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

Since manage.py is inside dolphin/app, that's your Django project's root directory.

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

On the command line: uwsgi --socket dolphin.sock --chdir app --wsgi-file app/wsgi.py

Reference: Quickstart for Python/WSGI applications

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen