checklist.txt 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. Run ``manage.py check --deploy``
  26. ================================
  27. Some of the checks described below can be automated using the :option:`check
  28. --deploy` option. Be sure to run it against your production settings file as
  29. described in the option's documentation.
  30. Critical settings
  31. =================
  32. :setting:`SECRET_KEY`
  33. ---------------------
  34. **The secret key must be a large random value and it must be kept secret.**
  35. Make sure that the key used in production isn't used anywhere else and avoid
  36. committing it to source control. This reduces the number of vectors from which
  37. an attacker may acquire the key.
  38. Instead of hardcoding the secret key in your settings module, consider loading
  39. it from an environment variable::
  40. import os
  41. SECRET_KEY = os.environ["SECRET_KEY"]
  42. or from a file::
  43. with open("/etc/secret_key.txt") as f:
  44. SECRET_KEY = f.read().strip()
  45. If rotating secret keys, you may use :setting:`SECRET_KEY_FALLBACKS`::
  46. import os
  47. SECRET_KEY = os.environ["CURRENT_SECRET_KEY"]
  48. SECRET_KEY_FALLBACKS = [
  49. os.environ["OLD_SECRET_KEY"],
  50. ]
  51. Ensure that old secret keys are removed from ``SECRET_KEY_FALLBACKS`` in a
  52. timely manner.
  53. :setting:`DEBUG`
  54. ----------------
  55. **You must never enable debug in production.**
  56. You're certainly developing your project with :setting:`DEBUG = True <DEBUG>`,
  57. since this enables handy features like full tracebacks in your browser.
  58. For a production environment, though, this is a really bad idea, because it
  59. leaks lots of information about your project: excerpts of your source code,
  60. local variables, settings, libraries used, etc.
  61. Environment-specific settings
  62. =============================
  63. :setting:`ALLOWED_HOSTS`
  64. ------------------------
  65. When :setting:`DEBUG = False <DEBUG>`, Django doesn't work at all without a
  66. suitable value for :setting:`ALLOWED_HOSTS`.
  67. This setting is required to protect your site against some CSRF attacks. If
  68. you use a wildcard, you must perform your own validation of the ``Host`` HTTP
  69. header, or otherwise ensure that you aren't vulnerable to this category of
  70. attacks.
  71. You should also configure the web server that sits in front of Django to
  72. validate the host. It should respond with a static error page or ignore
  73. requests for incorrect hosts instead of forwarding the request to Django. This
  74. way you'll avoid spurious errors in your Django logs (or emails if you have
  75. error reporting configured that way). For example, on nginx you might set up a
  76. default server to return "444 No Response" on an unrecognized host:
  77. .. code-block:: nginx
  78. server {
  79. listen 80 default_server;
  80. return 444;
  81. }
  82. :setting:`CACHES`
  83. -----------------
  84. If you're using a cache, connection parameters may be different in development
  85. and in production. Django defaults to per-process :ref:`local-memory caching
  86. <local-memory-caching>` which may not be desirable.
  87. Cache servers often have weak authentication. Make sure they only accept
  88. connections from your application servers.
  89. :setting:`DATABASES`
  90. --------------------
  91. Database connection parameters are probably different in development and in
  92. production.
  93. Database passwords are very sensitive. You should protect them exactly like
  94. :setting:`SECRET_KEY`.
  95. For maximum security, make sure database servers only accept connections from
  96. your application servers.
  97. If you haven't set up backups for your database, do it right now!
  98. :setting:`EMAIL_BACKEND` and related settings
  99. ---------------------------------------------
  100. If your site sends emails, these values need to be set correctly.
  101. By default, Django sends email from webmaster@localhost and root@localhost.
  102. However, some mail providers reject email from these addresses. To use
  103. different sender addresses, modify the :setting:`DEFAULT_FROM_EMAIL` and
  104. :setting:`SERVER_EMAIL` settings.
  105. :setting:`STATIC_ROOT` and :setting:`STATIC_URL`
  106. ------------------------------------------------
  107. Static files are automatically served by the development server. In
  108. production, you must define a :setting:`STATIC_ROOT` directory where
  109. :djadmin:`collectstatic` will copy them.
  110. See :doc:`/howto/static-files/index` for more information.
  111. :setting:`MEDIA_ROOT` and :setting:`MEDIA_URL`
  112. ----------------------------------------------
  113. Media files are uploaded by your users. They're untrusted! Make sure your web
  114. server never attempts to interpret them. For instance, if a user uploads a
  115. ``.php`` file, the web server shouldn't execute it.
  116. Now is a good time to check your backup strategy for these files.
  117. HTTPS
  118. =====
  119. Any website which allows users to log in should enforce site-wide HTTPS to
  120. avoid transmitting access tokens in clear. In Django, access tokens include
  121. the login/password, the session cookie, and password reset tokens. (You can't
  122. do much to protect password reset tokens if you're sending them by email.)
  123. Protecting sensitive areas such as the user account or the admin isn't
  124. sufficient, because the same session cookie is used for HTTP and HTTPS. Your
  125. web server must redirect all HTTP traffic to HTTPS, and only transmit HTTPS
  126. requests to Django.
  127. Once you've set up HTTPS, enable the following settings.
  128. :setting:`CSRF_COOKIE_SECURE`
  129. -----------------------------
  130. Set this to ``True`` to avoid transmitting the CSRF cookie over HTTP
  131. accidentally.
  132. :setting:`SESSION_COOKIE_SECURE`
  133. --------------------------------
  134. Set this to ``True`` to avoid transmitting the session cookie over HTTP
  135. accidentally.
  136. Performance optimizations
  137. =========================
  138. Setting :setting:`DEBUG = False <DEBUG>` disables several features that are
  139. only useful in development. In addition, you can tune the following settings.
  140. Sessions
  141. --------
  142. Consider using :ref:`cached sessions <cached-sessions-backend>` to improve
  143. performance.
  144. If using database-backed sessions, regularly :ref:`clear old sessions
  145. <clearing-the-session-store>` to avoid storing unnecessary data.
  146. :setting:`CONN_MAX_AGE`
  147. -----------------------
  148. Enabling :ref:`persistent database connections
  149. <persistent-database-connections>` can result in a nice speed-up when
  150. connecting to the database accounts for a significant part of the request
  151. processing time.
  152. This helps a lot on virtualized hosts with limited network performance.
  153. :setting:`TEMPLATES`
  154. --------------------
  155. Enabling the cached template loader often improves performance drastically, as
  156. it avoids compiling each template every time it needs to be rendered. When
  157. :setting:`DEBUG = False <DEBUG>`, the cached template loader is enabled
  158. automatically. See :class:`django.template.loaders.cached.Loader` for more
  159. information.
  160. Error reporting
  161. ===============
  162. By the time you push your code to production, it's hopefully robust, but you
  163. can't rule out unexpected errors. Thankfully, Django can capture errors and
  164. notify you accordingly.
  165. :setting:`LOGGING`
  166. ------------------
  167. Review your logging configuration before putting your website in production,
  168. and check that it works as expected as soon as you have received some traffic.
  169. See :doc:`/topics/logging` for details on logging.
  170. :setting:`ADMINS` and :setting:`MANAGERS`
  171. -----------------------------------------
  172. :setting:`ADMINS` will be notified of 500 errors by email.
  173. :setting:`MANAGERS` will be notified of 404 errors.
  174. :setting:`IGNORABLE_404_URLS` can help filter out spurious reports.
  175. See :doc:`/howto/error-reporting` for details on error reporting by email.
  176. .. admonition:: Error reporting by email doesn't scale very well
  177. Consider using an error monitoring system such as Sentry_ before your
  178. inbox is flooded by reports. Sentry can also aggregate logs.
  179. .. _Sentry: https://docs.sentry.io/
  180. Customize the default error views
  181. ---------------------------------
  182. Django includes default views and templates for several HTTP error codes. You
  183. may want to override the default templates by creating the following templates
  184. in your root template directory: ``404.html``, ``500.html``, ``403.html``, and
  185. ``400.html``. The :ref:`default error views <error-views>` that use these
  186. templates should suffice for 99% of web applications, but you can
  187. :ref:`customize them <customizing-error-views>` as well.