即使所有配置文件都正确, gunicorn 服务也没有启动

问题描述

gunicorn 服务没有启动,即使我正确配置了一切,但它不起作用

请看一下我的目录结构

directory structure

这是我的服务文件

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

[Service]
User=developer
Group=www-data
WorkingDirectory=/home/developer/myprojectdir
ExecStart=/home/developer/myprojectdir/myprojectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          bharathwajan.wsgi:application

[Install]
WantedBy=multi-user.target

这是我的套接文件

[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

当我尝试使用 'sudo journelctl -u gunicorn' 检查 gunicorn 的日志时,它显示我没有名为 myproject.wsgi 的模块(这里 myproject 是 bharathwajan)

   ModuleNotFoundError: No module named 'bharathwajan.wsgi'
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]: [2021-06-02 11:06:35 +0000] [195812] [INFO] Booting worker with pid: 195812
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195811]: [2021-06-02 11:06:36 +0000] [195811] [INFO] Worker exiting (pid: 195811)
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195813]: [2021-06-02 11:06:36 +0000] [195813] [INFO] Booting worker with pid: 195813
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]: [2021-06-02 11:06:36 +0000] [195812] [ERROR] Exception in worker process
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]: Traceback (most recent call last):
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]:   File "/home/bharathwajan/bharathwajan/django/lib/python3.8/site-packages/gunicorn/arbiter.py",>
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]:     worker.init_process()
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]:   File "/home/bharathwajan/bharathwajan/django/lib/python3.8/site-packages/gunicorn/workers/base>
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]:     self.load_wsgi()
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]:   File "/home/bharathwajan/bharathwajan/django/lib/python3.8/site-packages/gunicorn/workers/base>
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]:     self.wsgi = self.app.wsgi()
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]:   File "/home/bharathwajan/bharathwajan/django/lib/python3.8/site-packages/gunicorn/app/base.py">
    Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]:     self.callable = self.load()

请看上面的错误有一行不合适的目录

Jun 02 11:06:36 ubuntu-s-1vcpu-1gb-blr1-01 gunicorn[195812]:   File "/home/bharathwajan/bharathwajan/django/lib/python3.8/site-packages/gunicorn/app/base.py">

这是错误吗,如果是,我该如何解决?,从哪里获取这个目录

我的 gunicorn 套接字处于活动状态并且只监听

developer@ubuntu-s-1vcpu-1gb-blr1-01:~/myprojectdir$ sudo systemctl status gunicorn.socket
● gunicorn.socket - gunicorn socket
     Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)
     Active: active (listening) since Sun 2021-06-06 06:23:00 UTC; 28min ago
   Triggers: ● gunicorn.service
     Listen: /run/gunicorn.sock (Stream)
     CGroup: /system.slice/gunicorn.socket

Jun 06 06:23:00 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Closed gunicorn socket.
Jun 06 06:23:00 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Stopping gunicorn socket.
Jun 06 06:23:00 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.

但是当我使用 journalctl 检查 gunicorn.socket 的日志时,它显示错误“服务启动限制命中”

developer@ubuntu-s-1vcpu-1gb-blr1-01:~/myprojectdir$ sudo journalctl -u gunicorn.socket
-- Logs begin at Mon 2021-05-31 02:03:42 UTC,end at Sun 2021-06-06 06:53:57 UTC. --
Jun 02 11:04:28 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 11:06:38 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 11:12:10 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 15:01:32 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 15:15:35 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 15:18:37 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
-- Reboot --
Jun 02 15:20:08 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 15:22:28 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 15:23:31 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 15:43:25 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 15:46:44 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 15:47:18 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 16:25:37 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 16:26:29 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 16:28:39 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 16:31:42 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 17:17:28 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 17:18:10 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 17:19:33 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 17:19:56 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 17:23:57 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 17:24:13 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 17:24:53 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 02 17:46:19 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 02 17:47:24 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 03 07:08:43 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 03 07:20:43 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 03 07:34:59 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 03 07:49:37 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 03 08:05:22 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 03 08:07:32 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 03 08:18:01 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 03 08:18:57 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 03 09:11:37 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 03 14:59:33 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 03 15:00:45 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 03 15:04:27 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 03 15:21:45 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 03 15:40:40 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 03 16:02:08 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Succeeded.
Jun 03 16:02:08 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Closed gunicorn socket.
Jun 03 16:02:08 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Stopping gunicorn socket.
Jun 03 16:02:08 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: Listening on gunicorn socket.
Jun 03 16:03:01 ubuntu-s-1vcpu-1gb-blr1-01 systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
lines 1-46

谁能告诉我错误在哪里?

我的 settings.py 文件

"""

Django settings for bharathwajan project.

Generated by 'django-admin startproject' using Django 2.2.5.

For more information on this file,see
https://docs.djangoproject.com/en/2.2/topics/settings/

For the full list of settings and their values,see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR,...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


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

# Security WARNING: keep the secret key used in production secret!
SECRET_KEY = '*********'

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

ALLOWED_HOSTS = ['my_ip','localhost']


# Application deFinition

INSTALLED_APPS = [
    'blog.apps.BlogConfig','django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',]

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 = 'bharathwajan.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates','Dirs': [os.path.join(BASE_DIR,'templates')],#this command makes the django to fetch html files automatically
        '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 = 'bharathwajan.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2','NAME': 'bharathwajan','USER': 'bharathwajan','PASSWORD': '*******','HOST':'localhost'
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/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/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'
STATICFILES_Dirs=[
    os.path.join(BASE_DIR,'static')
]
STATIC_ROOT= os.path.join(BASE_DIR,'assests')
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')



EMAIL_HOST = 'mail.privateemail.com'
EMAIL_USE_SSL = True
EMAIL_PORT = 465
EMAIL_HOST_USER = '**********'
EMAIL_HOST_PASSWORD = '*****'

帮帮我!

解决方法

这不是答案,但我建议开发人员使用 UWSGI 应用程序服务器而不是 gunicorn,与 gunicorn 相比,uwsgi 的配置非常简单且非常容易

,

我认为您配置此行错误。

ExecStart=/home/mainuser/Personal_Web/vr/bin/gunicorn \
      --access-logfile - \
      --workers 3 \
      --bind unix:/run/gunicorn.sock \
     bharathwajan.wsgi:application

您确定 bharathwajan.wsgi:application 中的 bharathwajan 是您在 Django 项目中的主文件夹吗?您的主文件夹是包含 settings.py 和 wsgi.py 的文件夹。 bharathwajan 似乎是您的项目文件夹,而不是您的主应用程序文件夹。

您可以尝试将其更改为 name_main_folder.wsgi:application 吗?

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...