如何在启动EC2实例上启动gunicorn?

问题描述 投票:1回答:1

我试图在我的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

amazon-ec2 gunicorn systemd
1个回答
1
投票

我最终创建了一个虚拟环境,一切都运行良好,最终成为我在/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

© www.soinside.com 2019 - 2024. All rights reserved.