<!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>How to use Django with uWSGI — Django 1.4.11 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.4.11', 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="top" title="Django 1.4.11 documentation" href="../../../index.html" /> <link rel="up" title="How to deploy with WSGI" href="index.html" /> <link rel="next" title="How to use Django with FastCGI, SCGI, or AJP" href="../fastcgi.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> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../../../index.html">Django 1.4.11 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="../fastcgi.html" title="How to use Django with FastCGI, SCGI, or AJP">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="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://projects.unbit.it/uwsgi/wiki/Install">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-bash"><div class="highlight"><pre><span class="c"># Install current stable version.</span> <span class="nv">$ </span>sudo pip install uwsgi <span class="c"># Or install LTS (long term support).</span> <span class="nv">$ </span>sudo pip install http://projects.unbit.it/downloads/uwsgi-lts.tar.gz </pre></div> </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="http://projects.unbit.it/uwsgi/wiki/Doc">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>uwsgi --chdir<span class="o">=</span>/path/to/your/project --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="c"># can also be a file</span> --processes<span class="o">=</span>5 <span class="se">\ </span> <span class="c"># number of worker processes</span> --uid<span class="o">=</span>1000 --gid<span class="o">=</span>2000 <span class="se">\ </span> <span class="c"># if root, uwsgi can drop privileges</span> --harakiri<span class="o">=</span>20 <span class="se">\ </span> <span class="c"># respawn processes taking more than 20 seconds</span> --limit-as<span class="o">=</span>128 <span class="se">\ </span> <span class="c"># limit the project to 128 MB</span> --max-requests<span class="o">=</span>5000 <span class="se">\ </span> <span class="c"># respawn processes after serving 5000 requests</span> --vacuum <span class="se">\ </span> <span class="c"># clear environment on exit</span> --home<span class="o">=</span>/path/to/virtual/env <span class="se">\ </span> <span class="c"># optional path to a virtualenv</span> --daemonize<span class="o">=</span>/var/log/uwsgi/yourproject.log <span class="c"># background the process</span> </pre></div> </div> <p>This assumes you have a top-level project package named <tt class="docutils literal"><span class="pre">mysite</span></tt>, and within it a module <tt class="file docutils literal"><span class="pre">mysite/wsgi.py</span></tt> that contains a WSGI <tt class="docutils literal"><span class="pre">application</span></tt> object. This is the layout you’ll have if you ran <tt class="docutils literal"><span class="pre">django-admin.py</span> <span class="pre">startproject</span> <span class="pre">mysite</span></tt> (using your own project name in place of <tt class="docutils literal"><span class="pre">mysite</span></tt>) 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"><em>How to deploy with WSGI</em></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><tt class="docutils literal"><span class="pre">chdir</span></tt>: The path to the directory that needs to be on Python’s import path – i.e., the directory containing the <tt class="docutils literal"><span class="pre">mysite</span></tt> package.</li> <li><tt class="docutils literal"><span class="pre">module</span></tt>: The WSGI module to use – probably the <tt class="docutils literal"><span class="pre">mysite.wsgi</span></tt> module that <a class="reference internal" href="../../../ref/django-admin.html#django-admin-startproject"><tt class="xref std std-djadmin docutils literal"><span class="pre">startproject</span></tt></a> creates.</li> <li><tt class="docutils literal"><span class="pre">env</span></tt>: Should probably contain at least <tt class="docutils literal"><span class="pre">DJANGO_SETTINGS_MODULE</span></tt>.</li> <li><tt class="docutils literal"><span class="pre">home</span></tt>: Optional path to your project virtualenv.</li> </ul> <p>Example ini configuration file:</p> <div class="highlight-bash"><div class="highlight"><pre><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>uwsgi --ini uwsgi.ini </pre></div> </div> <p>See the uWSGI docs on <a class="reference external" href="http://projects.unbit.it/uwsgi/wiki/Management">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"> <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="../fastcgi.html">How to use Django with FastCGI, SCGI, or AJP</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../../../index.html">Django 1.4.11 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> <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 id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../../../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Apr 24, 2014</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="../fastcgi.html" title="How to use Django with FastCGI, SCGI, or AJP">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>