|
| 1 | +==================== |
| 2 | +Deployment checklist |
| 3 | +==================== |
| 4 | + |
| 5 | +The Internet is a hostile environment. Before deploying your Django project, |
| 6 | +you should take some time to review your settings, with security, performance, |
| 7 | +and operations in mind. |
| 8 | + |
| 9 | +Django includes many :doc:`security features </topics/security>`. Some are |
| 10 | +built-in and always enabled. Others are optional because they aren't always |
| 11 | +appropriate, or because they're inconvenient for development. For example, |
| 12 | +forcing HTTPS may not be suitable for all websites, and it's impractical for |
| 13 | +local development. |
| 14 | + |
| 15 | +Performance optimizations are another category of trade-offs with convenience. |
| 16 | +For instance, caching is useful in production, less so for local development. |
| 17 | +Error reporting needs are also widely different. |
| 18 | + |
| 19 | +The following checklist includes settings that: |
| 20 | + |
| 21 | +- must be set properly for Django to provide the expected level of security; |
| 22 | +- are expected to be different in each environment; |
| 23 | +- enable optional security features; |
| 24 | +- enable performance optimizations; |
| 25 | +- provide error reporting. |
| 26 | + |
| 27 | +Many of these settings are sensitive and should be treated as confidential. If |
| 28 | +you're releasing the source code for your project, a common practice is to |
| 29 | +publish suitable settings for development, and to use a private settings |
| 30 | +module for production. |
| 31 | + |
| 32 | +Critical settings |
| 33 | +================= |
| 34 | + |
| 35 | +:setting:`SECRET_KEY` |
| 36 | +--------------------- |
| 37 | + |
| 38 | +**The secret key must be a large random value and it must be kept secret.** |
| 39 | + |
| 40 | +Make sure that the key used in production isn't used anywhere else and avoid |
| 41 | +committing it to source control. This reduces the number of vectors from which |
| 42 | +an attacker may acquire the key. |
| 43 | + |
| 44 | +Instead of hardcoding the secret key in your settings module, consider loading |
| 45 | +it from an environment variable:: |
| 46 | + |
| 47 | + import os |
| 48 | + SECRET_KEY = os.environ['SECRET_KEY'] |
| 49 | + |
| 50 | +or from a file:: |
| 51 | + |
| 52 | + with open('/etc/secret_key.txt') as f: |
| 53 | + SECRET_KEY = f.read().strip() |
| 54 | + |
| 55 | +:setting:`DEBUG` |
| 56 | +---------------- |
| 57 | + |
| 58 | +**You must never enable debug in production.** |
| 59 | + |
| 60 | +You're certainly developing your project with :setting:`DEBUG = True <DEBUG>`, |
| 61 | +since this enables handy features like full tracebacks in your browser. |
| 62 | + |
| 63 | +For a production environment, though, this is a really bad idea, because it |
| 64 | +leaks lots of information about your project: excerpts of your source code, |
| 65 | +local variables, settings, libraries used, etc. |
| 66 | + |
| 67 | +Environment-specific settings |
| 68 | +============================= |
| 69 | + |
| 70 | +:setting:`ALLOWED_HOSTS` |
| 71 | +------------------------ |
| 72 | + |
| 73 | +When :setting:`DEBUG = False <DEBUG>`, Django doesn't work at all without a |
| 74 | +suitable value for :setting:`ALLOWED_HOSTS`. |
| 75 | + |
| 76 | +This setting is required to protect your site against some CSRF attacks. If |
| 77 | +you use a wildcard, you must perform your own validation of the ``Host`` HTTP |
| 78 | +header, or otherwise ensure that you aren't vulnerable to this category of |
| 79 | +attacks. |
| 80 | + |
| 81 | +:setting:`CACHES` |
| 82 | +----------------- |
| 83 | + |
| 84 | +If you're using a cache, connection parameters may be different in development |
| 85 | +and in production. |
| 86 | + |
| 87 | +Cache servers often have weak authentication. Make sure they only accept |
| 88 | +connections from your application servers. |
| 89 | + |
| 90 | +:setting:`DATABASES` |
| 91 | +-------------------- |
| 92 | + |
| 93 | +Database connection parameters are probably different in development and in |
| 94 | +production. |
| 95 | + |
| 96 | +For maximum security, make sure database servers only accept connections from |
| 97 | +your application servers. |
| 98 | + |
| 99 | +If you haven't set up backups for your database, do it right now! |
| 100 | + |
| 101 | +:setting:`EMAIL_BACKEND` and related settings |
| 102 | +--------------------------------------------- |
| 103 | + |
| 104 | +If your site sends emails, these values need to be set correctly. |
| 105 | + |
| 106 | +:setting:`STATIC_ROOT` and :setting:`STATIC_URL` |
| 107 | +------------------------------------------------ |
| 108 | + |
| 109 | +Static files are automatically served by the development server. In |
| 110 | +production, you must define a :setting:`STATIC_ROOT` directory where |
| 111 | +:djadmin:`collectstatic` will copy them. |
| 112 | + |
| 113 | +See :doc:`/howto/static-files` for more information. |
| 114 | + |
| 115 | +:setting:`MEDIA_ROOT` and :setting:`MEDIA_URL` |
| 116 | +---------------------------------------------- |
| 117 | + |
| 118 | +Media files are uploaded by your users. They're untrusted! Make sure your web |
| 119 | +server never attempt to interpret them. For instance, if a user uploads a |
| 120 | +``.php`` file , the web server shouldn't execute it. |
| 121 | + |
| 122 | +Now is a good time to check your backup strategy for these files. |
| 123 | + |
| 124 | +HTTPS |
| 125 | +===== |
| 126 | + |
| 127 | +Any website which allows users to log in should enforce site-wide HTTPS to |
| 128 | +avoid transmitting access tokens in clear. In Django, access tokens include |
| 129 | +the login/password, the session cookie, and password reset tokens. (You can't |
| 130 | +do much to protect password reset tokens if you're sending them by email.) |
| 131 | + |
| 132 | +Protecting sensitive areas such as the user account or the admin isn't |
| 133 | +sufficient, because the same session cookie is used for HTTP and HTTPS. |
| 134 | + |
| 135 | +Once you've set up HTTPS, enable the following settings. |
| 136 | + |
| 137 | +:setting:`CSRF_COOKIE_SECURE` |
| 138 | +----------------------------- |
| 139 | + |
| 140 | +Set this to ``True`` to avoid transmitting the CSRF cookie over HTTP |
| 141 | +accidentally. |
| 142 | + |
| 143 | +:setting:`SESSION_COOKIE_SECURE` |
| 144 | +-------------------------------- |
| 145 | + |
| 146 | +Set this to ``True`` to avoid transmitting the session cookie over HTTP |
| 147 | +accidentally. |
| 148 | + |
| 149 | +Performance optimizations |
| 150 | +========================= |
| 151 | + |
| 152 | +Setting :setting:`DEBUG = False <DEBUG>` disables several features that are |
| 153 | +only useful in development. In addition, you can tune the following settings. |
| 154 | + |
| 155 | +:setting:`TEMPLATE_LOADERS` |
| 156 | +--------------------------- |
| 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. See the |
| 160 | +:ref:`template loaders docs <template-loaders>` for more information. |
| 161 | + |
| 162 | +Error reporting |
| 163 | +=============== |
| 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 | + |
| 169 | +:setting:`LOGGING` |
| 170 | +------------------ |
| 171 | + |
| 172 | +Review your logging configuration before putting your website in production, |
| 173 | +and check that it works as expected as soon as you have received some traffic. |
| 174 | + |
| 175 | +See :doc:`/topics/logging` for details on logging. |
| 176 | + |
| 177 | +:setting:`ADMINS` and :setting:`MANAGERS` |
| 178 | +----------------------------------------- |
| 179 | + |
| 180 | +:setting:`ADMINS` will be notified of 500 errors by email. |
| 181 | + |
| 182 | +:setting:`MANAGERS` will be notified of 404 errors. |
| 183 | +:setting:`IGNORABLE_404_URLS` can help filter out spurious reports. |
| 184 | + |
| 185 | +See :doc:`/howto/error-reporting` for details on error reporting by email. |
| 186 | + |
| 187 | +.. admonition: Error reporting by email doesn't scale very well |
| 188 | + |
| 189 | + Consider using an error monitoring system such as Sentry_ before your |
| 190 | + inbox is flooded by reports. Sentry can also aggregate logs. |
| 191 | + |
| 192 | + .. _Sentry: http://sentry.readthedocs.org/en/latest/ |
| 193 | + |
| 194 | +Miscellaneous |
| 195 | +============= |
| 196 | + |
| 197 | +:setting:`ALLOWED_INCLUDE_ROOTS` |
| 198 | +-------------------------------- |
| 199 | + |
| 200 | +This setting is required if you're using the :ttag:`ssi` template tag. |
0 commit comments