0

I'm using a .env file which contains the configuration for my Django app. I have a systemd service which runs Daphne (similar to what's below)

[Unit]
Description=WebSocket Daphne Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/home/django/myproject/src
ExecStart=/home/django/myproject/venv/bin/python /home/django/myproject/venv/bin/daphne -e ssl:8001:privateKey=/etc/letsencrypt/live/myproject.com/privkey.pem:certKey=/etc/letsencrypt/live/myproject.com/fullchain.pem myproject.asgi:application  
Restart=on-failure

[Install]
WantedBy=multi-user.target

Moreover, I'm using gunicorn through a similar mechanism, which works perfect. However, Daphne does not.

When I run it through systemctl start daphne.service, it tells me that Django settings are improperly configured So, I tried setting the dotenv in the asgi.py file like so:

dotenv.load_dotenv( os.path.join(os.path.dirname(__file__), '.env' ))
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings.dev')

if os.getenv('DJANGO_SETTINGS_MODULE`):
    os.environ['DJANGO_SETTINGS_MODULE'] = os.getenv('DJANGO_SETTINGS_MODULE')

But this just gives me the same error. Any ideas how to fix this? I checked out this response, but it seems ridiculous/redundant to set the environment variables in daphne.service.

nonamorando
  • 1,556
  • 2
  • 14
  • 33

1 Answers1

1

The issue appears to be the path you have specified for your ExecStart line

You have:

ExecStart=/home/django/myproject/venv/bin/python ...

which should be

ExecStart=/home/django/myproject/venv/bin/daphne ...

see https://build.vsupalov.com/django-systemd-crashcourse/

Cal Abel
  • 173
  • 1
  • 4