checklist.txt 9.3 KB

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