<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang=""> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>How to deploy with WSGI — Django 1.8.19 documentation</title> <link rel="stylesheet" href="../../../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../../../', VERSION: '1.8.19', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../../../_static/jquery.js"></script> <script type="text/javascript" src="../../../_static/underscore.js"></script> <script type="text/javascript" src="../../../_static/doctools.js"></script> <link rel="index" title="Index" href="../../../genindex.html" /> <link rel="search" title="Search" href="../../../search.html" /> <link rel="top" title="Django 1.8.19 documentation" href="../../../contents.html" /> <link rel="up" title="Deploying Django" href="../index.html" /> <link rel="next" title="How to use Django with Apache and mod_wsgi" href="modwsgi.html" /> <link rel="prev" title="Deploying Django" href="../index.html" /> <script type="text/javascript" src="../../../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../../../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body role="document"> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../../index.html">Django 1.8.19 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../../../index.html">Home</a> | <a title="Table of contents" href="../../../contents.html">Table of contents</a> | <a title="Global index" href="../../../genindex.html">Index</a> | <a title="Module index" href="../../../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="../index.html" title="Deploying Django">previous</a> | <a href="../../index.html" title="&#8220;How-to&#8221; guides" accesskey="U">up</a> | <a href="modwsgi.html" title="How to use Django with Apache and mod_wsgi">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="howto-deployment-wsgi-index"> <div class="section" id="s-how-to-deploy-with-wsgi"> <span id="how-to-deploy-with-wsgi"></span><h1>How to deploy with WSGI<a class="headerlink" href="#how-to-deploy-with-wsgi" title="Permalink to this headline">¶</a></h1> <p>Django’s primary deployment platform is <a class="reference external" href="http://www.wsgi.org">WSGI</a>, the Python standard for web servers and applications.</p> <p>Django’s <a class="reference internal" href="../../../ref/django-admin.html#django-admin-startproject"><code class="xref std std-djadmin docutils literal"><span class="pre">startproject</span></code></a> management command sets up a simple default WSGI configuration for you, which you can tweak as needed for your project, and direct any WSGI-compliant application server to use.</p> <p>Django includes getting-started documentation for the following WSGI servers:</p> <div class="toctree-wrapper compound"> <ul> <li class="toctree-l1"><a class="reference internal" href="modwsgi.html">How to use Django with Apache and mod_wsgi</a></li> <li class="toctree-l1"><a class="reference internal" href="apache-auth.html">Authenticating against Django’s user database from Apache</a></li> <li class="toctree-l1"><a class="reference internal" href="gunicorn.html">How to use Django with Gunicorn</a></li> <li class="toctree-l1"><a class="reference internal" href="uwsgi.html">How to use Django with uWSGI</a></li> </ul> </div> <div class="section" id="s-the-application-object"> <span id="the-application-object"></span><h2>The <code class="docutils literal"><span class="pre">application</span></code> object<a class="headerlink" href="#the-application-object" title="Permalink to this headline">¶</a></h2> <p>The key concept of deploying with WSGI is the <code class="docutils literal"><span class="pre">application</span></code> callable which the application server uses to communicate with your code. It’s commonly provided as an object named <code class="docutils literal"><span class="pre">application</span></code> in a Python module accessible to the server.</p> <p>The <a class="reference internal" href="../../../ref/django-admin.html#django-admin-startproject"><code class="xref std std-djadmin docutils literal"><span class="pre">startproject</span></code></a> command creates a file <code class="file docutils literal"><span class="pre"><project_name>/wsgi.py</span></code> that contains such an <code class="docutils literal"><span class="pre">application</span></code> callable.</p> <p>It’s used both by Django’s development server and in production WSGI deployments.</p> <p>WSGI servers obtain the path to the <code class="docutils literal"><span class="pre">application</span></code> callable from their configuration. Django’s built-in servers, namely the <a class="reference internal" href="../../../ref/django-admin.html#django-admin-runserver"><code class="xref std std-djadmin docutils literal"><span class="pre">runserver</span></code></a> and <a class="reference internal" href="../../../ref/django-admin.html#django-admin-runfcgi"><code class="xref std std-djadmin docutils literal"><span class="pre">runfcgi</span></code></a> commands, read it from the <a class="reference internal" href="../../../ref/settings.html#std:setting-WSGI_APPLICATION"><code class="xref std std-setting docutils literal"><span class="pre">WSGI_APPLICATION</span></code></a> setting. By default, it’s set to <code class="docutils literal"><span class="pre"><project_name>.wsgi.application</span></code>, which points to the <code class="docutils literal"><span class="pre">application</span></code> callable in <code class="file docutils literal"><span class="pre"><project_name>/wsgi.py</span></code>.</p> </div> <div class="section" id="s-configuring-the-settings-module"> <span id="configuring-the-settings-module"></span><h2>Configuring the settings module<a class="headerlink" href="#configuring-the-settings-module" title="Permalink to this headline">¶</a></h2> <p>When the WSGI server loads your application, Django needs to import the settings module — that’s where your entire application is defined.</p> <p>Django uses the <span class="target" id="index-0"></span><a class="reference internal" href="../../../topics/settings.html#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> environment variable to locate the appropriate settings module. It must contain the dotted path to the settings module. You can use a different value for development and production; it all depends on how you organize your settings.</p> <p>If this variable isn’t set, the default <code class="file docutils literal"><span class="pre">wsgi.py</span></code> sets it to <code class="docutils literal"><span class="pre">mysite.settings</span></code>, where <code class="docutils literal"><span class="pre">mysite</span></code> is the name of your project. That’s how <a class="reference internal" href="../../../ref/django-admin.html#django-admin-runserver"><code class="xref std std-djadmin docutils literal"><span class="pre">runserver</span></code></a> discovers the default settings file by default.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>Since environment variables are process-wide, this doesn’t work when you run multiple Django sites in the same process. This happens with mod_wsgi.</p> <p class="last">To avoid this problem, use mod_wsgi’s daemon mode with each site in its own daemon process, or override the value from the environment by enforcing <code class="docutils literal"><span class="pre">os.environ["DJANGO_SETTINGS_MODULE"]</span> <span class="pre">=</span> <span class="pre">"mysite.settings"</span></code> in your <code class="file docutils literal"><span class="pre">wsgi.py</span></code>.</p> </div> </div> <div class="section" id="s-applying-wsgi-middleware"> <span id="applying-wsgi-middleware"></span><h2>Applying WSGI middleware<a class="headerlink" href="#applying-wsgi-middleware" title="Permalink to this headline">¶</a></h2> <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 simply wrap the application object. For instance you could add these lines at the bottom of <code class="file docutils literal"><span class="pre">wsgi.py</span></code>:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">helloworld.wsgi</span> <span class="k">import</span> <span class="n">HelloWorldApplication</span> <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> </pre></div> </div> <p>You could also replace the Django WSGI application with a custom WSGI application that later delegates to the Django WSGI application, if you want to combine a Django application with a WSGI application of another framework.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p class="last">Some third-party WSGI middleware do not call <code class="docutils literal"><span class="pre">close</span></code> on the response object after handling a request. In those cases the <a class="reference internal" href="../../../ref/signals.html#django.core.signals.request_finished" title="django.core.signals.request_finished"><code class="xref py py-data docutils literal"><span class="pre">request_finished</span></code></a> signal isn’t sent. This can result in idle connections to database and memcache servers.</p> </div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../../../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">How to deploy with WSGI</a><ul> <li><a class="reference internal" href="#the-application-object">The <code class="docutils literal"><span class="pre">application</span></code> object</a></li> <li><a class="reference internal" href="#configuring-the-settings-module">Configuring the settings module</a></li> <li><a class="reference internal" href="#applying-wsgi-middleware">Applying WSGI middleware</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="../index.html">Deploying Django</a></li> <li>Next: <a href="modwsgi.html">How to use Django with Apache and mod_wsgi</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../../index.html">Django 1.8.19 documentation</a> <ul><li><a href="../../index.html">“How-to” guides</a> <ul><li><a href="../index.html">Deploying Django</a> <ul><li>How to deploy with WSGI</li></ul> </li></ul></li></ul> </li> </ul> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../../../_sources/howto/deployment/wsgi/index.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <form class="search" action="../../../search.html" method="get"> <div><input type="text" name="q" /></div> <div><input type="submit" value="Go" /></div> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Feb 12, 2019</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="../index.html" title="Deploying Django">previous</a> | <a href="../../index.html" title="&#8220;How-to&#8221; guides" accesskey="U">up</a> | <a href="modwsgi.html" title="How to use Django with Apache and mod_wsgi">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>