{"title":"How to deploy with WSGI","version":"3.0","locale":"en","docname":"howto/deployment/wsgi/index","url":"/en/3.0/howto/deployment/wsgi/","canonical":"https://djangodocs.dev/en/3.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=\"/en/3.0/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startproject</span></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=\"/en/3.0/howto/deployment/wsgi/gunicorn/\">How to use Django with Gunicorn</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/en/3.0/howto/deployment/wsgi/uwsgi/\">How to use Django with uWSGI</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/en/3.0/howto/deployment/wsgi/modwsgi/\">How to use Django with Apache and <code class=\"docutils literal notranslate\"><span class=\"pre\">mod_wsgi</span></code></a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/en/3.0/howto/deployment/wsgi/apache-auth/\">Authenticating 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\"><span class=\"pre\">application</span></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\"><span class=\"pre\">application</span></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\"><span class=\"pre\">application</span></code> in a Python module accessible to\nthe server.</p>\n<p>The <a class=\"reference internal\" href=\"/en/3.0/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">startproject</span></code></a> command creates a file\n<code class=\"file docutils literal notranslate\"><span class=\"pre\">&lt;project_name&gt;/wsgi.py</span></code> that contains such an <code class=\"docutils literal notranslate\"><span class=\"pre\">application</span></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\"><span class=\"pre\">application</span></code> callable from their\nconfiguration. Django’s built-in server, namely the <a class=\"reference internal\" href=\"/en/3.0/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a>\ncommand, reads it from the <a class=\"reference internal\" href=\"/en/3.0/ref/settings/#std-setting-WSGI_APPLICATION\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">WSGI_APPLICATION</span></code></a> setting. By default, it’s\nset to <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;project_name&gt;.wsgi.application</span></code>, which points to the <code class=\"docutils literal notranslate\"><span class=\"pre\">application</span></code>\ncallable in <code class=\"file docutils literal notranslate\"><span class=\"pre\">&lt;project_name&gt;/wsgi.py</span></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=\"/en/3.0/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\"><span class=\"pre\">DJANGO_SETTINGS_MODULE</span></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\"><span class=\"pre\">wsgi.py</span></code> sets it to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">mysite.settings</span></code>, where <code class=\"docutils literal notranslate\"><span class=\"pre\">mysite</span></code> is the name of your project. That’s how\n<a class=\"reference internal\" href=\"/en/3.0/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a> discovers the default settings file by default.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</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\"><span class=\"pre\">os.environ[&quot;DJANGO_SETTINGS_MODULE&quot;]</span> <span class=\"pre\">=</span> <span class=\"pre\">&quot;mysite.settings&quot;</span></code> in\nyour <code class=\"file docutils literal notranslate\"><span class=\"pre\">wsgi.py</span></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 <a class=\"reference external\" href=\"https://www.python.org/dev/peps/pep-3333/#middleware-components-that-play-both-sides\">WSGI middleware</a> you can wrap the application object. For instance\nyou could add these lines at the bottom of <code class=\"file docutils literal notranslate\"><span class=\"pre\">wsgi.py</span></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=\"w\"> </span><span class=\"nn\">helloworld.wsgi</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HelloWorldApplication</span>\n<span class=\"n\">application</span> <span class=\"o\">=</span> <span class=\"n\">HelloWorldApplication</span><span class=\"p\">(</span><span class=\"n\">application</span><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":"/en/3.0/howto/"},{"docname":"howto/deployment/index","title":"Deploying Django","url":"/en/3.0/howto/deployment/"}],"prev":{"docname":"howto/deployment/index","title":"Deploying Django","url":"/en/3.0/howto/deployment/"},"next":{"docname":"howto/deployment/wsgi/gunicorn","title":"How to use Django with Gunicorn","url":"/en/3.0/howto/deployment/wsgi/gunicorn/"},"formats":{"html":"/en/3.0/howto/deployment/wsgi/","markdown":"/en/3.0/howto/deployment/wsgi.md","json":"/en/3.0/howto/deployment/wsgi.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/howto/deployment/wsgi/index.txt","official":"https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/","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","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}