5

I'm using django and now trying to configure server with nginx and uwsi. now i'm running nginx+socket+uwsgi_emperor well . for running uwsgi i use below command in terminal:

 /usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data

and it's working great. now i want add service with systemctl {start|stop|restart|status} uwsgi.service so i can easily use uwsgi. the problem here occur.here is /etc/systemd/system/uwsgi.service:

[Unit]
Description=uWSGI Emperor
After=syslog.target

[Service]
# i'm putting code same code of terminal exactly here:
ExecStart=/usr/local/bin/uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
# Requires systemd version 211 or newer
RuntimeDirectory=uwsgi
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target

now when i start it with systemctl start uwsgi.service its running and i can see systemctl status uwsgi.service thaat is loaded well but when i want access the ip the error appear in journalctl -b -u uwsgi that shown my django can't access(read) the environment variable.because i using several environment variable in django's setting so without value of them django can't work correctly.why this happen?(when i running uwsgi in command line all is good but when i want use same command in systemctl django can't read environment variable and error occur ). thank you

mehdi
  • 209
  • 1
  • 2
  • 7

2 Answers2

6

that shown my django can't access(read) the environment variable

Because systemd is not running in a shell with your user and that user environment and those variables.

when i running uwsgi in command line

then you are using your own user with that environment and those variables.


You need a

EnvironmentFile=/dir/to/file

before your ExecStart. Or if it is not that many you can also do

Environment={VAR}={VALUE}

(one line per variable) where

  • {VAR} is the name of your environment variable
  • {VALUE} is the value of that {VAR}
Rinzwind
  • 309,379
1

One other approach in addition to the accepted answer is to have this /bin/bash -lc '<your command>' for your ExecStart or ExecStop

ExecStart=/bin/bash -lc 'yarn start'

and have a corresponding /etc/profile file with the environment variables.