Beto
Beto

Reputation: 806

Environment variables are not available with systemd

I am deploying a flask app that is running inside virtual env

I have the systemd file as follow:

[Unit]
Description=Gunicorn instance to serve my-page
After=network.target

[Service]
User=jb
Group=www-data
WorkingDirectory=/home/jb/webjosue/my-page
Environment="PATH=/home/jb/webjosue/FlaskENV/bin"
ExecStart=/home/jb/webjosue/FlaskENV/bin/gunicorn --workers 3 --bind unix:my-page.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

In my main.py I have

settings = os.environ['APP_SETTINGS']

and I get this error

raise KeyError(key)
Sep 24 00:10:13 ubuntu-512mb-nyc3-01 gunicorn[23439]: KeyError: 'settings'

I am guessing since I have my environment pointing to the virtualenv (FlaskENV) the other environment variables that are in my .profile are not being recognized.

Any ideas?

Upvotes: 0

Views: 1396

Answers (2)

Kallos Zsolty
Kallos Zsolty

Reputation: 251

Somehow Environment="FLASK_ENV=development" with quotes wasn't working

Environment=FLASK_ENV=development

with no quotes was working just fine

Upvotes: 0

As @Joe Doherty said here, you can use Environment directive in Service section to add environment variables, more information. For instance:

[Service]
Environment="PATH=/xx/yy/zz/venv/bin"
Environment="FLASK_ENV=development"
Environment="APP_SETTINGS=config.DevelopmentConfig"

I was using virtualenv so /xx/yy/zz/venv/bin is the path of virtualenv folder.

Upvotes: 1

Related Questions