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

DigitalOcean上Django+Gunicorn部署报错求助

Django + Gunicorn + Nginx部署问题(DigitalOcean Ubuntu环境)

问题1:手动启动Gunicorn触发ModuleNotFoundError

执行命令 gunicorn --bind 0.0.0.0:8000 wsgi:app 时,Worker进程启动失败,报错ModuleNotFoundError: No module named 'wsgi',完整错误日志如下:

[2023-06-13 11:58:41 +0000] [68713] [INFO] Starting gunicorn 20.1.0
[2023-06-13 11:58:41 +0000] [68713] [INFO] Listening at: http://0.0.0.0:8000 (68713)
[2023-06-13 11:58:41 +0000] [68713] [INFO] Using worker: sync
[2023-06-13 11:58:41 +0000] [68714] [INFO] Booting worker with pid: 68714
[2023-06-13 11:58:41 +0000] [68714] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/gunicorn/arbiter.py", line 589, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.10/dist-packages/gunicorn/workers/base.py", line 134, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.10/dist-packages/gunicorn/workers/base.py", line 146, in load_wsgiapp
    self.wsgi = self.app.wsgi()
  File "/usr/local/lib/python3.10/dist-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/local/lib/python3.10/dist-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
  File "/usr/local/lib/python3.10/dist-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/local/lib/python3.10/dist-packages/gunicorn/util.py", line 359, in import_app
    mod = importlib.import_module(module)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'wsgi'
[2023-06-13 11:58:41 +0000] [68714] [INFO] Worker exiting (pid: 68714)
[2023-06-13 11:58:41 +0000] [68713] [INFO] Shutting down: Master
[2023-06-13 11:58:41 +0000] [68713] [INFO] Reason: Worker failed to boot.

问题2:gunicorn.service启动失败(找不到可执行文件)

启动gunicorn.service时提示无法找到指定路径的可执行文件,服务状态为失败,相关日志:

root@SnoffleSys:/home/mridul/djangoprojectdir# sudo systemctl status gunicorn
× gunicorn.service - gunicorn daemon
     Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; preset: enabled)
     Active: failed (Result: exit-code) since Tue 2023-06-13 09:53:07 UTC; 2h 9min ago
   Duration: 4ms
TriggeredBy: ● gunicorn.socket
   Main PID: 66337 (code=exited, status=203/EXEC)
        CPU: 2ms

Jun 13 09:53:07 SnoffleSys systemd[1]: Started gunicorn daemon.
Jun 13 09:53:07 SnoffleSys systemd[66337]: gunicorn.service: Failed to locate executable /home/mridul/djangoprojectdir/djangoprojectenv/bin>
Jun 13 09:53:07 SnoffleSys systemd[66337]: gunicorn.service: Failed at step EXEC spawning /home/mridul/djangoprojectdir/djangoprojectenv/bi>
Jun 13 09:53:07 SnoffleSys systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC
Jun 13 09:53:07 SnoffleSys systemd[1]: gunicorn.service: Failed with result 'exit-code'.

当前使用的gunicorn.service配置文件内容:

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

[Service]
User=mridul
Group=www-data
WorkingDirectory=/home/mridul/djangoprojectdir
ExecStart=/home/mridul/djangoprojectdir/djangoprojectenv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          Snoffle_tech.wsgi:application

[Install]
WantedBy=multi-user.target

问题3:gunicorn.socket状态异常

gunicorn.socket处于监听状态而非运行状态,触发的gunicorn.service显示红色异常,相关命令执行结果:

root@SnoffleSys:/home/mridul/djangoprojectdir# sudo systemctl start gunicorn.socket
root@SnoffleSys:/home/mridul/djangoprojectdir# sudo systemctl enable gunicorn.socket
root@SnoffleSys:/home/mridul/djangoprojectdir# sudo systemctl status gunicorn.socket
● gunicorn.socket - gunicorn socket
     Loaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; preset: enabled)
     Active: active (listening) since Tue 2023-06-13 08:27:09 UTC; 3h 26min ago (and it should be running not listening)
      Until: Tue 2023-06-13 08:27:09 UTC; 3h 26min ago
   Triggers: ● gunicorn.service (and the dot is red it is supposed to be be green)
     Listen: /run/gunicorn.sock (Stream)
     CGroup: /system.slice/gunicorn.socket

Jun 13 08:27:09 SnoffleSys systemd[1]: Listening on gunicorn socket.

另外,执行sudo journalctl -u gunicorn.socket的日志:

Jun 13 08:23:31 SnoffleSys systemd[1]: Listening on gunicorn socket.
Jun 13 08:26:56 SnoffleSys systemd[1]: gunicorn.socket: Failed with result 'service-start-limit-hit'.
Jun 13 08:27:09 SnoffleSys systemd[1]: Listening on gunicorn socket.

参考资料

参考DigitalOcean官方教程:《如何在Ubuntu 20.04上配置Django与PostgreSQL、Nginx和Gunicorn》


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 19:54:53