如何使用 Django 和 Gunicorn

Gunicorn(“绿色独角兽”)是一个纯 Python 的 UNIX WSGI 服务器。它没有依赖项,可以使用 pip 安装。

安装 Gunicorn

运行 python -m pip install gunicorn 来安装 gunicorn。更多详情,请参见 gunicorn 文档

将 Django 作为通用的 WSGI 应用程序在 Gunicorn 中运行

安装 Gunicorn 后,就会提供一个 gunicorn 命令,该命令启动 Gunicorn 服务器进程。Gunicorn 最简单的调用方法是传递包含名为 application 的 WSGI 应用程序对象的模块位置,对于典型的 Django 项目,它看起来像这样

gunicorn myproject.wsgi

这将启动一个进程,运行一个线程,监听 127.0.0.1:8000。这要求您的项目位于 Python 路径上;确保这一点最简单的方法是从与您的 manage.py 文件相同的目录运行此命令。

有关其他技巧,请参阅 Gunicorn 的 部署文档

返回顶部