如何在生产服务器上自动 运行 我的 Django 应用程序

How to run my Django application automatically on production server

我使用 Python 和 Django 在本地计算机上构建了一个应用程序。它工作正常。现在,我想自动化在生产服务器上启动我的 Django 应用程序的过程。

我需要做什么才能实现?

Django 文档不建议在生产环境中使用 Django 内置服务器。

DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)

建议使用 gunicorn or uWSGI 启动您的 WSGI 应用程序:
- How to use Django with gunicorn
- How to use Django with uWSGI

另一个好的做法是,使用supervisord启动你的进程,然后如果它死掉或由于某种原因被杀死,supervisord会重新启动这个进程

最后,使用nginx or apache作为代理服务器,这是可以处理费用的强大服务器。大多数教程或文档,推荐使用 nginx,因为它是一个高性能的 HTTP 服务器。

NGINX is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. NGINX is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

检查此以了解如何配置它们:
- Deploying Gunicorn with nginx
- Deploying uWSGI with nginx

另外,在部署 Django 应用程序之前,请阅读此清单,以确保一切都已正确配置:Django - Deployment checklist

使用此架构和工具,您将能够使用简单的 supervisord 命令启动 gunicorn 进程。就这样。然后 gunicorn 将启动您的 WSGI 应用程序。