2
0

checklist.txt 9.6 KB

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