如何为apache和django设置mod_wsgi?

时间:2021-12-23 23:05:26

I know that there are already a lot of information on this topic, but they are quite clumsy, not so simple and expressive. Could anyone explain me how to use django and with mod_wsgi and apache?

我知道关于这个话题已经有很多信息,但它们非常笨拙,不那么简单和富有表现力。任何人都可以解释我如何使用django和mod_wsgi和apache?

3 个解决方案

#1


9  

mod_wsgi isn't particularly the best fit for running Python WSGI applications, or, if you'd rather, there are more pythonic ways you can run your Django instance.

mod_wsgi不是特别适合运行Python WSGI应用程序,或者,如果你更愿意,有更多pythonic方法可以运行你的Django实例。

First off, I'd reason that it takes a lot of effort to understand Apache's request processing model and to configure it correctly, especially with respect to mod_wsgi. If your not exactly set or locked into using Apache, I would recommend that you take a look at running Spawning or Green Unicorn, behind a nginx proxy like @Neo suggested.

首先,我认为理解Apache的请求处理模型并正确配置它需要花费很多精力,特别是对于mod_wsgi。如果你没有完全设置或锁定使用Apache,我建议你看一下像@Neo建议的nginx代理后面运行Spawning或Green Unicorn。

Spawning and gunicorn are both ridiculously fast, don't require that you compile Apache with a specific Python interpreter and provide support for progressively updating your code base on the fly, hooks for Django and other goodies out of the box. nginx, Spawning and gunicorn all have a simple processing model, are kept completely independent of each other, so you get a more transparent architecture that is easier to maintain and monitor.

Spawning和gunicorn都非常快,不要求你使用特定的Python解释器编译Apache,并提供支持,以便动态地逐步更新代码库,为Django和其他开箱即用的钩子提供支持。 nginx,Spawning和gunicorn都有一个简单的处理模型,彼此完全独立,因此您可以获得更加透明的架构,更易于维护和监控。

Here's a great guide on setting up Spawning with Django by Eric Florenzano, and here's a thorough presentation on running Django with gunicorn by the project's author, Benoît Chesneau.

以下是Eric Florenzano用Django设置Spawning的一个很好的指南,这里有一个关于用该项目的作者BenoîtChesneau用gunicorn运行Django的详尽介绍。

Whichever you choose, you'll be feeling right @home.

无论你选择哪种,你都会对@home感觉正确。

#2


5  

I recently setup my application on Django, and this guide was all that I needed. http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

我最近在Django上设置了我的应用程序,这本指南就是我所需要的。 http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

So basically, the process is

所以基本上,这个过程是

  1. Setup another server to serve static files (Ex. Nginx) on Port 80.
  2. 设置另一台服务器以在端口80上提供静态文件(例如Nginx)。

  3. Setup apache on some other port.
  4. 在其他端口上设置apache。

  5. Run django application on apache using mod_wsgi
  6. 使用mod_wsgi在apache上运行django应用程序

  7. Reverseproxy all non-static/non-media files to apache+mod_wsgi/django
  8. 将所有非静态/非媒体文件重新调整为apache + mod_wsgi / django

Let me know on which step you are stuck.

让我知道你被困在哪一步。

#3


3  

Here's how I do it on my Mac, with Apache, Python and Django from Mac Ports. This is not necessarily the best approach but it works for me.

以下是我在Mac上使用来自Mac Ports的Apache,Python和Django的方法。这不一定是最好的方法,但它适用于我。

I have the following top-level directories:

我有以下*目录:

  • lib: python code, with settings.py in lib/settings.py
  • lib:python代码,lib / settings.py中的settings.py

  • static: stuff to be served by Apache, e.g. media and CSS
  • static:Apache提供的东西,例如媒体和CSS

  • tools: development tools, e.g. rollout scripts.
  • 工具:开发工具,例如推出脚本。

So here's the Apache config for an example site, then see Django WSGI script below:

所以这里是一个示例站点的Apache配置,然后看下面的Django WSGI脚本:

<VirtualHost *:80>
    # Stuff to served statically is in media directory
    DocumentRoot /Library/WebServer/mysite/static

    ServerName mysite.local

    # Redirect to homepage action
    RewriteEngine on
    RewriteRule ^/$ /mysite/ [R,L]

    # Static dirs first
    Alias /static/ /Library/WebServer/mysite/static/

    <Directory "/Library/WebServer/mysite/static/">
        Order allow,deny
        Allow from all
    </Directory>    

    # Now everything else goes to Django    
    WSGIDaemonProcess mysite-django.local processes=1 threads=5 maximum-requests=0 display-name=%{GROUP} python-path=/Library/WebServer/mysite/lib python-eggs=/tmp
    WSGIProcessGroup mysite-django.local
    WSGIScriptAlias / /Library/WebServer/mysite/lib/apache/django_wsgi.py

    <Directory "/Library/WebServer/mysite/lib/apache">
        Order allow,deny
        Allow from all
    </Directory>    

</VirtualHost>

