modwsgi.txt 4.1 KB

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