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