{"title":"Come distribuire file statici","version":"6.0","locale":"it","docname":"howto/static-files/deployment","url":"/it/6.0/howto/static-files/deployment/","canonical":"https://djangodocs.dev/it/6.0/howto/static-files/deployment/","summary":"Vedi anche Per una introduzione all’uso di django.contrib.staticfiles , vedi Come gestire file statici (Es. immagini, JavaScript, CSS) . Distribuire file statici in…","html":"<h1>Come distribuire file statici<a class=\"heading-anchor\" href=\"#how-to-deploy-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\">Vedi anche</p>\n<p>Per una introduzione all’uso di <a class=\"reference internal\" href=\"/it/6.0/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>, vedi <a class=\"reference internal\" href=\"/it/6.0/howto/static-files/\"><span class=\"doc\">Come gestire file statici (Es. immagini, JavaScript, CSS)</span></a>.</p>\n</aside>\n<section id=\"serving-static-files-in-production\">\n<span id=\"staticfiles-production\"></span><h2>Distribuire file statici in produzione<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 consists of two\nsteps: run the <a class=\"reference internal\" href=\"/it/6.0/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\narrange for the collected static files directory (<a class=\"reference internal\" href=\"/it/6.0/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\nmoved to the static file server and served. Depending on the <code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code>\n<a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STORAGES</span></code></a> alias, files may need to be moved to a new location\nmanually or the <a class=\"reference internal\" href=\"/it/6.0/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 of\nthe <code class=\"docutils literal notranslate\"><span class=\"pre\">Storage</span></code> class might take care of that.</p>\n<p>Così come tutti i task di deploy, il diavolo è nei dettagli. Ogni setup di produzione sarà differente, quindi dovrai adattare il piano di base per soddisfare le tue necessità. Ecco alcuni pattern che potrebbero essere di aiuto.</p>\n<section id=\"serving-the-site-and-your-static-files-from-the-same-server\">\n<h3>Distribuire il sito ed i file statici dallo stesso 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>Se vuoi distribuire i tuoi file statici dallo stesso server che sta attualmente distribuendo il tuo sito, puoi procedere in questo modo:</p>\n<ul class=\"simple\">\n<li><p>Invia il codice al server di distribuzione.</p></li>\n<li><p>Nel server, esegui <a class=\"reference internal\" href=\"/it/6.0/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> per copiare tutti i file statici in <a class=\"reference internal\" href=\"/it/6.0/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>Configura il tuo webserver per servire i file in <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> sotto la URL <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a>.  Per esempio, ecco <a class=\"reference internal\" href=\"/it/6.0/howto/deployment/wsgi/modwsgi/#serving-files\"><span class=\"std std-ref\">come farlo con Apache e mod_wsgi</span></a>.</p></li>\n</ul>\n<p>Probabilmente vorrai automatizzare questo processo, in special modo se ha diversi web servers.</p>\n</section>\n<section id=\"serving-static-files-from-a-dedicated-server\">\n<h3>Distribuire file statici da un server dedicato<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>Siti Django più grandi usano un web server separato – uno che non sta facendo girare Django – per servire file statici. Questo server spesso fa girare un tipo diverso di web server – più veloce ma meno completo. Alcune scelte comuni sono:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference external\" href=\"https://nginx.org/en/\">Nginx</a></p></li>\n<li><p>Una versione ridotta di <a class=\"reference external\" href=\"https://httpd.apache.org/\">Apache</a></p></li>\n</ul>\n<p>La configurazione di questi servers è al di là dello scopo di questo documento, consulta la documentazione dei rispettivi server per istruzioni.</p>\n<p>Dal momento che il tuo file server statico non eseguirà Django, dovrai modificare la strategia di distribuzione affinché sia qualcosa di simile:</p>\n<ul class=\"simple\">\n<li><p>Quando i tuoi file statici vengono modificati, lancia <a class=\"reference internal\" href=\"/it/6.0/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> localmente.</p></li>\n<li><p>Spingere <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> fino al file server statico nella directory che viene servita.`rsync &lt;<a class=\"reference external\" href=\"https://rsync.samba.org/\">https://rsync.samba.org/</a>&gt;`_ è una scelta comune per questo step perchè ha bisogno di trasferire solo i pezzi di file statici che non sono cambiati.</p></li>\n</ul>\n</section>\n<section id=\"serving-static-files-from-a-cloud-service-or-cdn\">\n<span id=\"staticfiles-from-cdn\"></span><h3>Distribuire file statici da un servizio cloud o da una 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>Un’altra tattica comune è servire i file statici da un provider cloud storage come Amazon S3 e/o CDN (content delivery network). Questo ti permette di ignorare il problema di servire i file statici e va spesso bene per pagine che devono caricarsi più velocemente (specialmente usando una CDN).</p>\n<p>Quando utilizzi questi servizi, il flusso di lavoro assomiglierà a quello visto in precedenza, ad eccezione dell’utilizzo di <code class=\"docutils literal notranslate\"><span class=\"pre\">rsync</span></code> per trasferire i tuoi file statici al server, dovrai invece trasferire i tuoi file verso lo storage provider o verso la CDN.</p>\n<p>There’s any number of ways you might do this, but if the provider has an API,\nyou can use a <a class=\"reference internal\" href=\"/it/6.0/howto/custom-file-storage/\"><span class=\"doc\">custom file storage backend</span></a>\nto integrate the CDN with your Django project. If you’ve written or are using a\n3rd party custom storage backend, you can tell <a class=\"reference internal\" href=\"/it/6.0/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> to use\nit by setting <code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code> in <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STORAGES</span></code></a>.</p>\n<p>Per esempio, e tu hai definito uno storage S3 come backend in <code class=\"docutils literal notranslate\"><span class=\"pre\">myproject.storage.S3Storage</span></code>, puoi utilizzarlo con:</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\">STORAGES</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"s2\">&quot;staticfiles&quot;</span><span class=\"p\">:</span> <span class=\"p\">{</span><span class=\"s2\">&quot;BACKEND&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;myproject.storage.S3Storage&quot;</span><span class=\"p\">}</span>\n<span class=\"p\">}</span>\n</code></pre></div>\n<p>Once that’s done, all you have to do is run <a class=\"reference internal\" href=\"/it/6.0/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, you may only have to\nchange <code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code> in the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STORAGES</span></code></a> setting.</p>\n<p>Per i dettagli su come potresti scrivere uno di questi backend, vedi <a class=\"reference internal\" href=\"/it/6.0/howto/custom-file-storage/\"><span class=\"doc\">Come scrivere una storage class personalizzata</span></a>. Sono disponibili app di terze parti che forniscono backend di archiviazione per molte API di storage file comuni. Un buon punto di partenza è la <a class=\"reference external\" href=\"https://djangopackages.org/grids/g/storage-backends/\">panoramica su djangopackages.org</a>.</p>\n</section>\n</section>\n<section id=\"learn-more\">\n<h2>Per saperne di più<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>Per avere dettagli completi su tutte le impostazioni, i comandi, i tag di template ed altri pezzi inclusi in <a class=\"reference internal\" href=\"/it/6.0/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>, visita <a class=\"reference internal\" href=\"/it/6.0/ref/contrib/staticfiles/\"><span class=\"doc\">la guida di riferimento di staticfiles</span></a>.</p>\n</section>","rootId":"how-to-deploy-static-files","toc":[{"title":"Distribuire file statici in produzione","anchor":"serving-static-files-in-production","children":[{"title":"Distribuire il sito ed i file statici dallo stesso server","anchor":"serving-the-site-and-your-static-files-from-the-same-server","children":[]},{"title":"Distribuire file statici da un server dedicato","anchor":"serving-static-files-from-a-dedicated-server","children":[]},{"title":"Distribuire file statici da un servizio cloud o da una CDN","anchor":"serving-static-files-from-a-cloud-service-or-cdn","children":[]}]},{"title":"Per saperne di più","anchor":"learn-more","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"How-to guides","url":"/it/6.0/howto/"}],"prev":{"docname":"howto/deployment/checklist","title":"Lista dei controlli per la distribuzione","url":"/it/6.0/howto/deployment/checklist/"},"next":{"docname":"howto/auth-remote-user","title":"Come autenticarsi usando REMOTE_USER","url":"/it/6.0/howto/auth-remote-user/"},"formats":{"html":"/it/6.0/howto/static-files/deployment/","markdown":"/it/6.0/howto/static-files/deployment.md","json":"/it/6.0/howto/static-files/deployment.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/howto/static-files/deployment.txt","official":"https://docs.djangoproject.com/it/6.0/howto/static-files/deployment/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}