deployment.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ======================
  2. Deploying 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 is simple: run the
  11. :djadmin:`collectstatic` command when static files change, then arrange for
  12. the collected static files directory (:setting:`STATIC_ROOT`) to be moved to
  13. the static file server and served. Depending on :setting:`STATICFILES_STORAGE`,
  14. files may need to be moved to a new location manually or the :func:`post_process
  15. <django.contrib.staticfiles.storage.StaticFilesStorage.post_process>` method
  16. of the ``Storage`` class might take care of that.
  17. Of course, as with all deployment tasks, the devil's in the details. Every
  18. production setup will be a bit different, so you'll need to adapt the basic
  19. outline to fit your needs. Below are a few common patterns that might help.
  20. Serving the site and your static files from the same server
  21. -----------------------------------------------------------
  22. If you want to serve your static files from the same server that's already
  23. serving your site, the process may look something like:
  24. * Push your code up to the deployment server.
  25. * On the server, run :djadmin:`collectstatic` to copy all the static files
  26. into :setting:`STATIC_ROOT`.
  27. * Configure your web server to serve the files in :setting:`STATIC_ROOT`
  28. under the URL :setting:`STATIC_URL`. For example, here's
  29. :ref:`how to do this with Apache and mod_wsgi <serving-files>`.
  30. You'll probably want to automate this process, especially if you've got
  31. multiple web servers. There's any number of ways to do this automation, but
  32. one option that many Django developers enjoy is `Fabric
  33. <http://fabfile.org/>`_.
  34. Below, and in the following sections, we'll show off a few example fabfiles
  35. (i.e. Fabric scripts) that automate these file deployment options. The syntax
  36. of a fabfile is fairly straightforward but won't be covered here; consult
  37. `Fabric's documentation <http://docs.fabfile.org/>`_, for a complete
  38. explanation of the syntax.
  39. So, a fabfile to deploy static files to a couple of web servers might look
  40. something like::
  41. from fabric.api import *
  42. # Hosts to deploy onto
  43. env.hosts = ['www1.example.com', 'www2.example.com']
  44. # Where your project code lives on the server
  45. env.project_root = '/home/www/myproject'
  46. def deploy_static():
  47. with cd(env.project_root):
  48. run('./manage.py collectstatic -v0 --noinput')
  49. Serving static files from a dedicated server
  50. --------------------------------------------
  51. Most larger Django sites use a separate Web server -- i.e., one that's not also
  52. running Django -- for serving static files. This server often runs a different
  53. type of web server -- faster but less full-featured. Some common choices are:
  54. * Nginx_
  55. * A stripped-down version of Apache_
  56. .. _Nginx: http://wiki.nginx.org/Main
  57. .. _Apache: https://httpd.apache.org/
  58. Configuring these servers is out of scope of this document; check each
  59. server's respective documentation for instructions.
  60. Since your static file server won't be running Django, you'll need to modify
  61. the deployment strategy to look something like:
  62. * When your static files change, run :djadmin:`collectstatic` locally.
  63. * Push your local :setting:`STATIC_ROOT` up to the static file server into the
  64. directory that's being served. `rsync <https://rsync.samba.org/>`_ is a
  65. common choice for this step since it only needs to transfer the bits of
  66. static files that have changed.
  67. Here's how this might look in a fabfile::
  68. from fabric.api import *
  69. from fabric.contrib import project
  70. # Where the static files get collected locally. Your STATIC_ROOT setting.
  71. env.local_static_root = '/path/to/static'
  72. # Where the static files should go remotely
  73. env.remote_static_root = '/home/www/static.example.com'
  74. @roles('static')
  75. def deploy_static():
  76. local('./manage.py collectstatic')
  77. project.rsync_project(
  78. remote_dir=env.remote_static_root,
  79. local_dir=env.local_static_root,
  80. delete=True,
  81. )
  82. .. _staticfiles-from-cdn:
  83. Serving static files from a cloud service or CDN
  84. ------------------------------------------------
  85. Another common tactic is to serve static files from a cloud storage provider
  86. like Amazon's S3 and/or a CDN (content delivery network). This lets you
  87. ignore the problems of serving static files and can often make for
  88. faster-loading webpages (especially when using a CDN).
  89. When using these services, the basic workflow would look a bit like the above,
  90. except that instead of using ``rsync`` to transfer your static files to the
  91. server you'd need to transfer the static files to the storage provider or CDN.
  92. There's any number of ways you might do this, but if the provider has an API a
  93. :doc:`custom file storage backend </howto/custom-file-storage>` will make the
  94. process incredibly simple. If you've written or are using a 3rd party custom
  95. storage backend, you can tell :djadmin:`collectstatic` to use it by setting
  96. :setting:`STATICFILES_STORAGE` to the storage engine.
  97. For example, if you've written an S3 storage backend in
  98. ``myproject.storage.S3Storage`` you could use it with::
  99. STATICFILES_STORAGE = 'myproject.storage.S3Storage'
  100. Once that's done, all you have to do is run :djadmin:`collectstatic` and your
  101. static files would be pushed through your storage package up to S3. If you
  102. later needed to switch to a different storage provider, it could be as simple
  103. as changing your :setting:`STATICFILES_STORAGE` setting.
  104. For details on how you'd write one of these backends, see
  105. :doc:`/howto/custom-file-storage`. There are 3rd party apps available that
  106. provide storage backends for many common file storage APIs. A good starting
  107. point is the `overview at djangopackages.com
  108. <https://www.djangopackages.com/grids/g/storage-backends/>`_.
  109. Learn more
  110. ==========
  111. For complete details on all the settings, commands, template tags, and other
  112. pieces included in :mod:`django.contrib.staticfiles`, see :doc:`the
  113. staticfiles reference </ref/contrib/staticfiles>`.