The Django WGCI script is in lib/apache/django_wsgi.py:

Django WGCI脚本位于lib / apache / django_wsgi.py中:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

#1


9  

mod_wsgi isn't particularly the best fit for running Python WSGI applications, or, if you'd rather, there are more pythonic ways you can run your Django instance.

mod_wsgi不是特别适合运行Python WSGI应用程序,或者,如果你更愿意,有更多pythonic方法可以运行你的Django实例。

First off, I'd reason that it takes a lot of effort to understand Apache's request processing model and to configure it correctly, especially with respect to mod_wsgi. If your not exactly set or locked into using Apache, I would recommend that you take a look at running Spawning or Green Unicorn, behind a nginx proxy like @Neo suggested.

首先,我认为理解Apache的请求处理模型并正确配置它需要花费很多精力,特别是对于mod_wsgi。如果你没有完全设置或锁定使用Apache,我建议你看一下像@Neo建议的nginx代理后面运行Spawning或Green Unicorn。

Spawning and gunicorn are both ridiculously fast, don't require that you compile Apache with a specific Python interpreter and provide support for progressively updating your code base on the fly, hooks for Django and other goodies out of the box. nginx, Spawning and gunicorn all have a simple processing model, are kept completely independent of each other, so you get a more transparent architecture that is easier to maintain and monitor.

Spawning和gunicorn都非常快,不要求你使用特定的Python解释器编译Apache,并提供支持,以便动态地逐步更新代码库,为Django和其他开箱即用的钩子提供支持。 nginx,Spawning和gunicorn都有一个简单的处理模型,彼此完全独立,因此您可以获得更加透明的架构,更易于维护和监控。

Here's a great guide on setting up Spawning with Django by Eric Florenzano, and here's a thorough presentation on running Django with gunicorn by the project's author, Benoît Chesneau.

以下是Eric Florenzano用Django设置Spawning的一个很好的指南,这里有一个关于用该项目的作者BenoîtChesneau用gunicorn运行Django的详尽介绍。

Whichever you choose, you'll be feeling right @home.

无论你选择哪种,你都会对@home感觉正确。

#2


5  

I recently setup my application on Django, and this guide was all that I needed. http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

我最近在Django上设置了我的应用程序,这本指南就是我所需要的。 http://blog.stannard.net.au/2010/12/11/installing-django-with-apache-and-mod_wsgi-on-ubuntu-10-04/

So basically, the process is

所以基本上,这个过程是

  1. Setup another server to serve static files (Ex. Nginx) on Port 80.
  2. 设置另一台服务器以在端口80上提供静态文件(例如Nginx)。

  3. Setup apache on some other port.
  4. 在其他端口上设置apache。

  5. Run django application on apache using mod_wsgi
  6. 使用mod_wsgi在apache上运行django应用程序

  7. Reverseproxy all non-static/non-media files to apache+mod_wsgi/django
  8. 将所有非静态/非媒体文件重新调整为apache + mod_wsgi / django

Let me know on which step you are stuck.

让我知道你被困在哪一步。

#3


3  

Here's how I do it on my Mac, with Apache, Python and Django from Mac Ports. This is not necessarily the best approach but it works for me.

以下是我在Mac上使用来自Mac Ports的Apache,Python和Django的方法。这不一定是最好的方法,但它适用于我。

I have the following top-level directories:

我有以下*目录:

  • lib: python code, with settings.py in lib/settings.py
  • lib:python代码,lib / settings.py中的settings.py

  • static: stuff to be served by Apache, e.g. media and CSS
  • static:Apache提供的东西,例如媒体和CSS

  • tools: development tools, e.g. rollout scripts.
  • 工具:开发工具,例如推出脚本。

So here's the Apache config for an example site, then see Django WSGI script below:

所以这里是一个示例站点的Apache配置,然后看下面的Django WSGI脚本:

<VirtualHost *:80>
    # Stuff to served statically is in media directory
    DocumentRoot /Library/WebServer/mysite/static

    ServerName mysite.local

    # Redirect to homepage action
    RewriteEngine on
    RewriteRule ^/$ /mysite/ [R,L]

    # Static dirs first
    Alias /static/ /Library/WebServer/mysite/static/

    <Directory "/Library/WebServer/mysite/static/">
        Order allow,deny
        Allow from all
    </Directory>    

    # Now everything else goes to Django    
    WSGIDaemonProcess mysite-django.local processes=1 threads=5 maximum-requests=0 display-name=%{GROUP} python-path=/Library/WebServer/mysite/lib python-eggs=/tmp
    WSGIProcessGroup mysite-django.local
    WSGIScriptAlias / /Library/WebServer/mysite/lib/apache/django_wsgi.py

    <Directory "/Library/WebServer/mysite/lib/apache">
        Order allow,deny
        Allow from all
    </Directory>    

</VirtualHost>

The Django WGCI script is in lib/apache/django_wsgi.py:

Django WGCI脚本位于lib / apache / django_wsgi.py中:

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()