【问题标题】:How to start gunicorn on startup EC2 instance?如何在启动 EC2 实例上启动 gunicorn?
【发布时间】:2019-04-02 09:23:12
【问题描述】:

我试图在我的 EC2 实例内启动时运行 gunicorn,我在 /lib/systemd/system/ 中创建了一个看起来像这样的 myproject.service 文件。

[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】:

    我最终创建了一个虚拟环境,然后一切正常,这最终成为我在 /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
    
    

    【讨论】:

      最近更新 更多