{"title":"How to use Django with uWSGI","version":"4.1","locale":"en","docname":"howto/deployment/wsgi/uwsgi","url":"/en/4.1/howto/deployment/wsgi/uwsgi/","canonical":"https://djangodocs.dev/en/4.1/howto/deployment/wsgi/uwsgi/","summary":"uWSGI is a fast, self-healing and developer/sysadmin-friendly application container server coded in pure C. See also The uWSGI docs offer a tutorial covering…","html":"<h1>How to use Django with uWSGI<a class=\"heading-anchor\" href=\"#how-to-use-django-with-uwsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p><a class=\"reference external\" href=\"https://uwsgi-docs.readthedocs.io/\">uWSGI</a> is a fast, self-healing and developer/sysadmin-friendly application\ncontainer server coded in pure C.</p>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">See also</p>\n<p>The uWSGI docs offer a <a class=\"reference external\" href=\"https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html\">tutorial</a> covering Django, nginx, and uWSGI (one\npossible deployment setup of many). The docs below are focused on how to\nintegrate Django with uWSGI.</p>\n</aside>\n<section id=\"prerequisite-uwsgi\">\n<h2>Prerequisite: uWSGI<a class=\"heading-anchor\" href=\"#prerequisite-uwsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The uWSGI wiki describes several <a class=\"reference external\" href=\"https://uwsgi-docs.readthedocs.io/en/latest/Install.html\">installation procedures</a>. Using pip, the\nPython package manager, you can install any uWSGI version with a single\ncommand. For example:</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</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=\"Shell code\"><code><span class=\"gp\"># </span>Install current stable version.\n<span class=\"gp\">$ </span>python -m pip install uwsgi\n\n<span class=\"gp\"># </span>Or install LTS <span class=\"o\">(</span>long term support<span class=\"o\">)</span>.\n<span class=\"gp\">$ </span>python -m pip install https://projects.unbit.it/downloads/uwsgi-lts.tar.gz\n</code></pre></div>\n<section id=\"uwsgi-model\">\n<h3>uWSGI model<a class=\"heading-anchor\" href=\"#uwsgi-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>uWSGI operates on a client-server model. Your web server (e.g., nginx, Apache)\ncommunicates with a <code class=\"docutils literal notranslate\">django-uwsgi</code> “worker” process to serve dynamic content.</p>\n</section>\n<section id=\"configuring-and-starting-the-uwsgi-server-for-django\">\n<h3>Configuring and starting the uWSGI server for Django<a class=\"heading-anchor\" href=\"#configuring-and-starting-the-uwsgi-server-for-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>uWSGI supports multiple ways to configure the process. See uWSGI’s\n<a class=\"reference external\" href=\"https://uwsgi.readthedocs.io/en/latest/Configuration.html\">configuration documentation</a>.</p>\n<p>Here’s an example command to start a uWSGI server:</p>\n<div class=\"code-block\" data-language=\"bash\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</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=\"Shell code\"><code>uwsgi --chdir<span class=\"o\">=</span>/path/to/your/project <span class=\"se\">\\</span>\n    --module<span class=\"o\">=</span>mysite.wsgi:application <span class=\"se\">\\</span>\n    --env <span class=\"nv\">DJANGO_SETTINGS_MODULE</span><span class=\"o\">=</span>mysite.settings <span class=\"se\">\\</span>\n    --master --pidfile<span class=\"o\">=</span>/tmp/project-master.pid <span class=\"se\">\\</span>\n    --socket<span class=\"o\">=</span><span class=\"m\">127</span>.0.0.1:49152 <span class=\"se\">\\ </span>     <span class=\"c1\"># can also be a file</span>\n    --processes<span class=\"o\">=</span><span class=\"m\">5</span> <span class=\"se\">\\ </span>                <span class=\"c1\"># number of worker processes</span>\n    --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>\n    --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>\n    --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>\n    --vacuum <span class=\"se\">\\ </span>                     <span class=\"c1\"># clear environment on exit</span>\n    --home<span class=\"o\">=</span>/path/to/virtual/env <span class=\"se\">\\ </span>  <span class=\"c1\"># optional path to a virtual environment</span>\n    --daemonize<span class=\"o\">=</span>/var/log/uwsgi/yourproject.log      <span class=\"c1\"># background the process</span>\n</code></pre></div>\n<p>This assumes you have a top-level project package named <code class=\"docutils literal notranslate\">mysite</code>, and\nwithin it a module <code class=\"file docutils literal notranslate\">mysite/wsgi.py</code> that contains a WSGI <code class=\"docutils literal notranslate\">application</code>\nobject. This is the layout you’ll have if you ran <code class=\"docutils literal notranslate\">django-admin\nstartproject mysite</code> (using your own project name in place of <code class=\"docutils literal notranslate\">mysite</code>) with\na recent version of Django. If this file doesn’t exist, you’ll need to create\nit. See the <a class=\"reference internal\" href=\"/en/4.1/howto/deployment/wsgi/\"><span class=\"doc\">How to deploy with WSGI</span></a> documentation for the default\ncontents you should put in this file and what else you can add to it.</p>\n<p>The Django-specific options here are:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\">chdir</code>: The path to the directory that needs to be on Python’s import\npath – i.e., the directory containing the <code class=\"docutils literal notranslate\">mysite</code> package.</p></li>\n<li><p><code class=\"docutils literal notranslate\">module</code>: The WSGI module to use – probably the <code class=\"docutils literal notranslate\">mysite.wsgi</code> module\nthat <a class=\"reference internal\" href=\"/en/4.1/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a> creates.</p></li>\n<li><p><code class=\"docutils literal notranslate\">env</code>: Should probably contain at least <span class=\"target\" id=\"index-0\"></span><a class=\"reference internal\" href=\"/en/4.1/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\">DJANGO_SETTINGS_MODULE</code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\">home</code>: Optional path to your project virtual environment.</p></li>\n</ul>\n<p>Example ini configuration file:</p>\n<div class=\"code-block\" data-language=\"bash\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</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=\"Shell code\"><code><span class=\"o\">[</span>uwsgi<span class=\"o\">]</span>\n<span class=\"nv\">chdir</span><span class=\"o\">=</span>/path/to/your/project\n<span class=\"nv\">module</span><span class=\"o\">=</span>mysite.wsgi:application\n<span class=\"nv\">master</span><span class=\"o\">=</span>True\n<span class=\"nv\">pidfile</span><span class=\"o\">=</span>/tmp/project-master.pid\n<span class=\"nv\">vacuum</span><span class=\"o\">=</span>True\nmax-requests<span class=\"o\">=</span><span class=\"m\">5000</span>\n<span class=\"nv\">daemonize</span><span class=\"o\">=</span>/var/log/uwsgi/yourproject.log\n</code></pre></div>\n<p>Example ini configuration file usage:</p>\n<div class=\"code-block\" data-language=\"bash\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</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=\"Shell code\"><code>uwsgi --ini uwsgi.ini\n</code></pre></div>\n<aside class=\"admonition-fixing-unicodeencodeerror-for-file-uploads admonition\" role=\"note\">\n<p class=\"admonition-title\">Fixing <code class=\"docutils literal notranslate\">UnicodeEncodeError</code> for file uploads</p>\n<p>If you get a <code class=\"docutils literal notranslate\">UnicodeEncodeError</code> when uploading files with file names\nthat contain non-ASCII characters, make sure uWSGI is configured to accept\nnon-ASCII file names by adding this to your <code class=\"docutils literal notranslate\">uwsgi.ini</code>:</p>\n<div class=\"code-block\" data-language=\"bash\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</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=\"Shell code\"><code><span class=\"nv\">env</span> <span class=\"o\">=</span> <span class=\"nv\">LANG</span><span class=\"o\">=</span>en_US.UTF-8\n</code></pre></div>\n<p>See the <a class=\"reference internal\" href=\"/en/4.1/ref/unicode/#unicode-files\"><span class=\"std std-ref\">Files</span></a> section of the Unicode reference guide for\ndetails.</p>\n</aside>\n<p>See the uWSGI docs on <a class=\"reference external\" href=\"https://uwsgi-docs.readthedocs.io/en/latest/Management.html\">managing the uWSGI process</a> for information on\nstarting, stopping and reloading the uWSGI workers.</p>\n</section>\n</section>","rootId":"how-to-use-django-with-uwsgi","toc":[{"title":"Prerequisite: uWSGI","anchor":"prerequisite-uwsgi","children":[{"title":"uWSGI model","anchor":"uwsgi-model","children":[]},{"title":"Configuring and starting the uWSGI server for Django","anchor":"configuring-and-starting-the-uwsgi-server-for-django","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"“How-to” guides","url":"/en/4.1/howto/"},{"docname":"howto/deployment/index","title":"How to deploy Django","url":"/en/4.1/howto/deployment/"},{"docname":"howto/deployment/wsgi/index","title":"How to deploy with WSGI","url":"/en/4.1/howto/deployment/wsgi/"}],"prev":{"docname":"howto/deployment/wsgi/gunicorn","title":"How to use Django with Gunicorn","url":"/en/4.1/howto/deployment/wsgi/gunicorn/"},"next":{"docname":"howto/deployment/wsgi/modwsgi","title":"How to use Django with Apache and mod_wsgi","url":"/en/4.1/howto/deployment/wsgi/modwsgi/"},"formats":{"html":"/en/4.1/howto/deployment/wsgi/uwsgi/","markdown":"/en/4.1/howto/deployment/wsgi/uwsgi.md","json":"/en/4.1/howto/deployment/wsgi/uwsgi.json"},"source":"https://github.com/django/django/blob/stable/4.1.x/docs/howto/deployment/wsgi/uwsgi.txt","official":"https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/uwsgi/","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","it","pt-br","ko","es","el","pl"]}