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

Posted

技术标签:

【中文标题】即使所有配置文件都正确,gunicorn 服务也没有启动【英文标题】:gunicorn service is not starting even with all configuration files are correct 【发布时间】:2021-08-23 15:37:07 【问题描述】:

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 = '*****'

帮帮我!

【问题讨论】:

有些奇怪:在您的配置文件中 gunicorn 路径是 /home/developer/myprojectdir/myprojectenv/bin/gunicorn 并且在错误日志中您的 guncorn 路径是 /home/bharathwajan/bharathwajan/django/ lib/python3.8/site-packages/gunicorn 。请向我们展示您的目录结构。 我在提问时附上了我的目录结构图片 谢谢,现在也给我们看看你的 settings.py 我编辑了问题并添加了我的设置文件 我认为“/home/developer/myprojectdir/myprojectenv/bin/gunicorn”这条路径是正确的,因为我是根据数字海洋“digitalocean.com/community/tutorials/…”的文档完成的 【参考方案1】:

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

【讨论】:

【参考方案2】:

我认为你配置错了这一行。

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吗?

【讨论】:

以上是关于即使所有配置文件都正确,gunicorn 服务也没有启动的主要内容,如果未能解决你的问题,请参考以下文章

配置 Gunicorn:未指定应用程序模块

python gunicorn 配置文件在哪

如何使用 gunicorn 为 django 配置 nginx?

请验证实例名称是不是正确并且 sql server 已配置为允许远程连接; 花了一下午也没解决,求大神

让 gunicorn 运行的正确方法是啥?

如何在 Digital Ocean 的源代码控制中包含 nginx 和 gunicorn 配置文件?