parts/django/docs/howto/deployment/modwsgi.txt
changeset 69 c6bca38c1cbf
equal deleted inserted replaced
68:5ff1fc726848 69:c6bca38c1cbf
       
     1 ==========================================
       
     2 How to use Django with Apache and mod_wsgi
       
     3 ==========================================
       
     4 
       
     5 Deploying Django with Apache_ and `mod_wsgi`_ is the recommended way to get
       
     6 Django into production.
       
     7 
       
     8 .. _Apache: http://httpd.apache.org/
       
     9 .. _mod_wsgi: http://code.google.com/p/modwsgi/
       
    10 
       
    11 mod_wsgi is an Apache module which can be used to host any Python application
       
    12 which supports the `Python WSGI interface`_, including Django. Django will work
       
    13 with any version of Apache which supports mod_wsgi.
       
    14 
       
    15 .. _python wsgi interface: http://www.python.org/dev/peps/pep-0333/
       
    16 
       
    17 The `official mod_wsgi documentation`_ is fantastic; it's your source for all
       
    18 the details about how to use mod_wsgi. You'll probably want to start with the
       
    19 `installation and configuration documentation`_.
       
    20 
       
    21 .. _official mod_wsgi documentation: http://code.google.com/p/modwsgi/
       
    22 .. _installation and configuration documentation: http://code.google.com/p/modwsgi/wiki/InstallationInstructions
       
    23 
       
    24 Basic Configuration
       
    25 ===================
       
    26 
       
    27 Once you've got mod_wsgi installed and activated, edit your ``httpd.conf`` file
       
    28 and add::
       
    29 
       
    30     WSGIScriptAlias / /path/to/mysite/apache/django.wsgi
       
    31 
       
    32 The first bit above is the url you want to be serving your application at (``/``
       
    33 indicates the root url), and the second is the location of a "WSGI file" -- see
       
    34 below -- on your system, usually inside of your project. This tells Apache
       
    35 to serve any request below the given URL using the WSGI application defined by that file.
       
    36 
       
    37 Next we'll need to actually create this WSGI application, so create the file
       
    38 mentioned in the second part of ``WSGIScriptAlias`` and add::
       
    39 
       
    40     import os
       
    41     import sys
       
    42 
       
    43     os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
       
    44 
       
    45     import django.core.handlers.wsgi
       
    46     application = django.core.handlers.wsgi.WSGIHandler()
       
    47 
       
    48 If your project is not on your ``PYTHONPATH`` by default you can add::
       
    49 
       
    50     path = '/usr/local/django'
       
    51     if path not in sys.path:
       
    52         sys.path.append(path)
       
    53 
       
    54 just above the final ``import`` line to place your project on the path. Remember to
       
    55 replace 'mysite.settings' with your correct settings file, and '/usr/local/django'
       
    56 with your own project's location.
       
    57 
       
    58 Serving media files
       
    59 ===================
       
    60 
       
    61 Django doesn't serve media files itself; it leaves that job to whichever Web
       
    62 server you choose.
       
    63 
       
    64 We recommend using a separate Web server -- i.e., one that's not also running
       
    65 Django -- for serving media. Here are some good choices:
       
    66 
       
    67     * lighttpd_
       
    68     * Nginx_
       
    69     * TUX_
       
    70     * A stripped-down version of Apache_
       
    71     * Cherokee_
       
    72 
       
    73 If, however, you have no option but to serve media files on the same Apache
       
    74 ``VirtualHost`` as Django, you can set up Apache to serve some URLs as
       
    75 static media, and others using the mod_wsgi interface to Django.
       
    76 
       
    77 This example sets up Django at the site root, but explicitly serves ``robots.txt``,
       
    78 ``favicon.ico``, any CSS file, and anything in the ``/media/`` URL space as a static
       
    79 file. All other URLs will be served using mod_wsgi::
       
    80 
       
    81     Alias /robots.txt /usr/local/wsgi/static/robots.txt
       
    82     Alias /favicon.ico /usr/local/wsgi/static/favicon.ico
       
    83 
       
    84     AliasMatch /([^/]*\.css) /usr/local/wsgi/static/styles/$1
       
    85 
       
    86     Alias /media/ /usr/local/wsgi/static/media/
       
    87 
       
    88     <Directory /usr/local/wsgi/static>
       
    89     Order deny,allow
       
    90     Allow from all
       
    91     </Directory>
       
    92 
       
    93     WSGIScriptAlias / /usr/local/wsgi/scripts/django.wsgi
       
    94 
       
    95     <Directory /usr/local/wsgi/scripts>
       
    96     Order allow,deny
       
    97     Allow from all
       
    98     </Directory>
       
    99 
       
   100 .. _lighttpd: http://www.lighttpd.net/
       
   101 .. _Nginx: http://wiki.nginx.org/Main
       
   102 .. _TUX: http://en.wikipedia.org/wiki/TUX_web_server
       
   103 .. _Apache: http://httpd.apache.org/
       
   104 .. _Cherokee: http://www.cherokee-project.com/
       
   105 
       
   106 More details on configuring a mod_wsgi site to serve static files can be found
       
   107 in the mod_wsgi documentation on `hosting static files`_.
       
   108 
       
   109 .. _hosting static files: http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#Hosting_Of_Static_Files
       
   110 
       
   111 Details
       
   112 =======
       
   113 
       
   114 For more details, see the `mod_wsgi documentation on Django integration`_,
       
   115 which explains the above in more detail, and walks through all the various
       
   116 options you've got when deploying under mod_wsgi.
       
   117 
       
   118 .. _mod_wsgi documentation on Django integration: http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango