{"title":"How to deploy with WSGI","version":"4.0","locale":"it","docname":"howto/deployment/wsgi/index","url":"/it/4.0/howto/deployment/wsgi/","canonical":"https://djangodocs.dev/it/4.0/howto/deployment/wsgi/","summary":"Django’s primary deployment platform is WSGI , the Python standard for web servers and applications. Django’s startproject management command sets up a minimal…","html":"<h1>How to deploy with WSGI<a class=\"heading-anchor\" href=\"#how-to-deploy-with-wsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Django’s primary deployment platform is <a class=\"reference external\" href=\"https://wsgi.readthedocs.io/en/latest/\">WSGI</a>, the Python standard for web\nservers and applications.</p>\n<p>Django’s <a class=\"reference internal\" href=\"/it/4.0/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a> management command sets up a minimal default\nWSGI configuration for you, which you can tweak as needed for your project,\nand direct any WSGI-compliant application server to use.</p>\n<p>Django includes getting-started documentation for the following WSGI servers:</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/it/4.0/howto/deployment/wsgi/gunicorn/\">Come installare Django con Gunicorn</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/it/4.0/howto/deployment/wsgi/uwsgi/\">Come usare Django con uWSGI</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/it/4.0/howto/deployment/wsgi/modwsgi/\">How to use Django with Apache and <code class=\"docutils literal notranslate\">mod_wsgi</code></a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/it/4.0/howto/deployment/wsgi/apache-auth/\">How to authenticate against Django’s user database from Apache</a></li>\n</ul>\n</div>\n<section id=\"the-application-object\">\n<h2>The <code class=\"docutils literal notranslate\">application</code> object<a class=\"heading-anchor\" href=\"#the-application-object\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The key concept of deploying with WSGI is the <code class=\"docutils literal notranslate\">application</code> callable which\nthe application server uses to communicate with your code. It’s commonly\nprovided as an object named <code class=\"docutils literal notranslate\">application</code> in a Python module accessible to\nthe server.</p>\n<p>The <a class=\"reference internal\" href=\"/it/4.0/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a> command creates a file\n<code class=\"file docutils literal notranslate\">&lt;project_name&gt;/wsgi.py</code> that contains such an <code class=\"docutils literal notranslate\">application</code> callable.</p>\n<p>It’s used both by Django’s development server and in production WSGI\ndeployments.</p>\n<p>WSGI servers obtain the path to the <code class=\"docutils literal notranslate\">application</code> callable from their\nconfiguration. Django’s built-in server, namely the <a class=\"reference internal\" href=\"/it/4.0/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</code></a>\ncommand, reads it from the <a class=\"reference internal\" href=\"/it/4.0/ref/settings/#std-setting-WSGI_APPLICATION\"><code class=\"xref std std-setting docutils literal notranslate\">WSGI_APPLICATION</code></a> setting. By default, it’s\nset to <code class=\"docutils literal notranslate\">&lt;project_name&gt;.wsgi.application</code>, which points to the <code class=\"docutils literal notranslate\">application</code>\ncallable in <code class=\"file docutils literal notranslate\">&lt;project_name&gt;/wsgi.py</code>.</p>\n</section>\n<section id=\"configuring-the-settings-module\">\n<h2>Configuring the settings module<a class=\"heading-anchor\" href=\"#configuring-the-settings-module\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When the WSGI server loads your application, Django needs to import the\nsettings module — that’s where your entire application is defined.</p>\n<p>Django uses the <span class=\"target\" id=\"index-0\"></span><a class=\"reference internal\" href=\"/it/4.0/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\">DJANGO_SETTINGS_MODULE</code></a> environment variable to\nlocate the appropriate settings module. It must contain the dotted path to the\nsettings module. You can use a different value for development and production;\nit all depends on how you organize your settings.</p>\n<p>If this variable isn’t set, the default <code class=\"file docutils literal notranslate\">wsgi.py</code> sets it to\n<code class=\"docutils literal notranslate\">mysite.settings</code>, where <code class=\"docutils literal notranslate\">mysite</code> is the name of your project. That’s how\n<a class=\"reference internal\" href=\"/it/4.0/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\">runserver</code></a> discovers the default settings file by default.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Nota</p>\n<p>Since environment variables are process-wide, this doesn’t work when you\nrun multiple Django sites in the same process. This happens with mod_wsgi.</p>\n<p>To avoid this problem, use mod_wsgi’s daemon mode with each site in its\nown daemon process, or override the value from the environment by\nenforcing <code class=\"docutils literal notranslate\">os.environ[&quot;DJANGO_SETTINGS_MODULE&quot;] = &quot;mysite.settings&quot;</code> in\nyour <code class=\"file docutils literal notranslate\">wsgi.py</code>.</p>\n</aside>\n</section>\n<section id=\"applying-wsgi-middleware\">\n<h2>Applying WSGI middleware<a class=\"heading-anchor\" href=\"#applying-wsgi-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To apply <span class=\"target\" id=\"index-1\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-3333/#middleware-components-that-play-both-sides\"><strong>WSGI middleware</strong></a> you can wrap the application\nobject. For instance you could add these lines at the bottom of\n<code class=\"file docutils literal notranslate\">wsgi.py</code>:</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=\"nn\">helloworld.wsgi</span> <span class=\"kn\">import</span> HelloWorldApplication\napplication <span class=\"o\">=</span> HelloWorldApplication<span class=\"p\">(</span>application<span class=\"p\">)</span>\n</code></pre></div>\n<p>You could also replace the Django WSGI application with a custom WSGI\napplication that later delegates to the Django WSGI application, if you want\nto combine a Django application with a WSGI application of another framework.</p>\n</section>","rootId":"how-to-deploy-with-wsgi","toc":[{"title":"The application object","anchor":"the-application-object","children":[]},{"title":"Configuring the settings module","anchor":"configuring-the-settings-module","children":[]},{"title":"Applying WSGI middleware","anchor":"applying-wsgi-middleware","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"«How-to» guides","url":"/it/4.0/howto/"},{"docname":"howto/deployment/index","title":"How to deploy Django","url":"/it/4.0/howto/deployment/"}],"prev":{"docname":"howto/deployment/index","title":"How to deploy Django","url":"/it/4.0/howto/deployment/"},"next":{"docname":"howto/deployment/wsgi/gunicorn","title":"Come installare Django con Gunicorn","url":"/it/4.0/howto/deployment/wsgi/gunicorn/"},"formats":{"html":"/it/4.0/howto/deployment/wsgi/","markdown":"/it/4.0/howto/deployment/wsgi.md","json":"/it/4.0/howto/deployment/wsgi.json"},"source":"https://github.com/django/django/blob/stable/4.0.x/docs/howto/deployment/wsgi/index.txt","official":"https://docs.djangoproject.com/it/4.0/howto/deployment/wsgi/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}