部署Django项目的不同方法及其优缺点?

时间:2021-03-31 20:23:18

I am quite a noob when it comes to deploying a Django project. I'd like to know what are the various methods to deploy Django project and which one is the most preferred.

在部署Django项目时,我是一个非常棒的人。我想知道部署Django项目的各种方法是什么,哪一个是最受欢迎的。

3 个解决方案

#1


4  

Use the Nginx/Apache/mod-wsgi and you can't go wrong.

使用Nginx / Apache / mod-wsgi,你不会出错。

If you prefer a simple alternative, just use Apache.

如果您更喜欢简单的替代方案,只需使用Apache。

There is a very good deployment document: http://lethain.com/entry/2009/feb/13/the-django-and-ubuntu-intrepid-almanac/

有一个非常好的部署文档:http://lethain.com/entry/2009/feb/13/the-django-and-ubuntu-intrepid-almanac/

#2


6  

The Django documentation lists Apache/mod_wsgi, Apache/mod_python and FastCGI etc.

Django文档列出了Apache / mod_wsgi,Apache / mod_python和FastCGI等。

mod_python is deprecated now, one should use mod_wsgi instead.

现在不推荐使用mod_python,而应该使用mod_wsgi。

Django with mod_wsgi is easy to setup, but:

使用mod_wsgi的Django很容易设置,但是:

  • you can only use one python version at a time [edit: you even can only use the python version mod_wsgi was compiled for]
  • 你一次只能使用一个python版本[编辑:你甚至只能使用编译的mod_wsgi的python版本]
  • [edit: seems if I'm wrong on mod_wsgi not supporting virtualenv: it does]
  • [编辑:似乎我错了mod_wsgi不支持virtualenv:它确实]

So for multiple sites (targeting different django/python versions) on a server mod_wsgi is not the best solution.

因此,对于服务器上的多个站点(针对不同的django / python版本),mod_wsgi不是最佳解决方案。

FastCGI can be used with virtualenv, also with different python versions, as you run it with

FastCGI可以与virtualenv一起使用,也可以与不同的python版本一起使用

./manage.py runfcgi …

and then configure your webserver to use this fcgi interface.

然后配置您的Web服务器以使用此fcgi接口。

The new, hot stuff about django deployment seems to be gunicorn. It's a webserver that implements wsgi and is typically used as backend with a "big" webserver as proxy.

关于django部署的新热点似乎是枪手。它是一个实现wsgi的网络服务器,通常用作后端,“大”网络服务器作为代理。

Deployment with gunicorn feels a lot like fcgi: you run a process doing the django processing stuff with manage.py, and a webserver as frontend to the world.

使用gunicorn进行部署感觉很像fcgi:你运行一个使用manage.py执行django处理的进程,以及作为世界前端的Web服务器。

But gunicorn deployment has some advantages over fcgi:

但枪支部署比fcgi有一些优势:

  • speed - I didn't find the sources, but benchmarks say fcgi is not as fast as the f suggests
  • 速度 - 我没有找到消息来源,但基准测试表明fcgi并不像f建议的那么快
  • config files, for fcgi you must do all configuration on the commandline when executing the manage.py command. This comes unhandy when running multiple django instances via an init.d (unix-like OS' system service startup). It's always the same cmdline, with just different configuration files
  • 配置文件,对于fcgi,您必须在执行manage.py命令时在命令行上执行所有配置。当通过init.d(类似Unix的系统服务启动)运行多个django实例时,这是不方便的。它总是相同的cmdline,只有不同的配置文件
  • gunicorn can drop privileges: no need to do this in your init.d script, and it's easy to switch to one user per django instance
  • gunicorn可以删除权限:无需在init.d脚本中执行此操作,并且每个django实例可以轻松切换到一个用户
  • gunicorn behaves more like a daemon: writing pidfile and logfile, forking to the background etc. makes again using it in an init.d script easier.
  • gunicorn更像一个守护进程:编写pidfile和logfile,分叉到后台等,再次使用它在init.d脚本中更容易。

Thus, I would suggest to use the gunicorn solution, unless you have a single site on a single server with low traffic, than you could use the wsgi solution. But I think in the long run you're more happy with gunicorn.

因此,我建议使用gunicorn解决方案,除非您在单个服务器上拥有低流量的单个站点,否则您可以使用wsgi解决方案。但我认为从长远来看,你对枪炮更加满意。

If you have a django only webserver, I would suggest to use nginx as frontendproxy, as it's the best performing (again this is based on benchmarks I read in some blogposts - don't have the url anymore). Personally I use apache as frontendproxy, as I need it for other sites hosted on the server.

如果你有一个只有django的网络服务器,我建议使用nginx作为frontendproxy,因为它是最好的表现(再次这是基于我在一些博文中读到的基准 - 再也没有网址)。我个人使用apache作为frontendproxy,因为我需要它在服务器上托管的其他站点。

A simple setup instruction for django deployment could be found here: http://ericholscher.com/blog/2010/aug/16/lessons-learned-dash-easy-django-deployment/

可以在此处找到有关django部署的简单设置说明:http://ericholscher.com/blog/2010/aug/16/lessons-learned-dash-easy-django-deployment/

My init.d script for gunicorn is located at github: https://gist.github.com/753053

我的gun.dorn的init.d脚本位于github:https://gist.github.com/753053

Unfortunately I did not yet blog about it, but an experienced sysadmin should be able to do the required setup.

不幸的是我还没有博客,但经验丰富的系统管理员应该能够进行必要的设置。

#3


1  

I myself have faced a lot of problems in deploying Django Projects and automating the deployment process. Apache and mod_wsgi were like curse for Django Deployment. There are several tools like Nginx, Gunicorn, SupervisorD and Fabric which are trending for Django deployment. At first I used/configured them individually without Deployment automation which took a lot of time(I had to maintain testing as well as production servers for my client and had to update them as soon as a new feature was tested and approved.) but then I stumbled upon django-fagungis, which totally automates my Django Deployment from cloning my project from bitbucket to deploying on my remote server (it uses Nginx, Gunicorn, SupervisorD, Fabtic and virtualenv and also installs all the dependencies on the fly), all with just three commands :) You can find more about it in my blog post here. Now I even don't have to get involved in this process(which used to take a lot of my time) and one of my junior developers runs those three commands of django-fagungis mentioned here on his local machine and we get a crisp new copy of our project deployed in minutes without any hassle:)

