Django部署网站
Django deploying website
我用django做了一个网站。我想将此应用程序部署到生产环境。
现在我对几件事感到困惑:在开发时我可以 运行 我的应用程序使用命令:python manage.py runserver IP_OF_SERVER:PORT
。
现在通过这种方法我可以做任何事情。我的工具(网站)只会在本地使用。
我只用这个命令部署我的网站可以吗?是否有必要做 django 生产过程,如果有必要怎么做?我是 django 的新手。请帮助我理解。
如果您想在 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)
通常,这些东西以 three tier 方式部署。
这里一般的做法是这样的
[Database] <-(db calls)-> [Django app] <- wsgi -> [gunicorn] <- http -> [nginx] <- http -> public
您的应用程序是这里的 "Django app" 块。您可以 运行 它与 manage.py runserver
之类的东西,但这是一个非常轻量级的玩具服务器,无法真正处理高流量。如果您有一个需要 1 毫秒处理的请求,并且有 100 个用户尝试发出相同的请求,那么最后一个客户端将必须等待 100 毫秒才能得到响应。只需 运行 应用程序的更多实例即可轻松解决此问题,但开发服务器无法做到这一点。
所谓的 "app server" 之类的 gunicorn 将允许您 运行 为您的应用程序提供一个更强大的网络服务器,它可以产生多个工作人员并处理一些恶作剧的流量模式。
现在,即使是 gunicorn 也可以被高性能服务器打败,尤其是在服务静态资产如图像、css、js 等方面。这就像 nginx。因此,我们设置了让 nginx 面向世界并直接为所有静态资产提供服务的东西。对实际应用程序的请求将被代理到 gunicorn,并由您的实际应用程序提供服务。
这并不像听起来那么复杂,您应该可以在一天左右的时间内 运行ning 得到一些东西。我提到的所有技术都有具有不同特性的替代品。 This 是一个很好的教程,介绍了如何让事情顺利进行以及在部署过程中需要注意什么。
我用django做了一个网站。我想将此应用程序部署到生产环境。
现在我对几件事感到困惑:在开发时我可以 运行 我的应用程序使用命令:python manage.py runserver IP_OF_SERVER:PORT
。
现在通过这种方法我可以做任何事情。我的工具(网站)只会在本地使用。
我只用这个命令部署我的网站可以吗?是否有必要做 django 生产过程,如果有必要怎么做?我是 django 的新手。请帮助我理解。
如果您想在 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)
通常,这些东西以 three tier 方式部署。
这里一般的做法是这样的
[Database] <-(db calls)-> [Django app] <- wsgi -> [gunicorn] <- http -> [nginx] <- http -> public
您的应用程序是这里的 "Django app" 块。您可以 运行 它与 manage.py runserver
之类的东西,但这是一个非常轻量级的玩具服务器,无法真正处理高流量。如果您有一个需要 1 毫秒处理的请求,并且有 100 个用户尝试发出相同的请求,那么最后一个客户端将必须等待 100 毫秒才能得到响应。只需 运行 应用程序的更多实例即可轻松解决此问题,但开发服务器无法做到这一点。
所谓的 "app server" 之类的 gunicorn 将允许您 运行 为您的应用程序提供一个更强大的网络服务器,它可以产生多个工作人员并处理一些恶作剧的流量模式。
现在,即使是 gunicorn 也可以被高性能服务器打败,尤其是在服务静态资产如图像、css、js 等方面。这就像 nginx。因此,我们设置了让 nginx 面向世界并直接为所有静态资产提供服务的东西。对实际应用程序的请求将被代理到 gunicorn,并由您的实际应用程序提供服务。
这并不像听起来那么复杂,您应该可以在一天左右的时间内 运行ning 得到一些东西。我提到的所有技术都有具有不同特性的替代品。 This 是一个很好的教程,介绍了如何让事情顺利进行以及在部署过程中需要注意什么。