Django部署网站

时间:2017-06-27 06:39:35

标签: python django production

我用django创建了一个网站。我想将此应用程序部署到生产中。

现在我对几件事情感到困惑:在开发时我可以使用命令运行我的应用程序:python manage.py runserver IP_OF_SERVER:PORT。 现在通过这种方法,我可以做任何事情。我的工具(网站)只能在本地使用。 我只使用此命令部署我的网站是否可以?有必要做django生产过程,如果有必要怎么做?我是django的新手。请帮我理解。

2 个答案:

答案 0 :(得分:4)

通常,这些事情以three tier方式部署。

这里,一般方法就是这样

[Database] <-(db calls)-> [Django app] <- wsgi -> [gunicorn] <- http -> [nginx] <- http -> public

你的应用程序是这里的“Django app”块。您可以使用类似manage.py runserver的东西运行它,但这是一个非常轻量级的玩具服务器,无法真正处理高流量。如果您的请求需要1毫秒来处理,而100个用户尝试发出相同的请求,那么最后一个客户端必须等待100毫秒才能获得响应。只需运行应用程序的更多实例即可轻松解决此问题,但开发服务器无法做到这一点。

像gunicorn这样的所谓“应用服务器”将允许您为您的应用程序运行更强大的Web服务器,该服务器可以产生多个工作人员并处理某些恶作剧流量模式。

现在,甚至可以通过高性能服务器击败gunicorn,尤其是服务于图像,css,js等静态资产。这就像nginx。因此,我们设置了让nginx面向世界并直接服务所有静态资产的东西。并且对实际应用程序的请求将被代理到gunicorn,并且将由您的实际应用程序提供。

这并不像听起来那么复杂,你应该能够在一天左右的时间内运行。我提到的所有技术都有不同特征的替代品。 This是一个很好的教程,介绍如何让事情进展以及在部署期间需要注意什么。

答案 1 :(得分:1)

如果您想在Windows服务器中通过IIS设置Django生产服务器(如果用户较少,您甚至可以使用普通的Windows 7或10专业机器)。此视频可以帮助您逐步完成此操作

https://www.youtube.com/watch?v=kXbfHtAvubc

我以这种方式培养了几个制作网站。

虽然您尝试做的那个也有效,但您的方法唯一的问题是您应该注意控制台永远不会被任何人或您意外关闭。但是有很多隐藏的问题,通常是生产,这是不推荐的。

为避免意外关闭,您可以在Windows中进行以下操作(将其作为服务运行):

对于这种方法,您需要安装pywin32,从这里安装它:https://sourceforge.net/projects/pywin32/files/pywin32/Build%20217/

import win32service
import win32serviceutil
import win32event
import subprocess
import os

class PySvc(win32serviceutil.ServiceFramework):

    # you can NET START/STOP the service by the following name
    _svc_name_ = "Name your Service here"
    # this text shows up as the service name in the Service
    # Control Manager (SCM)
    _svc_display_name_ = "External Display Name of your service"
    # this text shows up as the description in the SCM
    _svc_description_ = "Description what this service does"

    def __init__(self, args):
        import platform
        win32serviceutil.ServiceFramework.__init__(self, args)
        # create an event to listen for stop requests on
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    # core logic of the service
    def SvcDoRun(self):
        os.chdir('your site root directory')
        subprocess.Popen('python manage.py runserver IP:PORT')
        # if you need stdout and stderr, open file handlers and keep redirecting to those files with subprocess `stdout` and `stderr`

    # called when we're being shut down
    def SvcStop(self):
        # tell the SCM we're shutting down
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

        # fire the stop event
        win32event.SetEvent(self.hWaitStop)


if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(PySvc)
相关问题