You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在文件夹内通过WSGI运行Gunicorn失败问题求助

解决Gunicorn启动失败(status=203/EXEC)的问题

先理清楚你的项目和配置情况,再一步步排查问题:

你的项目结构

~/myproject/
- env
- server
  - api
  - home
    - settings
      - dev.py
      - prod.py
    - wsgi
      - dev.py
      - prod.py

相关配置内容

myproject/server/home/wsgi/dev.py

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "home.settings.dev")
application = get_wsgi_application()

myproject/server/home/settings/dev.py

WSGI_APPLICATION = 'home.wsgi.dev.application'

gunicorn.service 配置

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=demouser
Group=www-data
WorkingDirectory=/home/demouser/myproject
ExecStart=/home/demouser/myproject/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/demouser/myproject.sock home.wsgi.dev:application

[Install]
WantedBy=multi-user.target

报错信息

Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2020-08-30 05:04:48 UTC; 14min ago
Process: 13354 ExecStart=/home/demouser/myproject/env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/
Main PID: 13354 (code=exited, status=203/EXEC)
Aug 30 05:04:48 localhost systemd[1]: Started gunicorn daemon.
Aug 30 05:04:48 localhost systemd[13354]: gunicorn.service: Failed to execute command: No such file or directory
Aug 30 05:04:48 localhost systemd[13354]: gunicorn.service: Failed at step EXEC spawning /home/demouser/myproject/env/
Aug 30 05:04:48 localhost systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC
Aug 30 05:04:48 localhost systemd[1]: gunicorn.service: Failed with result 'exit-code'.


问题分析

status=203/EXEC这个错误说明systemd要么找不到指定的Gunicorn可执行文件,要么Python无法定位到你的home模块。结合你的项目结构来看,核心问题是Python模块的路径不对,同时也要验证文件权限和路径的正确性。

解决步骤

1. 先验证Gunicorn路径和权限

登录服务器后,切换到demouser用户,检查Gunicorn文件是否存在且有执行权限:

su - demouser
cd /home/demouser/myproject
# 检查文件是否存在
ls -la env/bin/gunicorn
# 如果存在但没有执行权限,添加权限
chmod +x env/bin/gunicorn

2. 修复Python模块路径问题

你的home模块在server目录下,但Gunicorn的工作目录设置为/home/demouser/myproject,这时候Python找不到home模块(因为server不在Python的搜索路径里)。有两种解决方法:

方法一:修改工作目录到server文件夹

编辑/etc/systemd/system/gunicorn.service,把WorkingDirectory改成:

WorkingDirectory=/home/demouser/myproject/server
方法二:添加Python路径到环境变量

在gunicorn.service的[Service]块中添加一行环境变量配置,把server目录加入Python路径:

Environment="PYTHONPATH=/home/demouser/myproject/server"

3. 重新加载systemd配置并重启服务

修改完配置后,执行以下命令让systemd生效:

sudo systemctl daemon-reload
sudo systemctl restart gunicorn
# 检查服务状态
sudo systemctl status gunicorn

4. 手动测试(可选)

如果还是有问题,可以手动切换到demouser用户,在正确的目录下启动Gunicorn,看是否能正常运行:

su - demouser
cd /home/demouser/myproject/server
env/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/demouser/myproject.sock home.wsgi.dev:application

如果手动启动正常,说明systemd的配置已经修复;如果还是报错,根据手动启动的错误信息进一步排查即可。

内容的提问来源于stack exchange,提问作者Chosen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 23:07:46