deployment.txt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. ==========================
  2. How to deploy static files
  3. ==========================
  4. .. seealso::
  5. For an introduction to the use of :mod:`django.contrib.staticfiles`, see
  6. :doc:`/howto/static-files/index`.
  7. .. _staticfiles-production:
  8. Serving static files in production
  9. ==================================
  10. The basic outline of putting static files into production consists of two
  11. steps: run the :djadmin:`collectstatic` command when static files change, then
  12. arrange for the collected static files directory (:setting:`STATIC_ROOT`) to be
  13. moved to the static file server and served. Depending on the ``staticfiles``
  14. :setting:`STORAGES` alias, files may need to be moved to a new location
  15. manually or the :func:`post_process
  16. <django.contrib.staticfiles.storage.StaticFilesStorage.post_process>` method of
  17. the ``Storage`` class might take care of that.
  18. As with all deployment tasks, the devil's in the details. Every production
  19. setup will be a bit different, so you'll need to adapt the basic outline to fit
  20. your needs. Below are a few common patterns that might help.
  21. Serving the site and your static files from the same server
  22. -----------------------------------------------------------
  23. If you want to serve your static files from the same server that's already
  24. serving your site, the process may look something like:
  25. * Push your code up to the deployment server.
  26. * On the server, run :djadmin:`collectstatic` to copy all the static files
  27. into :setting:`STATIC_ROOT`.
  28. * Configure your web server to serve the files in :setting:`STATIC_ROOT`
  29. under the URL :setting:`STATIC_URL`. For example, here's
  30. :ref:`how to do this with Apache and mod_wsgi <serving-files>`.
  31. You'll probably want to automate this process, especially if you've got
  32. multiple web servers.
  33. Serving static files from a dedicated server
  34. --------------------------------------------
  35. Most larger Django sites use a separate web server -- i.e., one that's not also
  36. running Django -- for serving static files. This server often runs a different
  37. type of web server -- faster but less full-featured. Some common choices are:
  38. * Nginx_
  39. * A stripped-down version of Apache_
  40. .. _Nginx: https://nginx.org/en/
  41. .. _Apache: https://httpd.apache.org/
  42. Configuring these servers is out of scope of this document; check each
  43. server's respective documentation for instructions.
  44. Since your static file server won't be running Django, you'll need to modify
  45. the deployment strategy to look something like:
  46. * When your static files change, run :djadmin:`collectstatic` locally.
  47. * Push your local :setting:`STATIC_ROOT` up to the static file server into the
  48. directory that's being served. `rsync <https://rsync.samba.org/>`_ is a
  49. common choice for this step since it only needs to transfer the bits of
  50. static files that have changed.
  51. .. _staticfiles-from-cdn:
  52. Serving static files from a cloud service or CDN
  53. ------------------------------------------------
  54. Another common tactic is to serve static files from a cloud storage provider
  55. like Amazon's S3 and/or a CDN (content delivery network). This lets you
  56. ignore the problems of serving static files and can often make for
  57. faster-loading web pages (especially when using a CDN).
  58. When using these services, the basic workflow would look a bit like the above,
  59. except that instead of using ``rsync`` to transfer your static files to the
  60. server you'd need to transfer the static files to the storage provider or CDN.
  61. There's any number of ways you might do this, but if the provider has an API,
  62. you can use a :doc:`custom file storage backend </howto/custom-file-storage>`
  63. to integrate the CDN with your Django project. If you've written or are using a
  64. 3rd party custom storage backend, you can tell :djadmin:`collectstatic` to use
  65. it by setting ``staticfiles`` in :setting:`STORAGES`.
  66. For example, if you've written an S3 storage backend in
  67. ``myproject.storage.S3Storage`` you could use it with::
  68. STORAGES = {
  69. # ...
  70. "staticfiles": {"BACKEND": "myproject.storage.S3Storage"}
  71. }
  72. Once that's done, all you have to do is run :djadmin:`collectstatic` and your
  73. static files would be pushed through your storage package up to S3. If you
  74. later needed to switch to a different storage provider, you may only have to
  75. change ``staticfiles`` in the :setting:`STORAGES` setting.
  76. For details on how you'd write one of these backends, see
  77. :doc:`/howto/custom-file-storage`. There are 3rd party apps available that
  78. provide storage backends for many common file storage APIs. A good starting
  79. point is the `overview at djangopackages.org
  80. <https://djangopackages.org/grids/g/storage-backends/>`_.
  81. Learn more
  82. ==========
  83. For complete details on all the settings, commands, template tags, and other
  84. pieces included in :mod:`django.contrib.staticfiles`, see :doc:`the
  85. staticfiles reference </ref/contrib/staticfiles>`.