我试图在我的EC2实例内启动时运行gunicorn,我在myproject.service
中创建了一个/lib/systemd/system/
文件,看起来像这样。
[Unit]
Description="my startup file"
[Service]
WorkingDirectory=/home/ubuntu/myproject
Type=simple
ExecStart=/home/ubuntu/.local/bin/gunicorn -w 1 -b 0.0.0.0:8080 wsgi:application
[Install]
WantedBy=multi-user.target
为了测试它是否正常工作,我一直在运行这些命令
sudo systemctl daemon-reload
sudo systemctl start myproject
sudo systemctl status myproject
这会返回错误
Apr 02 09:14:13 ip-172-31-32-45 gunicorn[5827]: File "/home/ubuntu/.local/bin/gunicorn", line 7, in <module>
Apr 02 09:14:13 ip-172-31-32-45 gunicorn[5827]: from gunicorn.app.wsgiapp import run
Apr 02 09:14:13 ip-172-31-32-45 gunicorn[5827]: ModuleNotFoundError: No module named 'gunicorn'
Apr 02 09:14:13 ip-172-31-32-45 systemd[1]: myproject.service: Main process exited, code=exited, status=1/FAILURE
Apr 02 09:14:13 ip-172-31-32-45 systemd[1]: myproject.service: Failed with result 'exit-code'.
我用which gunicorn
返回绝对路径所以我不明白为什么找不到gunicorn
我最终创建了一个虚拟环境,一切都运行良好,最终成为我在/lib/systemd/system
内的服务文件
# myproject.service
[Service]
WorkingDirectory=/home/ubuntu/myproject
Type=simple
Environment="PATH=/home/ubuntu/myproject/venv36/bin"
ExecStart=/home/ubuntu/myproject/venv36/bin/gunicorn -w 1 -b 0.0.0.0:8080 wsgi:application --daemon
[Install]
WantedBy=multi-user.target