【问题标题】:Failed to start Gunicorn启动 Gunicorn 失败
【发布时间】:2021-08-26 17:34:27
【问题描述】:

尝试在 ubuntu 上部署 django 应用。

start-server.sh 有

cd /home/django
source env/bin/activate
cd tutorial
gunicorn tutorial.wsgi

如果我 bash start-server.sh 一切运行良好。

所以,我写了以下内容。

gunicorn.service 保存在 /etc/systemd/system/ 看起来像

[Unit]
Description=Gunicorn
After=network.target

[Service]
Type=simple
User=django
ExecStart=/bin/bash /home/django/bin/start-server.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

然后我跑

 sudo systemctl enable gunicorn
 sudo systemctl start gunicorn

但现在我看到 502 错误。当我 bash start-server.sh 时,一切都很完美。但是,不知何故,gunicorn 无法正常工作。

guicorn 18.0 版(我试过 20.0 但没有运气)

sudo systemctl status gunicorn 显示

● gunicorn.service - Gunicorn
     Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2021-06-09 16:56:59 UTC; 2min 5s ago
   Main PID: 26285 (code=exited, status=126)

Jun 09 16:56:58 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Main process exited, code=exited, status=126/n/a
Jun 09 16:56:58 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Scheduled restart job, restart counter is at 5.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: Stopped Gunicorn.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Start request repeated too quickly.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: Failed to start Gunicorn.

【问题讨论】:

    标签: django ubuntu gunicorn


    【解决方案1】:

    不要在 bash 脚本中执行 cd-commands 而是使用完整路径

    source /home/django/env/bin/activate
    gunicorn /home/django/env/bin/activate/tutorial.wsgi
    

    【讨论】:

    • 显示 /bin/bash: /home/django/bin/start-server.sh: Permission denied 试过 sudo chown myuser /home/django/bin/start-server.sh -R 但没有运气
    【解决方案2】:

    start-server.sh之上添加以下行:

    #!/bin/bash
    

    这被称为“shebang”线。类 Unix 系统通常不注意文件扩展名,因此文件名以 .sh 结尾的事实被忽略。系统会查看这一行以确定该文件将由 bash 执行。

    另外,你需要确保文件是可执行的:

    chown +x /home/django/bin/start-server.sh
    

    然而,你真的根本不需要start-server.sh,因为它所做的一切都可以由 systemd 完成。将此行添加到 [Service] 部分:

    WorkingDirectory=/home/django/tutorial
    

    并将ExecStart 替换为:

    ExecStart=/home/django/env/bin/gunicorn tutorial.wsgi
    

    就是这样。如果您觉得它令人困惑,请阅读virtualenv demystified

    【讨论】:

    • #!/bin/bash 为我工作。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    • 2017-10-07
    • 2020-03-21
    • 2023-01-25
    • 2014-11-17
    • 2019-08-11
    • 2015-08-24
    相关资源
    最近更新 更多