我自己在部署Django项目和自动化部署过程中遇到了很多问题。 Apache和mod_wsgi就像是Django Deployment的诅咒。有许多工具,如Nginx,Gunicorn,SupervisorD和Fabric,它们都是Django部署的趋势。起初我单独使用/配置它们而没有部署自动化,这需要花费大量时间(我必须为我的客户端维护测试以及生产服务器,并且必须在测试和批准新功能后立即更新它们。)但是我偶然发现django-fagungis,它完全自动化我的Django部署,将我的项目从bitbucket克隆到我的远程服务器上部署(它使用Nginx,Gunicorn,SupervisorD,Fabtic和virtualenv,并且还可以安装所有依赖项),所有这些都是只有三个命令:)您可以在我的博客文章中找到更多相关信息。现在我甚至不必参与这个过程(过去需要花费我很多时间),我的一个初级开发人员在他的本地机器上运行这里提到的django-fagungis的这三个命令,我们得到一个清晰的新我们的项目副本在几分钟内部署,没有任何麻烦:)

#1


4  

Use the Nginx/Apache/mod-wsgi and you can't go wrong.

使用Nginx / Apache / mod-wsgi,你不会出错。

If you prefer a simple alternative, just use Apache.

如果您更喜欢简单的替代方案,只需使用Apache。

There is a very good deployment document: http://lethain.com/entry/2009/feb/13/the-django-and-ubuntu-intrepid-almanac/

有一个非常好的部署文档:http://lethain.com/entry/2009/feb/13/the-django-and-ubuntu-intrepid-almanac/

#2


6  

The Django documentation lists Apache/mod_wsgi, Apache/mod_python and FastCGI etc.

Django文档列出了Apache / mod_wsgi,Apache / mod_python和FastCGI等。

mod_python is deprecated now, one should use mod_wsgi instead.

现在不推荐使用mod_python,而应该使用mod_wsgi。

Django with mod_wsgi is easy to setup, but:

