I'm trying to set up a Django server with Nginx + uWSGI on Ubuntu 18.04 on an Amazon EC2 instance.
** Various versions ** Nginx: 1.14.0 uWSGI: 2.0.18 Python: 3.7.5 Django: 2.2.11
First, in order to start uWSGI, I created the following file called uwsgi-hogehoge.service.
[Unit]
Description=uWSGI service for mysite
[Service]
User=ubuntu
ExecStart=/bin/bash -c 'sh /hogehoge/start.sh'
ExecStartPre=/bin/bash -c 'sh /hogehoge/start_pre.sh'
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
start.sh contains the process to start uwsgi.
I created a service file to start uWSGI and Nginx, and put a symbolic link to systemd. And
$ systemctl start uwsgi-hogehoge.service
When I try to start the service with, apparently it is not possible as far as I can see the status.
$ journalctl -xe
When I checked the log with, the following error was displayed.
uwsgi: command not found
・ The uwsgi pass is normal. -It is not an error related to permissions such as permissions. -Even if there is an error around the authority in the first place, the appropriate authority is given to the file and the user.
From the above, it seems that uwsgi's path is normally passed, but I questioned the hypothesis that systemd's path is not passed only when it is executed as a startup command.
After all there is a great ancestor in everything. I found the following god article and solved everything.
Make systemd read user environment variables
Quoting from the article above,
When I looked it up, systemd was in .barshrc and .bash_profile I found that it didn't read the environment variables I defined.
It seems that ...
$ echo $PATH
Display the path with. Then
/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/.local/bin
It was displayed like this. Then I created a sysconfig directory in / etc and created a file with an appropriate name. In my case, I was a user named ubuntu, so I changed the file name to ubuntu. And I edited the ubuntu file as follows.
# cat <_EOF_ > /etc/sysconfig/ubuntu
PATH=/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/ubuntu/.local/bin/usr/local/bin
_EOF_
Next, set the path to the file you created earlier in the uwsgi-hogehoge.service file.
[Unit]
Description=uWSGI service for mysite
[Service]
User=ubuntu
EnvironmentFile=/etc/sysconfig/ubuntu <--- Add here! !!
ExecStart=/bin/bash -c 'sh /hogehoge/start.sh'
ExecStartPre=/bin/bash -c 'sh /hogehoge/start_pre.sh'
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
[Install]
WantedBy=multi-user.target
Now you can read environment variables properly when you start the service with systemd.
$ systemctl daemon-reload
Load the service file update properly and try again
$ systemctl start uwsgi-hogehoge
This time it started properly.
Recommended Posts