How to set mod_wsgi for apache and django?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 18:39:09

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.

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.

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.

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.

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

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/

So basically, the process is

  1. Setup another server to serve static files (Ex. Nginx) on Port 80.
  2. Setup apache on some other port.
  3. Run django application on apache using mod_wsgi
  4. Reverseproxy all non-static/non-media files to apache+mod_wsgi/django

Let me know on which step you are stuck.

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.

I have the following top-level directories:

  • lib: python code, with settings.py in lib/settings.py
  • static: stuff to be served by Apache, e.g. media and CSS
  • tools: development tools, e.g. rollout scripts.

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

<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:

import os
import sys

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

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!