{"title":"Deploying static files","version":"1.10","locale":"en","docname":"howto/static-files/deployment","url":"/en/1.10/howto/static-files/deployment/","canonical":"https://djangodocs.dev/en/1.10/howto/static-files/deployment/","summary":"See also For an introduction to the use of django.contrib.staticfiles , see Managing static files (e.g. images, JavaScript, CSS) . Serving static files in…","html":"<h1>Deploying static files<a class=\"heading-anchor\" href=\"#deploying-static-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">See also</p>\n<p>For an introduction to the use of <a class=\"reference internal\" href=\"/en/1.10/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a>, see\n<a class=\"reference internal\" href=\"/en/1.10/howto/static-files/\"><span class=\"doc\">Managing static files (e.g. images, JavaScript, CSS)</span></a>.</p>\n</aside>\n<section id=\"serving-static-files-in-production\">\n<span id=\"staticfiles-production\"></span><h2>Serving static files in production<a class=\"heading-anchor\" href=\"#serving-static-files-in-production\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The basic outline of putting static files into production is simple: run the\n<a class=\"reference internal\" href=\"/en/1.10/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> command when static files change, then arrange for\nthe collected static files directory (<a class=\"reference internal\" href=\"/en/1.10/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a>) to be moved to\nthe static file server and served. Depending on <a class=\"reference internal\" href=\"/en/1.10/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_STORAGE</span></code></a>,\nfiles may need to be moved to a new location manually or the <a class=\"reference internal\" href=\"/en/1.10/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.StaticFilesStorage.post_process\" title=\"django.contrib.staticfiles.storage.StaticFilesStorage.post_process\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">post_process</span></code></a> method\nof the <code class=\"docutils literal notranslate\"><span class=\"pre\">Storage</span></code> class might take care of that.</p>\n<p>Of course, as with all deployment tasks, the devil’s in the details. Every\nproduction setup will be a bit different, so you’ll need to adapt the basic\noutline to fit your needs. Below are a few common patterns that might help.</p>\n<section id=\"serving-the-site-and-your-static-files-from-the-same-server\">\n<h3>Serving the site and your static files from the same server<a class=\"heading-anchor\" href=\"#serving-the-site-and-your-static-files-from-the-same-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you want to serve your static files from the same server that’s already\nserving your site, the process may look something like:</p>\n<ul class=\"simple\">\n<li><p>Push your code up to the deployment server.</p></li>\n<li><p>On the server, run <a class=\"reference internal\" href=\"/en/1.10/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> to copy all the static files\ninto <a class=\"reference internal\" href=\"/en/1.10/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a>.</p></li>\n<li><p>Configure your web server to serve the files in <a class=\"reference internal\" href=\"/en/1.10/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a>\nunder the URL <a class=\"reference internal\" href=\"/en/1.10/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a>. For example, here’s\n<a class=\"reference internal\" href=\"/en/1.10/howto/deployment/wsgi/modwsgi/#serving-files\"><span class=\"std std-ref\">how to do this with Apache and mod_wsgi</span></a>.</p></li>\n</ul>\n<p>You’ll probably want to automate this process, especially if you’ve got\nmultiple web servers. There’s any number of ways to do this automation, but\none option that many Django developers enjoy is <a class=\"reference external\" href=\"http://fabfile.org/\">Fabric</a>.</p>\n<p>Below, and in the following sections, we’ll show off a few example fabfiles\n(i.e. Fabric scripts) that automate these file deployment options. The syntax\nof a fabfile is fairly straightforward but won’t be covered here; consult\n<a class=\"reference external\" href=\"http://docs.fabfile.org/\">Fabric’s documentation</a>, for a complete\nexplanation of the syntax.</p>\n<p>So, a fabfile to deploy static files to a couple of web servers might look\nsomething like:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">fabric.api</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"o\">*</span>\n\n<span class=\"c1\"># Hosts to deploy onto</span>\n<span class=\"n\">env</span><span class=\"o\">.</span><span class=\"n\">hosts</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s1\">&#39;www1.example.com&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;www2.example.com&#39;</span><span class=\"p\">]</span>\n\n<span class=\"c1\"># Where your project code lives on the server</span>\n<span class=\"n\">env</span><span class=\"o\">.</span><span class=\"n\">project_root</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;/home/www/myproject&#39;</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">deploy_static</span><span class=\"p\">():</span>\n    <span class=\"k\">with</span> <span class=\"n\">cd</span><span class=\"p\">(</span><span class=\"n\">env</span><span class=\"o\">.</span><span class=\"n\">project_root</span><span class=\"p\">):</span>\n        <span class=\"n\">run</span><span class=\"p\">(</span><span class=\"s1\">&#39;./manage.py collectstatic -v0 --noinput&#39;</span><span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"serving-static-files-from-a-dedicated-server\">\n<h3>Serving static files from a dedicated server<a class=\"heading-anchor\" href=\"#serving-static-files-from-a-dedicated-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Most larger Django sites use a separate Web server – i.e., one that’s not also\nrunning Django – for serving static files. This server often runs a different\ntype of web server – faster but less full-featured. Some common choices are:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference external\" href=\"http://wiki.nginx.org/Main\">Nginx</a></p></li>\n<li><p>A stripped-down version of <a class=\"reference external\" href=\"https://httpd.apache.org/\">Apache</a></p></li>\n</ul>\n<p>Configuring these servers is out of scope of this document; check each\nserver’s respective documentation for instructions.</p>\n<p>Since your static file server won’t be running Django, you’ll need to modify\nthe deployment strategy to look something like:</p>\n<ul class=\"simple\">\n<li><p>When your static files change, run <a class=\"reference internal\" href=\"/en/1.10/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> locally.</p></li>\n<li><p>Push your local <a class=\"reference internal\" href=\"/en/1.10/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> up to the static file server into the\ndirectory that’s being served. <a class=\"reference external\" href=\"https://rsync.samba.org/\">rsync</a> is a\ncommon choice for this step since it only needs to transfer the bits of\nstatic files that have changed.</p></li>\n</ul>\n<p>Here’s how this might look in a fabfile:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">fabric.api</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"o\">*</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">fabric.contrib</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">project</span>\n\n<span class=\"c1\"># Where the static files get collected locally. Your STATIC_ROOT setting.</span>\n<span class=\"n\">env</span><span class=\"o\">.</span><span class=\"n\">local_static_root</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;/path/to/static&#39;</span>\n\n<span class=\"c1\"># Where the static files should go remotely</span>\n<span class=\"n\">env</span><span class=\"o\">.</span><span class=\"n\">remote_static_root</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;/home/www/static.example.com&#39;</span>\n\n<span class=\"nd\">@roles</span><span class=\"p\">(</span><span class=\"s1\">&#39;static&#39;</span><span class=\"p\">)</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">deploy_static</span><span class=\"p\">():</span>\n    <span class=\"n\">local</span><span class=\"p\">(</span><span class=\"s1\">&#39;./manage.py collectstatic&#39;</span><span class=\"p\">)</span>\n    <span class=\"n\">project</span><span class=\"o\">.</span><span class=\"n\">rsync_project</span><span class=\"p\">(</span>\n        <span class=\"n\">remote_dir</span><span class=\"o\">=</span><span class=\"n\">env</span><span class=\"o\">.</span><span class=\"n\">remote_static_root</span><span class=\"p\">,</span>\n        <span class=\"n\">local_dir</span><span class=\"o\">=</span><span class=\"n\">env</span><span class=\"o\">.</span><span class=\"n\">local_static_root</span><span class=\"p\">,</span>\n        <span class=\"n\">delete</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">,</span>\n    <span class=\"p\">)</span>\n</code></pre></div>\n</section>\n<section id=\"serving-static-files-from-a-cloud-service-or-cdn\">\n<span id=\"staticfiles-from-cdn\"></span><h3>Serving static files from a cloud service or CDN<a class=\"heading-anchor\" href=\"#serving-static-files-from-a-cloud-service-or-cdn\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Another common tactic is to serve static files from a cloud storage provider\nlike Amazon’s S3 and/or a CDN (content delivery network). This lets you\nignore the problems of serving static files and can often make for\nfaster-loading Web pages (especially when using a CDN).</p>\n<p>When using these services, the basic workflow would look a bit like the above,\nexcept that instead of using <code class=\"docutils literal notranslate\"><span class=\"pre\">rsync</span></code> to transfer your static files to the\nserver you’d need to transfer the static files to the storage provider or CDN.</p>\n<p>There’s any number of ways you might do this, but if the provider has an API a\n<a class=\"reference internal\" href=\"/en/1.10/howto/custom-file-storage/\"><span class=\"doc\">custom file storage backend</span></a> will make the\nprocess incredibly simple. If you’ve written or are using a 3rd party custom\nstorage backend, you can tell <a class=\"reference internal\" href=\"/en/1.10/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> to use it by setting\n<a class=\"reference internal\" href=\"/en/1.10/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_STORAGE</span></code></a> to the storage engine.</p>\n<p>For example, if you’ve written an S3 storage backend in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">myproject.storage.S3Storage</span></code> you could use it with:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">STATICFILES_STORAGE</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;myproject.storage.S3Storage&#39;</span>\n</code></pre></div>\n<p>Once that’s done, all you have to do is run <a class=\"reference internal\" href=\"/en/1.10/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> and your\nstatic files would be pushed through your storage package up to S3. If you\nlater needed to switch to a different storage provider, it could be as simple\nas changing your <a class=\"reference internal\" href=\"/en/1.10/ref/settings/#std-setting-STATICFILES_STORAGE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_STORAGE</span></code></a> setting.</p>\n<p>For details on how you’d write one of these backends, see\n<a class=\"reference internal\" href=\"/en/1.10/howto/custom-file-storage/\"><span class=\"doc\">Writing a custom storage system</span></a>. There are 3rd party apps available that\nprovide storage backends for many common file storage APIs. A good starting\npoint is the <a class=\"reference external\" href=\"https://www.djangopackages.com/grids/g/storage-backends/\">overview at djangopackages.com</a>.</p>\n</section>\n</section>\n<section id=\"learn-more\">\n<h2>Learn more<a class=\"heading-anchor\" href=\"#learn-more\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>For complete details on all the settings, commands, template tags, and other\npieces included in <a class=\"reference internal\" href=\"/en/1.10/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a>, see <a class=\"reference internal\" href=\"/en/1.10/ref/contrib/staticfiles/\"><span class=\"doc\">the\nstaticfiles reference</span></a>.</p>\n</section>","rootId":"deploying-static-files","toc":[{"title":"Serving static files in production","anchor":"serving-static-files-in-production","children":[{"title":"Serving the site and your static files from the same server","anchor":"serving-the-site-and-your-static-files-from-the-same-server","children":[]},{"title":"Serving static files from a dedicated server","anchor":"serving-static-files-from-a-dedicated-server","children":[]},{"title":"Serving static files from a cloud service or CDN","anchor":"serving-static-files-from-a-cloud-service-or-cdn","children":[]}]},{"title":"Learn more","anchor":"learn-more","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"“How-to” guides","url":"/en/1.10/howto/"}],"prev":{"docname":"howto/static-files/index","title":"Managing static files (e.g. images, JavaScript, CSS)","url":"/en/1.10/howto/static-files/"},"next":{"docname":"howto/windows","title":"How to install Django on Windows","url":"/en/1.10/howto/windows/"},"formats":{"html":"/en/1.10/howto/static-files/deployment/","markdown":"/en/1.10/howto/static-files/deployment.md","json":"/en/1.10/howto/static-files/deployment.json"},"source":"https://github.com/django/django/blob/stable/1.10.x/docs/howto/static-files/deployment.txt","official":"https://docs.djangoproject.com/en/1.10/howto/static-files/deployment/","inVersions":["dev","6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9","1.8"],"inLocales":["en","fr","ja","id","pt-br","es","el","pl"]}