{"title":"How to use Django with FastCGI, SCGI, or AJP","version":"1.8","locale":"en","docname":"howto/deployment/fastcgi","url":"/en/1.8/howto/deployment/fastcgi/","canonical":"https://djangodocs.dev/en/1.8/howto/deployment/fastcgi/","summary":"Although WSGI is the preferred deployment platform for Django, many people use shared hosting, on which protocols such as FastCGI, SCGI or AJP are the only…","html":"<h1>How to use Django with FastCGI, SCGI, or AJP<a class=\"heading-anchor\" href=\"#how-to-use-django-with-fastcgi-scgi-or-ajp\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<aside class=\"version-note version-deprecated\" data-version=\"1.7\" role=\"note\">\n<p class=\"version-note-title\">Deprecated since Django 1.7</p><p><span class=\"versionmodified deprecated\">Deprecated since version 1.7: </span>FastCGI support is deprecated and will be removed in Django 1.9.</p>\n</aside>\n<p>Although <a class=\"reference internal\" href=\"/en/1.8/howto/deployment/wsgi/\"><span class=\"doc\">WSGI</span></a> is the preferred deployment\nplatform for Django, many people use shared hosting, on which protocols such as\nFastCGI, SCGI or AJP are the only viable options.</p>\n<aside class=\"admonition-note admonition\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>This document primarily focuses on FastCGI. Other protocols, such as SCGI\nand AJP, are also supported, through the <code class=\"docutils literal notranslate\">flup</code> Python package. See the\n<a class=\"reference internal\" href=\"#protocols\">Protocols</a> section below for specifics about SCGI and AJP.</p>\n</aside>\n<p>Essentially, FastCGI is an efficient way of letting an external application\nserve pages to a Web server. The Web server delegates the incoming Web requests\n(via a socket) to FastCGI, which executes the code and passes the response back\nto the Web server, which, in turn, passes it back to the client’s Web browser.</p>\n<p>Like WSGI, FastCGI allows code to stay in memory, allowing requests to be\nserved with no startup time. While\ne.g. <a class=\"reference internal\" href=\"/en/1.8/howto/deployment/wsgi/modwsgi/\"><span class=\"doc\">mod_wsgi</span></a> can either be configured\nembedded in the Apache Web server process or as a separate daemon process, a\nFastCGI process never runs inside the Web server process, always in a separate,\npersistent process.</p>\n<aside class=\"admonition-why-run-code-in-a-separate-process admonition\" role=\"note\">\n<p class=\"admonition-title\">Why run code in a separate process?</p>\n<p>The traditional <code class=\"docutils literal notranslate\">mod_*</code> arrangements in Apache embed various scripting\nlanguages (most notably PHP, Python and Perl) inside the process space of\nyour Web server. Although this lowers startup time – because code doesn’t\nhave to be read off disk for every request – it comes at the cost of\nmemory use.</p>\n<p>Due to the nature of FastCGI, it’s even possible to have processes that run\nunder a different user account than the Web server process. That’s a nice\nsecurity benefit on shared systems, because it means you can secure your\ncode from other users.</p>\n</aside>\n<section id=\"prerequisite-flup\">\n<h2>Prerequisite: flup<a class=\"heading-anchor\" href=\"#prerequisite-flup\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Before you can start using FastCGI with Django, you’ll need to install <a class=\"reference external\" href=\"http://www.saddi.com/software/flup/\">flup</a>, a\nPython library for dealing with FastCGI. Version 0.5 or newer should work fine.</p>\n</section>\n<section id=\"starting-your-fastcgi-server\">\n<h2>Starting your FastCGI server<a class=\"heading-anchor\" href=\"#starting-your-fastcgi-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>FastCGI operates on a client-server model, and in most cases you’ll be starting\nthe FastCGI process on your own. Your Web server (be it Apache, lighttpd, or\notherwise) only contacts your Django-FastCGI process when the server needs a\ndynamic page to be loaded. Because the daemon is already running with the code\nin memory, it’s able to serve the response very quickly.</p>\n<aside class=\"admonition-note admonition\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>If you’re on a shared hosting system, you’ll probably be forced to use\nWeb server-managed FastCGI processes. See the section below on running\nDjango with Web server-managed processes for more information.</p>\n</aside>\n<p>A Web server can connect to a FastCGI server in one of two ways: It can use\neither a Unix domain socket (a “named pipe” on Win32 systems), or it can use a\nTCP socket. What you choose is a manner of preference; a TCP socket is usually\neasier due to permissions issues.</p>\n<p>To start your server, first change into the directory of your project (wherever\nyour <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/\"><span class=\"doc\">manage.py</span></a> is), and then run the\n<a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-runfcgi\"><code class=\"xref std std-djadmin docutils literal notranslate\">runfcgi</code></a> command:</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>./manage.py runfcgi <span class=\"o\">[</span>options<span class=\"o\">]</span>\n</code></pre></div>\n<p>If you specify <code class=\"docutils literal notranslate\">help</code> as the only option after <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-runfcgi\"><code class=\"xref std std-djadmin docutils literal notranslate\">runfcgi</code></a>, it’ll\ndisplay a list of all the available options.</p>\n<p>You’ll need to specify either a <code class=\"docutils literal notranslate\">socket</code>, a <code class=\"docutils literal notranslate\">protocol</code>\nor both <code class=\"docutils literal notranslate\">host</code> and <code class=\"docutils literal notranslate\">port</code>. Then, when you set up your\nWeb server, you’ll just need to point it at the host/port or socket you\nspecified when starting the FastCGI server. See the <a class=\"reference internal\" href=\"#examples\">examples</a>, below.</p>\n<section id=\"protocols\">\n<h3>Protocols<a class=\"heading-anchor\" href=\"#protocols\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Django supports all the protocols that <a class=\"reference external\" href=\"http://www.saddi.com/software/flup/\">flup</a> does, namely <a class=\"reference external\" href=\"http://www.fastcgi.com/\">fastcgi</a>, <a class=\"reference external\" href=\"http://python.ca/scgi/protocol.txt\">SCGI</a> and\n<a class=\"reference external\" href=\"http://tomcat.apache.org/connectors-doc/ajp/ajpv13a.html\">AJP1.3</a> (the Apache JServ Protocol, version 1.3). Select your preferred\nprotocol by using the <code class=\"docutils literal notranslate\">protocol</code> option\nwith <code class=\"docutils literal notranslate\">./manage.py runfcgi</code> – where <code class=\"docutils literal notranslate\">&lt;protocol_name&gt;</code> may be one of:\n<code class=\"docutils literal notranslate\">fcgi</code> (the default), <code class=\"docutils literal notranslate\">scgi</code> or <code class=\"docutils literal notranslate\">ajp</code>. For example:</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>./manage.py runfcgi <span class=\"nv\">protocol</span><span class=\"o\">=</span>scgi\n</code></pre></div>\n</section>\n<section id=\"examples\">\n<h3>Examples<a class=\"heading-anchor\" href=\"#examples\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Running a threaded server on a TCP port:</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>./manage.py runfcgi <span class=\"nv\">method</span><span class=\"o\">=</span>threaded <span class=\"nv\">host</span><span class=\"o\">=</span><span class=\"m\">127</span>.0.0.1 <span class=\"nv\">port</span><span class=\"o\">=</span><span class=\"m\">3033</span>\n</code></pre></div>\n<p>Running a preforked server on a Unix domain socket:</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>./manage.py runfcgi <span class=\"nv\">method</span><span class=\"o\">=</span>prefork <span class=\"nv\">socket</span><span class=\"o\">=</span>/home/user/mysite.sock <span class=\"nv\">pidfile</span><span class=\"o\">=</span>django.pid\n</code></pre></div>\n<aside class=\"admonition-socket-security admonition\" role=\"note\">\n<p class=\"admonition-title\">Socket security</p>\n<p>Django’s default umask requires that the web server and the Django fastcgi\nprocess be run with the same group <strong>and</strong> user. For increased security,\nyou can run them under the same group but as different users. If you do\nthis, you will need to set the umask to 0002 using the <code class=\"docutils literal notranslate\">umask</code> argument\nto <code class=\"docutils literal notranslate\">runfcgi</code>.</p>\n</aside>\n<p>Run without daemonizing (backgrounding) the process (good for debugging):</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>./manage.py runfcgi <span class=\"nv\">daemonize</span><span class=\"o\">=</span><span class=\"nb\">false</span> <span class=\"nv\">socket</span><span class=\"o\">=</span>/tmp/mysite.sock <span class=\"nv\">maxrequests</span><span class=\"o\">=</span><span class=\"m\">1</span>\n</code></pre></div>\n</section>\n<section id=\"stopping-the-fastcgi-daemon\">\n<h3>Stopping the FastCGI daemon<a class=\"heading-anchor\" href=\"#stopping-the-fastcgi-daemon\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you have the process running in the foreground, it’s easy enough to stop it:\nSimply hitting <code class=\"docutils literal notranslate\">Ctrl-C</code> will stop and quit the FastCGI server. However, when\nyou’re dealing with background processes, you’ll need to resort to the Unix\n<code class=\"docutils literal notranslate\">kill</code> command.</p>\n<p>If you specify the <code class=\"docutils literal notranslate\">pidfile</code> option to <a class=\"reference internal\" href=\"/en/1.8/ref/django-admin/#django-admin-runfcgi\"><code class=\"xref std std-djadmin docutils literal notranslate\">runfcgi</code></a>, you can\nkill the running FastCGI daemon like this:</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=\"nb\">kill</span> <span class=\"sb\">`</span>cat <span class=\"nv\">$PIDFILE</span><span class=\"sb\">`</span>\n</code></pre></div>\n<p>…where <code class=\"docutils literal notranslate\">$PIDFILE</code> is the <code class=\"docutils literal notranslate\">pidfile</code> you specified.</p>\n<p>To easily restart your FastCGI daemon on Unix, try this small shell script:</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=\"ch\">#!/bin/bash</span>\n\n<span class=\"c1\"># Replace these three settings.</span>\n<span class=\"nv\">PROJDIR</span><span class=\"o\">=</span><span class=\"s2\">&quot;/home/user/myproject&quot;</span>\n<span class=\"nv\">PIDFILE</span><span class=\"o\">=</span><span class=\"s2\">&quot;</span><span class=\"nv\">$PROJDIR</span><span class=\"s2\">/mysite.pid&quot;</span>\n<span class=\"nv\">SOCKET</span><span class=\"o\">=</span><span class=\"s2\">&quot;</span><span class=\"nv\">$PROJDIR</span><span class=\"s2\">/mysite.sock&quot;</span>\n\n<span class=\"nb\">cd</span> <span class=\"nv\">$PROJDIR</span>\n<span class=\"k\">if</span> <span class=\"o\">[</span> -f <span class=\"nv\">$PIDFILE</span> <span class=\"o\">]</span><span class=\"p\">;</span> <span class=\"k\">then</span>\n    <span class=\"nb\">kill</span> <span class=\"sb\">`</span>cat -- <span class=\"nv\">$PIDFILE</span><span class=\"sb\">`</span>\n    rm -f -- <span class=\"nv\">$PIDFILE</span>\n<span class=\"k\">fi</span>\n\n<span class=\"nb\">exec</span> /usr/bin/env - <span class=\"se\">\\</span>\n  <span class=\"nv\">PYTHONPATH</span><span class=\"o\">=</span><span class=\"s2\">&quot;../python:..&quot;</span> <span class=\"se\">\\</span>\n  ./manage.py runfcgi <span class=\"nv\">socket</span><span class=\"o\">=</span><span class=\"nv\">$SOCKET</span> <span class=\"nv\">pidfile</span><span class=\"o\">=</span><span class=\"nv\">$PIDFILE</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"apache-setup\">\n<h2>Apache setup<a class=\"heading-anchor\" href=\"#apache-setup\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>To use Django with Apache and FastCGI, you’ll need Apache installed and\nconfigured, with <a class=\"reference external\" href=\"http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html\">mod_fastcgi</a> installed and enabled. Consult the Apache\ndocumentation for instructions.</p>\n<p>Once you’ve got that set up, point Apache at your Django FastCGI instance by\nediting the <code class=\"docutils literal notranslate\">httpd.conf</code> (Apache configuration) file. You’ll need to do two\nthings:</p>\n<ul class=\"simple\">\n<li><p>Use the <code class=\"docutils literal notranslate\">FastCGIExternalServer</code> directive to specify the location of\nyour FastCGI server.</p></li>\n<li><p>Use <code class=\"docutils literal notranslate\">mod_rewrite</code> to point URLs at FastCGI as appropriate.</p></li>\n</ul>\n<section id=\"specifying-the-location-of-the-fastcgi-server\">\n<h3>Specifying the location of the FastCGI server<a class=\"heading-anchor\" href=\"#specifying-the-location-of-the-fastcgi-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The <code class=\"docutils literal notranslate\">FastCGIExternalServer</code> directive tells Apache how to find your FastCGI\nserver. As the <a class=\"reference external\" href=\"http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiExternalServer\">FastCGIExternalServer docs</a> explain, you can specify either a\n<code class=\"docutils literal notranslate\">socket</code> or a <code class=\"docutils literal notranslate\">host</code>. Here are examples of both:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"c\"># Connect to FastCGI via a socket / named pipe.</span>\n<span class=\"nb\">FastCGIExternalServer</span> <span class=\"sx\">/home/user/public_html/mysite.fcgi</span> -socket <span class=\"sx\">/home/user/mysite.sock</span>\n\n<span class=\"c\"># Connect to FastCGI via a TCP host/port.</span>\n<span class=\"nb\">FastCGIExternalServer</span> <span class=\"sx\">/home/user/public_html/mysite.fcgi</span> -host <span class=\"m\">127.0.0.1</span>:3033\n</code></pre></div>\n<p>In either case, the file <code class=\"docutils literal notranslate\">/home/user/public_html/mysite.fcgi</code> doesn’t\nactually have to exist. It’s just a URL used by the Web server internally – a\nhook for signifying which requests at a URL should be handled by FastCGI. (More\non this in the next section.)</p>\n</section>\n<section id=\"using-mod-rewrite-to-point-urls-at-fastcgi\">\n<h3>Using mod_rewrite to point URLs at FastCGI<a class=\"heading-anchor\" href=\"#using-mod-rewrite-to-point-urls-at-fastcgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The second step is telling Apache to use FastCGI for URLs that match a certain\npattern. To do this, use the <a class=\"reference external\" href=\"http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html\">mod_rewrite</a> module and rewrite URLs to\n<code class=\"docutils literal notranslate\">mysite.fcgi</code> (or whatever you specified in the <code class=\"docutils literal notranslate\">FastCGIExternalServer</code>\ndirective, as explained in the previous section).</p>\n<p>In this example, we tell Apache to use FastCGI to handle any request that\ndoesn’t represent a file on the filesystem and doesn’t start with <code class=\"docutils literal notranslate\">/media/</code>.\nThis is probably the most common case, if you’re using Django’s admin site:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nt\">&lt;VirtualHost</span> <span class=\"s\">12.34.56.78</span><span class=\"nt\">&gt;</span>\n  <span class=\"nb\">ServerName</span> example.com\n  <span class=\"nb\">DocumentRoot</span> <span class=\"sx\">/home/user/public_html</span>\n  <span class=\"nb\">Alias</span> <span class=\"sx\">/media</span> <span class=\"sx\">/home/user/python/django/contrib/admin/media</span>\n  <span class=\"nb\">RewriteEngine</span> <span class=\"k\">On</span>\n  <span class=\"nb\">RewriteRule</span> ^/(media.*)$ /$1 [QSA,L,PT]\n  <span class=\"nb\">RewriteCond</span> %{REQUEST_FILENAME} !-f\n  <span class=\"nb\">RewriteRule</span> ^/(.*)$ <span class=\"sx\">/mysite.fcgi/</span>$1 [QSA,L]\n<span class=\"nt\">&lt;/VirtualHost&gt;</span>\n</code></pre></div>\n<p>Django will automatically use the pre-rewrite version of the URL when\nconstructing URLs with the <a class=\"reference internal\" href=\"/en/1.8/ref/templates/builtins/#std-templatetag-url\"><code class=\"xref std std-ttag docutils literal notranslate\">{% url %}</code></a> template tag (and similar\nmethods).</p>\n</section>\n<section id=\"using-mod-fcgid-as-alternative-to-mod-fastcgi\">\n<h3>Using mod_fcgid as alternative to mod_fastcgi<a class=\"heading-anchor\" href=\"#using-mod-fcgid-as-alternative-to-mod-fastcgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Another way to serve applications through FastCGI is by using Apache’s\n<a class=\"reference external\" href=\"http://httpd.apache.org/mod_fcgid/\">mod_fcgid</a> module. Compared to mod_fastcgi mod_fcgid handles FastCGI\napplications differently in that it manages the spawning of worker processes\nby itself and doesn’t offer something like <code class=\"docutils literal notranslate\">FastCGIExternalServer</code>. This\nmeans that the configuration looks slightly different.</p>\n<p>In effect, you have to go the way of adding a script handler similar to what\nis described later on regarding running Django in a <a class=\"reference internal\" href=\"#apache-shared-hosting\"><span class=\"std std-ref\">shared-hosting\nenvironment</span></a>. For further details please refer to the\n<a class=\"reference external\" href=\"http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html\">mod_fcgid reference</a></p>\n</section>\n</section>\n<section id=\"lighttpd-setup\">\n<h2>lighttpd setup<a class=\"heading-anchor\" href=\"#lighttpd-setup\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><a class=\"reference external\" href=\"http://www.lighttpd.net/\">lighttpd</a> is a lightweight Web server commonly used for serving static files. It\nsupports FastCGI natively and, thus, is a good choice for serving both static\nand dynamic pages, if your site doesn’t have any Apache-specific needs.</p>\n<p>Make sure <code class=\"docutils literal notranslate\">mod_fastcgi</code> is in your modules list, somewhere after\n<code class=\"docutils literal notranslate\">mod_rewrite</code> and <code class=\"docutils literal notranslate\">mod_access</code>, but not after <code class=\"docutils literal notranslate\">mod_accesslog</code>. You’ll\nprobably want <code class=\"docutils literal notranslate\">mod_alias</code> as well, for serving admin media.</p>\n<p>Add the following to your lighttpd config file:</p>\n<div class=\"code-block\" data-language=\"lua\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Lua</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=\"Lua code\"><code><span class=\"nv\">server</span><span class=\"p\">.</span><span class=\"py\">document</span><span class=\"o\">-</span><span class=\"nv\">root</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;/home/user/public_html&quot;</span>\n<span class=\"nv\">fastcgi</span><span class=\"p\">.</span><span class=\"py\">server</span> <span class=\"o\">=</span> <span class=\"p\">(</span>\n    <span class=\"s2\">&quot;/mysite.fcgi&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"p\">(</span>\n        <span class=\"s2\">&quot;main&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"p\">(</span>\n            <span class=\"o\">#</span> <span class=\"nv\">Use</span> <span class=\"nv\">host</span> <span class=\"o\">/</span> <span class=\"nv\">port</span> <span class=\"nv\">instead</span> <span class=\"nv\">of</span> <span class=\"nv\">socket</span> <span class=\"kr\">for</span> <span class=\"nv\">TCP</span> <span class=\"nv\">fastcgi</span>\n            <span class=\"o\">#</span> <span class=\"s2\">&quot;host&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"s2\">&quot;127.0.0.1&quot;</span><span class=\"p\">,</span>\n            <span class=\"o\">#</span> <span class=\"s2\">&quot;port&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"mi\">3033</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;socket&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"s2\">&quot;/home/user/mysite.sock&quot;</span><span class=\"p\">,</span>\n            <span class=\"s2\">&quot;check-local&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"s2\">&quot;disable&quot;</span><span class=\"p\">,</span>\n        <span class=\"p\">)</span>\n    <span class=\"p\">),</span>\n<span class=\"p\">)</span>\n<span class=\"nv\">alias</span><span class=\"p\">.</span><span class=\"py\">url</span> <span class=\"o\">=</span> <span class=\"p\">(</span>\n    <span class=\"s2\">&quot;/media&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"s2\">&quot;/home/user/django/contrib/admin/media/&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n\n<span class=\"nv\">url</span><span class=\"p\">.</span><span class=\"py\">rewrite</span><span class=\"o\">-</span><span class=\"nv\">once</span> <span class=\"o\">=</span> <span class=\"p\">(</span>\n    <span class=\"s2\">&quot;^(/media.*)$&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"s2\">&quot;$1&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;^/favicon</span><span class=\"err\">\\</span><span class=\"s2\">.ico$&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"s2\">&quot;/media/favicon.ico&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;^(/.*)$&quot;</span> <span class=\"o\">=&gt;</span> <span class=\"s2\">&quot;/mysite.fcgi$1&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">)</span>\n</code></pre></div>\n<section id=\"running-multiple-django-sites-on-one-lighttpd\">\n<h3>Running multiple Django sites on one lighttpd<a class=\"heading-anchor\" href=\"#running-multiple-django-sites-on-one-lighttpd\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>lighttpd lets you use “conditional configuration” to allow configuration to be\ncustomized per host. To specify multiple FastCGI sites, just add a conditional\nblock around your FastCGI config for each site:</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=\"c1\"># If the hostname is &#39;www.example1.com&#39;...</span>\n<span class=\"nv\">$HTTP</span><span class=\"o\">[</span><span class=\"s2\">&quot;host&quot;</span><span class=\"o\">]</span> <span class=\"o\">==</span> <span class=\"s2\">&quot;www.example1.com&quot;</span> <span class=\"o\">{</span>\n    server.document-root <span class=\"o\">=</span> <span class=\"s2\">&quot;/foo/site1&quot;</span>\n    fastcgi.server <span class=\"o\">=</span> <span class=\"o\">(</span>\n       ...\n    <span class=\"o\">)</span>\n    ...\n<span class=\"o\">}</span>\n\n<span class=\"c1\"># If the hostname is &#39;www.example2.com&#39;...</span>\n<span class=\"nv\">$HTTP</span><span class=\"o\">[</span><span class=\"s2\">&quot;host&quot;</span><span class=\"o\">]</span> <span class=\"o\">==</span> <span class=\"s2\">&quot;www.example2.com&quot;</span> <span class=\"o\">{</span>\n    server.document-root <span class=\"o\">=</span> <span class=\"s2\">&quot;/foo/site2&quot;</span>\n    fastcgi.server <span class=\"o\">=</span> <span class=\"o\">(</span>\n       ...\n    <span class=\"o\">)</span>\n    ...\n<span class=\"o\">}</span>\n</code></pre></div>\n<p>You can also run multiple Django installations on the same site simply by\nspecifying multiple entries in the <code class=\"docutils literal notranslate\">fastcgi.server</code> directive. Add one\nFastCGI host for each.</p>\n</section>\n</section>\n<section id=\"cherokee-setup\">\n<h2>Cherokee setup<a class=\"heading-anchor\" href=\"#cherokee-setup\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Cherokee is a very fast, flexible and easy to configure Web Server. It\nsupports the widespread technologies nowadays: FastCGI, SCGI, PHP, CGI, SSI,\nTLS and SSL encrypted connections, Virtual hosts, Authentication, on the fly\nencoding, Load Balancing, Apache compatible log files, Data Base Balancer,\nReverse HTTP Proxy and much more.</p>\n<p>The Cherokee project provides a documentation to <a class=\"reference external\" href=\"http://www.cherokee-project.com/doc/cookbook_django.html\">setting up Django</a> with Cherokee.</p>\n</section>\n<section id=\"running-django-on-a-shared-hosting-provider-with-apache\">\n<span id=\"apache-shared-hosting\"></span><h2>Running Django on a shared-hosting provider with Apache<a class=\"heading-anchor\" href=\"#running-django-on-a-shared-hosting-provider-with-apache\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Many shared-hosting providers don’t allow you to run your own server daemons or\nedit the <code class=\"docutils literal notranslate\">httpd.conf</code> file. In these cases, it’s still possible to run Django\nusing Web server-spawned processes.</p>\n<aside class=\"admonition-note admonition\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>If you’re using Web server-spawned processes, as explained in this section,\nthere’s no need for you to start the FastCGI server on your own. Apache\nwill spawn a number of processes, scaling as it needs to.</p>\n</aside>\n<p>In your Web root directory, add this to a file named <code class=\"docutils literal notranslate\">.htaccess</code>:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nb\">AddHandler</span> fastcgi-script .fcgi\n<span class=\"nb\">RewriteEngine</span> <span class=\"k\">On</span>\n<span class=\"nb\">RewriteCond</span> %{REQUEST_FILENAME} !-f\n<span class=\"nb\">RewriteRule</span> ^(.*)$ mysite.fcgi/$1 [QSA,L]\n</code></pre></div>\n<p>Then, create a small script that tells Apache how to spawn your FastCGI\nprogram. Create a file <code class=\"docutils literal notranslate\">mysite.fcgi</code> and place it in your Web directory, and\nbe sure to make it executable:</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=\"ch\">#!/usr/bin/python</span>\nimport sys, os\n\n<span class=\"c1\"># Add a custom Python path.</span>\nsys.path.insert<span class=\"o\">(</span><span class=\"m\">0</span>, <span class=\"s2\">&quot;/home/user/python&quot;</span><span class=\"o\">)</span>\n\n<span class=\"c1\"># Switch to the directory of your project. (Optional.)</span>\n<span class=\"c1\"># os.chdir(&quot;/home/user/myproject&quot;)</span>\n\n<span class=\"c1\"># Set the DJANGO_SETTINGS_MODULE environment variable.</span>\nos.environ<span class=\"o\">[</span><span class=\"s1\">&#39;DJANGO_SETTINGS_MODULE&#39;</span><span class=\"o\">]</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;myproject.settings&quot;</span>\n\nfrom django.core.servers.fastcgi import runfastcgi\nrunfastcgi<span class=\"o\">(</span><span class=\"nv\">method</span><span class=\"o\">=</span><span class=\"s2\">&quot;threaded&quot;</span>, <span class=\"nv\">daemonize</span><span class=\"o\">=</span><span class=\"s2\">&quot;false&quot;</span><span class=\"o\">)</span>\n</code></pre></div>\n<p>This works if your server uses mod_fastcgi. If, on the other hand, you are\nusing mod_fcgid the setup is mostly the same except for a slight change in the\n<code class=\"docutils literal notranslate\">.htaccess</code> file. Instead of adding a fastcgi-script handler, you have to\nadd a fcgid-handler:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</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=\"Apache code\"><code><span class=\"nb\">AddHandler</span> fcgid-script .fcgi\n<span class=\"nb\">RewriteEngine</span> <span class=\"k\">On</span>\n<span class=\"nb\">RewriteCond</span> %{REQUEST_FILENAME} !-f\n<span class=\"nb\">RewriteRule</span> ^(.*)$ mysite.fcgi/$1 [QSA,L]\n</code></pre></div>\n<section id=\"restarting-the-spawned-server\">\n<h3>Restarting the spawned server<a class=\"heading-anchor\" href=\"#restarting-the-spawned-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you change any Python code on your site, you’ll need to tell FastCGI the\ncode has changed. But there’s no need to restart Apache in this case. Rather,\njust reupload <code class=\"docutils literal notranslate\">mysite.fcgi</code>, or edit the file, so that the timestamp on the\nfile will change. When Apache sees the file has been updated, it will restart\nyour Django application for you.</p>\n<p>If you have access to a command shell on a Unix system, you can accomplish this\neasily by using the <code class=\"docutils literal notranslate\">touch</code> command:</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>touch mysite.fcgi\n</code></pre></div>\n</section>\n</section>\n<section id=\"serving-admin-media-files\">\n<h2>Serving admin media files<a class=\"heading-anchor\" href=\"#serving-admin-media-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Regardless of the server and configuration you eventually decide to use, you\nwill also need to give some thought to how to serve the admin media files. The\nadvice given in the <a class=\"reference internal\" href=\"/en/1.8/howto/deployment/wsgi/modwsgi/#serving-the-admin-files\"><span class=\"std std-ref\">mod_wsgi</span></a> documentation\nis also applicable in the setups detailed above.</p>\n</section>\n<section id=\"forcing-the-url-prefix-to-a-particular-value\">\n<h2>Forcing the URL prefix to a particular value<a class=\"heading-anchor\" href=\"#forcing-the-url-prefix-to-a-particular-value\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Because many of these fastcgi-based solutions require rewriting the URL at\nsome point inside the Web server, the path information that Django sees may not\nresemble the original URL that was passed in. This is a problem if the Django\napplication is being served from under a particular prefix and you want your\nURLs from the <a class=\"reference internal\" href=\"/en/1.8/ref/templates/builtins/#std-templatetag-url\"><code class=\"xref std std-ttag docutils literal notranslate\">{% url %}</code></a> tag to look like the prefix, rather than\nthe rewritten version, which might contain, for example, <code class=\"docutils literal notranslate\">mysite.fcgi</code>.</p>\n<p>Django makes a good attempt to work out what the real script name prefix\nshould be. In particular, if the Web server sets the <code class=\"docutils literal notranslate\">SCRIPT_URL</code> (specific\nto Apache’s mod_rewrite), or <code class=\"docutils literal notranslate\">REDIRECT_URL</code> (set by a few servers, including\nApache + mod_rewrite in some situations), Django will work out the original\nprefix automatically.</p>\n<p>In the cases where Django cannot work out the prefix correctly and where you\nwant the original value to be used in URLs, you can set the\n<a class=\"reference internal\" href=\"/en/1.8/ref/settings/#std-setting-FORCE_SCRIPT_NAME\"><code class=\"xref std std-setting docutils literal notranslate\">FORCE_SCRIPT_NAME</code></a> setting in your main <code class=\"docutils literal notranslate\">settings</code> file. This sets the\nscript name uniformly for every URL served via that settings file. Thus you’ll\nneed to use different settings files if you want different sets of URLs to\nhave different script names in this case, but that is a rare situation.</p>\n<p>As an example of how to use it, if your Django configuration is serving all of\nthe URLs under <code class=\"docutils literal notranslate\">'/'</code> and you wanted to use this setting, you would set\n<code class=\"docutils literal notranslate\">FORCE_SCRIPT_NAME = ''</code> in your settings file.</p>\n</section>","rootId":"how-to-use-django-with-fastcgi-scgi-or-ajp","toc":[{"title":"Prerequisite: flup","anchor":"prerequisite-flup","children":[]},{"title":"Starting your FastCGI server","anchor":"starting-your-fastcgi-server","children":[{"title":"Protocols","anchor":"protocols","children":[]},{"title":"Examples","anchor":"examples","children":[]},{"title":"Stopping the FastCGI daemon","anchor":"stopping-the-fastcgi-daemon","children":[]}]},{"title":"Apache setup","anchor":"apache-setup","children":[{"title":"Specifying the location of the FastCGI server","anchor":"specifying-the-location-of-the-fastcgi-server","children":[]},{"title":"Using mod_rewrite to point URLs at FastCGI","anchor":"using-mod-rewrite-to-point-urls-at-fastcgi","children":[]},{"title":"Using mod_fcgid as alternative to mod_fastcgi","anchor":"using-mod-fcgid-as-alternative-to-mod-fastcgi","children":[]}]},{"title":"lighttpd setup","anchor":"lighttpd-setup","children":[{"title":"Running multiple Django sites on one lighttpd","anchor":"running-multiple-django-sites-on-one-lighttpd","children":[]}]},{"title":"Cherokee setup","anchor":"cherokee-setup","children":[]},{"title":"Running Django on a shared-hosting provider with Apache","anchor":"running-django-on-a-shared-hosting-provider-with-apache","children":[{"title":"Restarting the spawned server","anchor":"restarting-the-spawned-server","children":[]}]},{"title":"Serving admin media files","anchor":"serving-admin-media-files","children":[]},{"title":"Forcing the URL prefix to a particular value","anchor":"forcing-the-url-prefix-to-a-particular-value","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"“How-to” guides","url":"/en/1.8/howto/"},{"docname":"howto/deployment/index","title":"Deploying Django","url":"/en/1.8/howto/deployment/"}],"prev":{"docname":"howto/deployment/checklist","title":"Deployment checklist","url":"/en/1.8/howto/deployment/checklist/"},"next":{"docname":"howto/upgrade-version","title":"Upgrading Django to a newer version","url":"/en/1.8/howto/upgrade-version/"},"formats":{"html":"/en/1.8/howto/deployment/fastcgi/","markdown":"/en/1.8/howto/deployment/fastcgi.md","json":"/en/1.8/howto/deployment/fastcgi.json"},"source":"https://github.com/django/django/blob/stable/1.8.x/docs/howto/deployment/fastcgi.txt","official":"https://docs.djangoproject.com/en/1.8/howto/deployment/fastcgi/","inVersions":["1.8"],"inLocales":["en"]}