modwsgi.txt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. Serving media files
  40. ===================
  41. Django doesn't serve media files itself; it leaves that job to whichever Web
  42. server you choose.
  43. We recommend using a separate Web server -- i.e., one that's not also running
  44. Django -- for serving media. Here are some good choices:
  45. * lighttpd_
  46. * Nginx_
  47. * TUX_
  48. * A stripped-down version of Apache_
  49. * Cherokee_
  50. If, however, you have no option but to serve media files on the same Apache
  51. ``VirtualHost`` as Django, you can set up Apache to serve some URLs as
  52. static media, and others using the mod_wsgi interface to Django.
  53. This example sets up Django at the site root, but explicitly serves ``robots.txt``,
  54. ``favicon.ico``, any CSS file, and anything in the ``/media/`` URL space as a static
  55. file. All other URLs will be served using mod_wsgi::
  56. Alias /robots.txt /usr/local/wsgi/static/robots.txt
  57. Alias /favicon.ico /usr/local/wsgi/static/favicon.ico
  58. AliasMatch /([^/]*\.css) /usr/local/wsgi/static/styles/$1
  59. Alias /media/ /usr/local/wsgi/static/media/
  60. <Directory /usr/local/wsgi/static>
  61. Order deny,allow
  62. Allow from all
  63. </Directory>
  64. WSGIScriptAlias / /usr/local/wsgi/scripts/django.wsgi
  65. <Directory /usr/local/wsgi/scripts>
  66. Order allow,deny
  67. Allow from all
  68. </Directory>
  69. .. _lighttpd: http://www.lighttpd.net/
  70. .. _Nginx: http://wiki.nginx.org/Main
  71. .. _TUX: http://en.wikipedia.org/wiki/TUX_web_server
  72. .. _Apache: http://httpd.apache.org/
  73. .. _Cherokee: http://www.cherokee-project.com/
  74. More details on configuring a mod_wsgi site to serve static files can be found
  75. in the mod_wsgi documentation on `hosting static files`_.
  76. .. _hosting static files: http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines#Hosting_Of_Static_Files
  77. .. _serving-the-admin-files:
  78. Serving the admin files
  79. =======================
  80. Note that the Django development server automagically serves admin media files,
  81. but this is not the case when you use any other server arrangement. You're
  82. responsible for setting up Apache, or whichever media server you're using, to
  83. serve the admin files.
  84. The admin files live in (:file:`django/contrib/admin/media`) of the Django
  85. distribution.
  86. Here are two recommended approaches:
  87. 1. Create a symbolic link to the admin media files from within your
  88. document root. This way, all of your Django-related files -- code **and**
  89. templates -- stay in one place, and you'll still be able to ``svn
  90. update`` your code to get the latest admin templates, if they change.
  91. 2. Or, copy the admin media files so that they live within your Apache
  92. document root.
  93. Details
  94. =======
  95. For more details, see the `mod_wsgi documentation on Django integration`_,
  96. which explains the above in more detail, and walks through all the various
  97. options you've got when deploying under mod_wsgi.
  98. .. _mod_wsgi documentation on Django integration: http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango