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

时间:2019-04-02 09:23:12

标签: amazon-ec2 gunicorn systemd

我正在尝试在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返回绝对路径,所以我不明白为什么找不到金枪鱼

1 个答案:

答案 0 :(得分: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