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