Ubuntu VPS部署Django时Gunicorn启动失败的解决方法
解决Gunicorn启动失败(status=203/EXEC)导致的Nginx 502 Bad Gateway错误
问题场景
在Hostinger提供的Ubuntu系统VPS上部署Django项目,采用Gunicorn搭配Nginx的架构,持续出现502 Bad Gateway错误,排查发现Gunicorn无法启动。
执行命令查看Gunicorn状态:
sudo systemctl status gunicorn
返回错误信息:
× gunicorn.service - gunicorn daemon Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2024-01-02 07:53:47 UTC; 4 days ago TriggeredBy: ● gunicorn.socket Main PID: 6153 (code=exited, status=203/EXEC) CPU: 1ms Jan 02 07:53:47 srv457341 systemd[1]: Started gunicorn daemon. Jan 02 07:53:47 srv457341 systemd[6153]: gunicorn.service: Failed to execute /root/Assistanza/Assistanza/env/bin/gunicorn: No such file or directory Jan 02 07:53:47 srv457341 systemd[6153]: gunicorn.service: Failed at step EXEC spawning /root/Assistanza/Assistanza/env/bin/gunicorn: No such file or directory Jan 02 07:53:47 srv457341 systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC Jan 02 07:53:47 srv457341 systemd[1]: gunicorn.service: Failed with result 'exit-code'. Jan 02 07:53:47 srv457341 systemd[1]: gunicorn.service: Start request repeated too quickly. Jan 02 07:53:47 srv457341 systemd[1]: gunicorn.service: Failed with result 'exit-code'. Jan 02 07:53:47 srv457341 systemd[1]: Failed to start gunicorn daemon.
当前Gunicorn服务文件(/etc/systemd/system/gunicorn.service)内容:
[Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] User=root Group=www-data WorkingDirectory=/root/Assistanza/Assistanza ExecStart=/root/Assistanza/Assistanza/env/bin/gunicorn \ --access-logfile - \ --workers 3 \ --bind unix:/run/gunicorn.sock \ Assistanza.wsgi:application [Install] WantedBy=multi-user.target
解决步骤
问题根源是虚拟环境中Gunicorn未正确安装,按以下步骤修复:
- 删除原有异常的虚拟环境,进入项目根目录:
cd /root/Assistanza/Assistanza - 新建虚拟环境:
python3 -m venv new_env(可自定义虚拟环境名称) - 激活虚拟环境:
source new_env/bin/activate - 重装Gunicorn:
pip install gunicorn - 获取Gunicorn的正确执行路径:
which gunicorn,会返回类似/root/Assistanza/Assistanza/new_env/bin/gunicorn的路径 - 编辑Gunicorn服务文件,将
ExecStart后的路径替换为上一步得到的正确路径 - 重新加载systemd配置并重启服务:
sudo systemctl daemon-reload sudo systemctl start gunicorn - 再次执行
sudo systemctl status gunicorn确认服务启动成功
内容的提问来源于stack exchange,提问作者TheLostChild
相关产品推荐
相关产品推荐

