'Setting up gunicorn.service [Service] for Django app

Following the documentation and online tutorials on setting up my gunicorn.service results in ModuleNotFoundError: No module named 'my-app' when I run sudo systemctl status gunicorn

I realise something is being imported wrong or I do not have the correct directory listed in my gunicorn.service, but I'm struggling to figure out where the issue is regarding the [Service] part.

My /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

My directories are as follows:

./
./
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

Also, my settings.py since it might be relevant:

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/'
                                    

Please could you help me with my EnvironmentFile, WorkingDirectory, ExecStart. Also, is my WSGI_APPLICATION = 'my-app.app.wsgi.application'or WSGI_APPLICATION = 'app.wsgi.application'? I feel like i've made this kind of mistake using the 2 interchangeably in many files, so pleas elet me know in which other files I would have to correct for this. Thank you so much in advance!



Solution 1:[1]

Everything looks good, you may be having a little confusion as to the structure of your WorkingDirectory and your ExecStart. In any case as you edit and test. Do in this order:

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

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