首页 > 解决方案 > 为 Django 应用设置 gunicorn.service [Service]

问题描述

在我运行时按照文档和在线教程设置我的gunicorn.service结果ModuleNotFoundError: No module named 'my-app'sudo systemctl status gunicorn

我意识到某些东西被错误地导入了,或者我的目录中没有列出正确的目录gunicorn.service,但我正在努力找出与 [Service] 部分有关的问题所在。

我的/etc/systemd/system/gunicorn.service

[Unit]
    Description=gunicorn daemon
    Requires=gunicorn.socket
    After=network.target

[Service]
User=myname
Group=myname
EnvironmentFile=/home/myname/my-app/my-app/env
WorkingDirectory=/home/myname/my-app/my-app/app
ExecStart=/home/myname/env/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
         my-app.app.wsgi:application

[Install]
WantedBy=multi-user.target

我的目录如下:

./
./
env/ (./ ../ bin/ include/ lib/ lib64 -> lib/ pyvenv.cfg share/)
.bash_history
.bash_logout
.bashrc
.cache/
.cloud-locale-test.skip
.gitconfig
.local/
.profile
.ssh/
.sudo_as_admin_successful
.vim/
.viminfo
my-app/
   |_ .DS_Store
   |_ .git/
   |_ .idea/
   |_ .travis.yml
   |_ Dockerfile
   |_ docker-compose.yml
   |_ env/ (./ ../ bin/ include/ lib/ lib64 -> lib/ pyvenv.cfg share/)
   |_ requirements.txt
   |_ my_app/
         |_ .flake8                 
         |_ db.sqlite3
         |_ env
         |_ manage.py*
         |_ static/
         |_  app/
             |_ ./
             |_./
             |___init__.py
             |___pycache__/
             |_asgi.py
             |_settings.py
             |_urls.py
             |_wsgi.py

另外,我的settings.py因为它可能是相关的:

from pathlib import Path

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


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'seeecret'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app'
    'my-app'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'app.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
           'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'my-app.app.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

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


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
          },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'
                                    

请你帮我解决我的 EnvironmentFile, WorkingDirectory, ExecStart. 另外,是我的WSGI_APPLICATION = 'my-app.app.wsgi.application'还是WSGI_APPLICATION = 'app.wsgi.application'?我觉得我在许多文件中交替使用 2 时犯了这种错误,所以请让我知道我必须在哪些其他文件中对此进行更正。非常感谢您!

标签: djangonginxgunicornwsgisystemd

解决方案


一切看起来都不错,您可能对您的结构WorkingDirectory和您的ExecStart. 无论如何,在您编辑和测试时。按此顺序执行:

(1) sudo vi /etc/systemd/system /gunicorn.service
(2) sudo systemctl daemon-reload
(3) sudo systemctl restart gunicorn
(4) sudo systemctl status gunicorn

推荐阅读