checklist.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. ====================
  2. Deployment checklist
  3. ====================
  4. The Internet is a hostile environment. Before deploying your Django project,
  5. you should take some time to review your settings, with security, performance,
  6. and operations in mind.
  7. Django includes many :doc:`security features </topics/security>`. Some are
  8. built-in and always enabled. Others are optional because they aren't always
  9. appropriate, or because they're inconvenient for development. For example,
  10. forcing HTTPS may not be suitable for all websites, and it's impractical for
  11. local development.
  12. Performance optimizations are another category of trade-offs with convenience.
  13. For instance, caching is useful in production, less so for local development.
  14. Error reporting needs are also widely different.
  15. The following checklist includes settings that:
  16. - must be set properly for Django to provide the expected level of security;
  17. - are expected to be different in each environment;
  18. - enable optional security features;
  19. - enable performance optimizations;
  20. - provide error reporting.
  21. Many of these settings are sensitive and should be treated as confidential. If
  22. you're releasing the source code for your project, a common practice is to
  23. publish suitable settings for development, and to use a private settings
  24. module for production.
  25. Critical settings
  26. =================
  27. :setting:`SECRET_KEY`
  28. ---------------------
  29. **The secret key must be a large random value and it must be kept secret.**
  30. Make sure that the key used in production isn't used anywhere else and avoid
  31. committing it to source control. This reduces the number of vectors from which
  32. an attacker may acquire the key.
  33. Instead of hardcoding the secret key in your settings module, consider loading
  34. it from an environment variable::
  35. import os
  36. SECRET_KEY = os.environ['SECRET_KEY']
  37. or from a file::
  38. with open('/etc/secret_key.txt') as f:
  39. SECRET_KEY = f.read().strip()
  40. :setting:`DEBUG`
  41. ----------------
  42. **You must never enable debug in production.**
  43. You're certainly developing your project with :setting:`DEBUG = True <DEBUG>`,
  44. since this enables handy features like full tracebacks in your browser.
  45. For a production environment, though, this is a really bad idea, because it
  46. leaks lots of information about your project: excerpts of your source code,
  47. local variables, settings, libraries used, etc.
  48. Environment-specific settings
  49. =============================
  50. :setting:`ALLOWED_HOSTS`
  51. ------------------------
  52. When :setting:`DEBUG = False <DEBUG>`, Django doesn't work at all without a
  53. suitable value for :setting:`ALLOWED_HOSTS`.
  54. This setting is required to protect your site against some CSRF attacks. If
  55. you use a wildcard, you must perform your own validation of the ``Host`` HTTP
  56. header, or otherwise ensure that you aren't vulnerable to this category of
  57. attacks.
  58. :setting:`CACHES`
  59. -----------------
  60. If you're using a cache, connection parameters may be different in development
  61. and in production.
  62. Cache servers often have weak authentication. Make sure they only accept
  63. connections from your application servers.
  64. If you're using Memcached, consider using :ref:`cached sessions
  65. <cached-sessions-backend>` to improve performance.
  66. :setting:`DATABASES`
  67. --------------------
  68. Database connection parameters are probably different in development and in
  69. production.
  70. Database passwords are very sensitive. You should protect them exactly like
  71. :setting:`SECRET_KEY`.
  72. For maximum security, make sure database servers only accept connections from
  73. your application servers.
  74. If you haven't set up backups for your database, do it right now!
  75. :setting:`EMAIL_BACKEND` and related settings
  76. ---------------------------------------------
  77. If your site sends emails, these values need to be set correctly.
  78. :setting:`STATIC_ROOT` and :setting:`STATIC_URL`
  79. ------------------------------------------------
  80. Static files are automatically served by the development server. In
  81. production, you must define a :setting:`STATIC_ROOT` directory where
  82. :djadmin:`collectstatic` will copy them.
  83. See :doc:`/howto/static-files/index` for more information.
  84. :setting:`MEDIA_ROOT` and :setting:`MEDIA_URL`
  85. ----------------------------------------------
  86. Media files are uploaded by your users. They're untrusted! Make sure your web
  87. server never attempt to interpret them. For instance, if a user uploads a
  88. ``.php`` file , the web server shouldn't execute it.
  89. Now is a good time to check your backup strategy for these files.
  90. HTTPS
  91. =====
  92. Any website which allows users to log in should enforce site-wide HTTPS to
  93. avoid transmitting access tokens in clear. In Django, access tokens include
  94. the login/password, the session cookie, and password reset tokens. (You can't
  95. do much to protect password reset tokens if you're sending them by email.)
  96. Protecting sensitive areas such as the user account or the admin isn't
  97. sufficient, because the same session cookie is used for HTTP and HTTPS. Your
  98. web server must redirect all HTTP traffic to HTTPS, and only transmit HTTPS
  99. requests to Django.
  100. Once you've set up HTTPS, enable the following settings.
  101. :setting:`CSRF_COOKIE_SECURE`
  102. -----------------------------
  103. Set this to ``True`` to avoid transmitting the CSRF cookie over HTTP
  104. accidentally.
  105. :setting:`SESSION_COOKIE_SECURE`
  106. --------------------------------
  107. Set this to ``True`` to avoid transmitting the session cookie over HTTP
  108. accidentally.
  109. Performance optimizations
  110. =========================
  111. Setting :setting:`DEBUG = False <DEBUG>` disables several features that are
  112. only useful in development. In addition, you can tune the following settings.
  113. :setting:`CONN_MAX_AGE`
  114. -----------------------
  115. Enabling :ref:`persistent database connections
  116. <persistent-database-connections>` can result in a nice speed-up when
  117. connecting to the database accounts for a significant part of the request
  118. processing time.
  119. This helps a lot on virtualized hosts with limited network performance.
  120. :setting:`TEMPLATE_LOADERS`
  121. ---------------------------
  122. Enabling the cached template loader often improves performance drastically, as
  123. it avoids compiling each template every time it needs to be rendered. See the
  124. :ref:`template loaders docs <template-loaders>` for more information.
  125. Error reporting
  126. ===============
  127. By the time you push your code to production, it's hopefully robust, but you
  128. can't rule out unexpected errors. Thankfully, Django can capture errors and
  129. notify you accordingly.
  130. :setting:`LOGGING`
  131. ------------------
  132. Review your logging configuration before putting your website in production,
  133. and check that it works as expected as soon as you have received some traffic.
  134. See :doc:`/topics/logging` for details on logging.
  135. :setting:`ADMINS` and :setting:`MANAGERS`
  136. -----------------------------------------
  137. :setting:`ADMINS` will be notified of 500 errors by email.
  138. :setting:`MANAGERS` will be notified of 404 errors.
  139. :setting:`IGNORABLE_404_URLS` can help filter out spurious reports.
  140. See :doc:`/howto/error-reporting` for details on error reporting by email.
  141. .. admonition: Error reporting by email doesn't scale very well
  142. Consider using an error monitoring system such as Sentry_ before your
  143. inbox is flooded by reports. Sentry can also aggregate logs.
  144. .. _Sentry: http://sentry.readthedocs.org/en/latest/
  145. Customize the default error views
  146. ---------------------------------
  147. Django includes default views and templates for several HTTP error codes. You
  148. may want to override the default templates by creating the following templates
  149. in your root template directory: ``404.html``, ``500.html``, ``403.html``, and
  150. ``400.html``. The default views should suffice for 99% of Web applications, but
  151. if you desire to customize them, see these instructions which also contain
  152. details about the default templates:
  153. * :ref:`http_not_found_view`
  154. * :ref:`http_internal_server_error_view`
  155. * :ref:`http_forbidden_view`
  156. * :ref:`http_bad_request_view`
  157. Miscellaneous
  158. =============
  159. :setting:`ALLOWED_INCLUDE_ROOTS`
  160. --------------------------------
  161. This setting is required if you're using the :ttag:`ssi` template tag.
  162. Python Options
  163. ==============
  164. It's strongly recommended that you invoke the Python process running your
  165. Django application using the `-R`_ option or with the
  166. :envvar:`PYTHONHASHSEED` environment variable set to ``random``.
  167. These options help protect your site from denial-of-service (DoS)
  168. attacks triggered by carefully crafted inputs. Such an attack can
  169. drastically increase CPU usage by causing worst-case performance when
  170. creating ``dict`` instances. See `oCERT advisory #2011-003
  171. <http://www.ocert.org/advisories/ocert-2011-003.html>`_ for more information.
  172. .. _-r: http://docs.python.org/2.7/using/cmdline.html#cmdoption-R