<!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 use Django with uWSGI — 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="How to deploy with WSGI" href="index.html" /> <link rel="next" title="Deployment checklist" href="../checklist.html" /> <link rel="prev" title="How to use Django with Gunicorn" href="gunicorn.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="gunicorn.html" title="How to use Django with Gunicorn">previous</a> | <a href="../../index.html" title="&#8220;How-to&#8221; guides" accesskey="U">up</a> | <a href="../checklist.html" title="Deployment checklist">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="howto-deployment-wsgi-uwsgi"> <div class="section" id="s-how-to-use-django-with-uwsgi"> <span id="how-to-use-django-with-uwsgi"></span><h1>How to use Django with uWSGI<a class="headerlink" href="#how-to-use-django-with-uwsgi" title="Permalink to this headline">¶</a></h1> <p><a class="reference external" href="http://projects.unbit.it/uwsgi/">uWSGI</a> is a fast, self-healing and developer/sysadmin-friendly application container server coded in pure C.</p> <div class="admonition seealso"> <p class="first admonition-title">See also</p> <p class="last">The uWSGI docs offer a <a class="reference external" href="https://uwsgi.readthedocs.org/en/latest/tutorials/Django_and_nginx.html">tutorial</a> covering Django, nginx, and uWSGI (one possible deployment setup of many). The docs below are focused on how to integrate Django with uWSGI.</p> </div> <div class="section" id="s-prerequisite-uwsgi"> <span id="prerequisite-uwsgi"></span><h2>Prerequisite: uWSGI<a class="headerlink" href="#prerequisite-uwsgi" title="Permalink to this headline">¶</a></h2> <p>The uWSGI wiki describes several <a class="reference external" href="http://uwsgi-docs.readthedocs.org/en/latest/Install.html">installation procedures</a>. Using pip, the Python package manager, you can install any uWSGI version with a single command. For example:</p> <div class="highlight-console"><div class="highlight"><pre><span></span><span class="gp">#</span> Install current stable version. <span class="gp">$</span> pip install uwsgi <span class="gp">#</span> Or install LTS <span class="o">(</span>long term support<span class="o">)</span>. <span class="gp">$</span> pip install http://projects.unbit.it/downloads/uwsgi-lts.tar.gz </pre></div> </div> <div class="admonition warning"> <p class="first admonition-title">Warning</p> <p class="last">Some distributions, including Debian and Ubuntu, ship an outdated version of uWSGI that does not conform to the WSGI specification. Versions prior to 1.2.6 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 class="section" id="s-uwsgi-model"> <span id="uwsgi-model"></span><h3>uWSGI model<a class="headerlink" href="#uwsgi-model" title="Permalink to this headline">¶</a></h3> <p>uWSGI operates on a client-server model. Your Web server (e.g., nginx, Apache) communicates with a django-uwsgi “worker” process to serve dynamic content. See uWSGI’s <a class="reference external" href="http://projects.unbit.it/uwsgi/wiki/Background">background documentation</a> for more detail.</p> </div> <div class="section" id="s-configuring-and-starting-the-uwsgi-server-for-django"> <span id="configuring-and-starting-the-uwsgi-server-for-django"></span><h3>Configuring and starting the uWSGI server for Django<a class="headerlink" href="#configuring-and-starting-the-uwsgi-server-for-django" title="Permalink to this headline">¶</a></h3> <p>uWSGI supports multiple ways to configure the process. See uWSGI’s <a class="reference external" href="https://uwsgi.readthedocs.org/en/latest/Configuration.html">configuration documentation</a> and <a class="reference external" href="http://projects.unbit.it/uwsgi/wiki/Example">examples</a>.</p> <p>Here’s an example command to start a uWSGI server:</p> <div class="highlight-bash"><div class="highlight"><pre><span></span>uwsgi --chdir<span class="o">=</span>/path/to/your/project <span class="se">\</span> --module<span class="o">=</span>mysite.wsgi:application <span class="se">\</span> --env <span class="nv">DJANGO_SETTINGS_MODULE</span><span class="o">=</span>mysite.settings <span class="se">\</span> --master --pidfile<span class="o">=</span>/tmp/project-master.pid <span class="se">\</span> --socket<span class="o">=</span>127.0.0.1:49152 <span class="se">\ </span> <span class="c1"># can also be a file</span> --processes<span class="o">=</span><span class="m">5</span> <span class="se">\ </span> <span class="c1"># number of worker processes</span> --uid<span class="o">=</span><span class="m">1000</span> --gid<span class="o">=</span><span class="m">2000</span> <span class="se">\ </span> <span class="c1"># if root, uwsgi can drop privileges</span> --harakiri<span class="o">=</span><span class="m">20</span> <span class="se">\ </span> <span class="c1"># respawn processes taking more than 20 seconds</span> --max-requests<span class="o">=</span><span class="m">5000</span> <span class="se">\ </span> <span class="c1"># respawn processes after serving 5000 requests</span> --vacuum <span class="se">\ </span> <span class="c1"># clear environment on exit</span> --home<span class="o">=</span>/path/to/virtual/env <span class="se">\ </span> <span class="c1"># optional path to a virtualenv</span> --daemonize<span class="o">=</span>/var/log/uwsgi/yourproject.log <span class="c1"># background the process</span> </pre></div> </div> <p>This assumes you have a top-level project package named <code class="docutils literal"><span class="pre">mysite</span></code>, and within it a module <code class="file docutils literal"><span class="pre">mysite/wsgi.py</span></code> that contains a WSGI <code class="docutils literal"><span class="pre">application</span></code> object. This is the layout you’ll have if you ran <code class="docutils literal"><span class="pre">django-admin</span> <span class="pre">startproject</span> <span class="pre">mysite</span></code> (using your own project name in place of <code class="docutils literal"><span class="pre">mysite</span></code>) with a recent version of Django. If this file doesn’t exist, you’ll need to create it. See the <a class="reference internal" href="index.html"><span class="doc">How to deploy with WSGI</span></a> documentation for the default contents you should put in this file and what else you can add to it.</p> <p>The Django-specific options here are:</p> <ul class="simple"> <li><code class="docutils literal"><span class="pre">chdir</span></code>: The path to the directory that needs to be on Python’s import path – i.e., the directory containing the <code class="docutils literal"><span class="pre">mysite</span></code> package.</li> <li><code class="docutils literal"><span class="pre">module</span></code>: The WSGI module to use – probably the <code class="docutils literal"><span class="pre">mysite.wsgi</span></code> module that <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> creates.</li> <li><code class="docutils literal"><span class="pre">env</span></code>: Should probably contain at least <code class="docutils literal"><span class="pre">DJANGO_SETTINGS_MODULE</span></code>.</li> <li><code class="docutils literal"><span class="pre">home</span></code>: Optional path to your project virtualenv.</li> </ul> <p>Example ini configuration file:</p> <div class="highlight-bash"><div class="highlight"><pre><span></span><span class="o">[</span>uwsgi<span class="o">]</span> <span class="nv">chdir</span><span class="o">=</span>/path/to/your/project <span class="nv">module</span><span class="o">=</span>mysite.wsgi:application <span class="nv">master</span><span class="o">=</span>True <span class="nv">pidfile</span><span class="o">=</span>/tmp/project-master.pid <span class="nv">vacuum</span><span class="o">=</span>True max-requests<span class="o">=</span>5000 <span class="nv">daemonize</span><span class="o">=</span>/var/log/uwsgi/yourproject.log </pre></div> </div> <p>Example ini configuration file usage:</p> <div class="highlight-bash"><div class="highlight"><pre><span></span>uwsgi --ini uwsgi.ini </pre></div> </div> <div class="admonition-fixing-unicodeencodeerror-for-file-uploads admonition"> <p class="first admonition-title">Fixing <code class="docutils literal"><span class="pre">UnicodeEncodeError</span></code> for file uploads</p> <p>If you get a <code class="docutils literal"><span class="pre">UnicodeEncodeError</span></code> when uploading files with file names that contain non-ASCII characters, make sure uWSGI is configured to accept non-ASCII file names by adding this to your <code class="docutils literal"><span class="pre">uwsgi.ini</span></code>:</p> <div class="highlight-bash"><div class="highlight"><pre><span></span><span class="nv">env</span> <span class="o">=</span> <span class="nv">LANG</span><span class="o">=</span>en_US.UTF-8 </pre></div> </div> <p class="last">See the <a class="reference internal" href="../../../ref/unicode.html#unicode-files"><span class="std std-ref">Files</span></a> section of the Unicode reference guide for details.</p> </div> <p>See the uWSGI docs on <a class="reference external" href="http://uwsgi-docs.readthedocs.org/en/latest/Management.html">managing the uWSGI process</a> for information on starting, stopping and reloading the uWSGI workers.</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 use Django with uWSGI</a><ul> <li><a class="reference internal" href="#prerequisite-uwsgi">Prerequisite: uWSGI</a><ul> <li><a class="reference internal" href="#uwsgi-model">uWSGI model</a></li> <li><a class="reference internal" href="#configuring-and-starting-the-uwsgi-server-for-django">Configuring and starting the uWSGI server for Django</a></li> </ul> </li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="gunicorn.html">How to use Django with Gunicorn</a></li> <li>Next: <a href="../checklist.html">Deployment checklist</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><a href="index.html">How to deploy with WSGI</a> <ul><li>How to use Django with uWSGI</li></ul> </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/uwsgi.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">Mar 10, 2018</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="gunicorn.html" title="How to use Django with Gunicorn">previous</a> | <a href="../../index.html" title="&#8220;How-to&#8221; guides" accesskey="U">up</a> | <a href="../checklist.html" title="Deployment checklist">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>