【问题标题】:Do we need to use runserver command in production to start our django project?我们是否需要在生产环境中使用 runserver 命令来启动我们的 django 项目?
【发布时间】:2021-10-21 09:21:14
【问题描述】:

我们是否需要在生产环境中使用 runserver 命令来启动我们的项目?如果是,那么如何做,如果不是,那么out project server是如何启动的?

【问题讨论】:

  • the docs 中有一条重要信息 - “请勿在生产环境中使用此服务器”
  • 您需要使用类似gunicorn 的东西来创建 django 工作人员。然后使用像Nginx 这样的服务器作为反向代理来“服务”它们。这是一个教程digitalocean.com/community/tutorials/…
  • 目前我用的是openlitespeed服务器,那我还需要用Nginx吗?

标签: python django web-deployment uwsgi wsgi


【解决方案1】:

目前在生产环境中启动 Django 最常见的方式是使用 UWSGI (https://uwsgi-docs.readthedocs.io/en/latest/)(或异步版本的 ASGI) 你需要这样的配置:

[uwsgi]
socket = 0.0.0.0:3300
chdir = /app/src
pythonpath=/app/src
wsgi-file = /app/src/django_app/wsgi.py
env = DJANGO_SETTINGS_MODULE=django_app.settings
module = django.core.wsgi:get_wsgi_application()
max-requests = 1000
harakiri = 60
buffer-size = 65535
no-orphans = true
touch-reload = /var/run/uwsgi-touch-reload
uid = www-data
gid = www-data
master = 1
workers = 8

然后你开始你的应用程序:

#!/usr/bin/env bash
uwsgi --ini /app/uwsgi.ini

然后你的 HTTP 服务器,例如 nginx,向 uwsgi 应用程序发送请求。 Nginx 配置我是这样的:

upstream django {
    server django:3300;
}

location / {
    include uwsgi_params;
    default_type  text/html;
    uwsgi_pass django;
    uwsgi_param HTTP_X_FORWARDED_PROTOCOL 'https';
}

【讨论】:

  • 我正在使用 openlitespeed 服务器和 wsgi.py 文件,我需要 uwsgi 文件吗?还是wsgi文件就够了?
猜你喜欢
  • 2021-02-22
  • 2019-08-21
  • 2014-05-17
  • 2020-11-19
  • 2019-01-22
  • 2022-11-14
  • 2020-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多