使用mod_wsgi的Django很容易设置,但是:

  • you can only use one python version at a time [edit: you even can only use the python version mod_wsgi was compiled for]
  • 你一次只能使用一个python版本[编辑:你甚至只能使用编译的mod_wsgi的python版本]
  • [edit: seems if I'm wrong on mod_wsgi not supporting virtualenv: it does]
  • [编辑:似乎我错了mod_wsgi不支持virtualenv:它确实]

So for multiple sites (targeting different django/python versions) on a server mod_wsgi is not the best solution.

因此,对于服务器上的多个站点(针对不同的django / python版本),mod_wsgi不是最佳解决方案。

FastCGI can be used with virtualenv, also with different python versions, as you run it with

FastCGI可以与virtualenv一起使用,也可以与不同的python版本一起使用

./manage.py runfcgi …

and then configure your webserver to use this fcgi interface.

然后配置您的Web服务器以使用此fcgi接口。

The new, hot stuff about django deployment seems to be gunicorn. It's a webserver that implements wsgi and is typically used as backend with a "big" webserver as proxy.

关于django部署的新热点似乎是枪手。它是一个实现wsgi的网络服务器,通常用作后端,“大”网络服务器作为代理。

Deployment with gunicorn feels a lot like fcgi: you run a process doing the django processing stuff with manage.py, and a webserver as frontend to the world.

使用gunicorn进行部署感觉很像fcgi:你运行一个使用manage.py执行django处理的进程,以及作为世界前端的Web服务器。

But gunicorn deployment has some advantages over fcgi:

但枪支部署比fcgi有一些优势:

  • speed - I didn't find the sources, but benchmarks say fcgi is not as fast as the f suggests
  • 速度 - 我没有找到消息来源,但基准测试表明fcgi并不像f建议的那么快
  • config files, for fcgi you must do all configuration on the commandline when executing the manage.py command. This comes unhandy when running multiple django instances via an init.d (unix-like OS' system service startup). It's always the same cmdline, with just different configuration files
  • 配置文件,对于fcgi,您必须在执行manage.py命令时在命令行上执行所有配置。当通过init.d(类似Unix的系统服务启动)运行多个django实例时,这是不方便的。它总是相同的cmdline,只有不同的配置文件
  • gunicorn can drop privileges: no need to do this in your init.d script, and it's easy to switch to one user per django instance
  • gunicorn可以删除权限:无需在init.d脚本中执行此操作,并且每个django实例可以轻松切换到一个用户
  • gunicorn behaves more like a daemon: writing pidfile and logfile, forking to the background etc. makes again using it in an init.d script easier.
  • gunicorn更像一个守护进程:编写pidfile和logfile,分叉到后台等,再次使用它在init.d脚本中更容易。

Thus, I would suggest to use the gunicorn solution, unless you have a single site on a single server with low traffic, than you could use the wsgi solution. But I think in the long run you're more happy with gunicorn.

因此,我建议使用gunicorn解决方案,除非您在单个服务器上拥有低流量的单个站点,否则您可以使用wsgi解决方案。但我认为从长远来看,你对枪炮更加满意。

If you have a django only webserver, I would suggest to use nginx as frontendproxy, as it's the best performing (again this is based on benchmarks I read in some blogposts - don't have the url anymore). Personally I use apache as frontendproxy, as I need it for other sites hosted on the server.

如果你有一个只有django的网络服务器,我建议使用nginx作为frontendproxy,因为它是最好的表现(再次这是基于我在一些博文中读到的基准 - 再也没有网址)。我个人使用apache作为frontendproxy,因为我需要它在服务器上托管的其他站点。

A simple setup instruction for django deployment could be found here: http://ericholscher.com/blog/2010/aug/16/lessons-learned-dash-easy-django-deployment/

可以在此处找到有关django部署的简单设置说明:http://ericholscher.com/blog/2010/aug/16/lessons-learned-dash-easy-django-deployment/

My init.d script for gunicorn is located at github: https://gist.github.com/753053

我的gun.dorn的init.d脚本位于github:https://gist.github.com/753053

Unfortunately I did not yet blog about it, but an experienced sysadmin should be able to do the required setup.

不幸的是我还没有博客,但经验丰富的系统管理员应该能够进行必要的设置。

#3


1  

I myself have faced a lot of problems in deploying Django Projects and automating the deployment process. Apache and mod_wsgi were like curse for Django Deployment. There are several tools like Nginx, Gunicorn, SupervisorD and Fabric which are trending for Django deployment. At first I used/configured them individually without Deployment automation which took a lot of time(I had to maintain testing as well as production servers for my client and had to update them as soon as a new feature was tested and approved.) but then I stumbled upon django-fagungis, which totally automates my Django Deployment from cloning my project from bitbucket to deploying on my remote server (it uses Nginx, Gunicorn, SupervisorD, Fabtic and virtualenv and also installs all the dependencies on the fly), all with just three commands :) You can find more about it in my blog post here. Now I even don't have to get involved in this process(which used to take a lot of my time) and one of my junior developers runs those three commands of django-fagungis mentioned here on his local machine and we get a crisp new copy of our project deployed in minutes without any hassle:)

我自己在部署Django项目和自动化部署过程中遇到了很多问题。 Apache和mod_wsgi就像是Django Deployment的诅咒。有许多工具,如Nginx,Gunicorn,SupervisorD和Fabric,它们都是Django部署的趋势。起初我单独使用/配置它们而没有部署自动化,这需要花费大量时间(我必须为我的客户端维护测试以及生产服务器,并且必须在测试和批准新功能后立即更新它们。)但是我偶然发现django-fagungis,它完全自动化我的Django部署,将我的项目从bitbucket克隆到我的远程服务器上部署(它使用Nginx,Gunicorn,SupervisorD,Fabtic和virtualenv,并且还可以安装所有依赖项),所有这些都是只有三个命令:)您可以在我的博客文章中找到更多相关信息。现在我甚至不必参与这个过程(过去需要花费我很多时间),我的一个初级开发人员在他的本地机器上运行这里提到的django-fagungis的这三个命令,我们得到一个清晰的新我们的项目副本在几分钟内部署,没有任何麻烦:)