<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Werkzeug Documentation</title> <link rel="stylesheet" href="../_static/style.css" type="text/css"> <link rel="stylesheet" href="../_static/print.css" type="text/css" media="print"> <link rel="stylesheet" href="../_static/pygments.css" type="text/css"> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '0.6.1' }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/interface.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/werkzeug.js"></script> <link rel="contents" title="Global table of contents" href="../contents.html"> <link rel="index" title="Global index" href="../genindex.html"> <link rel="search" title="Search" href="../search.html"> <link rel="top" title="Werkzeug v0.6.1 documentation" href="../index.html"> <link rel="up" title="Application Deployment" href="index.html"> <link rel="next" title="HTTP Proxying" href="proxying.html"> <link rel="prev" title="mod_wsgi (Apache)" href="mod_wsgi.html"> </head> <body> <div class="page"> <div class="header"> <h1 class="heading"><a href="../index.html" title="back to the documentation overview"><span>Werkzeug</span></a></h1> </div> <ul class="navigation"> <li class="indexlink"><a href="../index.html">Overview</a></li> <li><a href="mod_wsgi.html">« <cite>mod_wsgi</cite> (Apache)</a></li> <li class="active"><a href="#">FastCGI</a></li> <li><a href="proxying.html">HTTP Proxying »</a></li> </ul> <div class="body"> <div id="toc"> <h3>Table Of Contents</h3> <div class="inner"><ul> <li><a class="reference external" href="#">FastCGI</a><ul> <li><a class="reference external" href="#creating-a-fcgi-file">Creating a <cite>.fcgi</cite> file</a></li> <li><a class="reference external" href="#configuring-lighttpd">Configuring lighttpd</a></li> <li><a class="reference external" href="#configuring-nginx">Configuring nginx</a></li> <li><a class="reference external" href="#debugging">Debugging</a></li> </ul> </li> </ul> </div> </div> <div class="section" id="fastcgi"> <h1>FastCGI<a class="headerlink" href="#fastcgi" title="Permalink to this headline">¶</a></h1> <p>A very popular deployment setup on servers like <a class="reference external" href="http://www.lighttpd.net/">lighttpd</a> and <a class="reference external" href="http://nginx.net/">nginx</a> is FastCGI. To use your WSGI application with any of them you will need a FastCGI server first.</p> <p>The most popular one is <a class="reference external" href="http://trac.saddi.com/flup">flup</a> which we will use for this guide. Make sure to have it installed.</p> <div class="section" id="creating-a-fcgi-file"> <h2>Creating a <cite>.fcgi</cite> file<a class="headerlink" href="#creating-a-fcgi-file" title="Permalink to this headline">¶</a></h2> <p>First you need to create the FastCGI server file. Let’s call it <cite>yourapplication.fcgi</cite>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c">#!/usr/bin/python</span> <span class="kn">from</span> <span class="nn">flup.server.fcgi</span> <span class="kn">import</span> <span class="n">WSGIServer</span> <span class="kn">from</span> <span class="nn">yourapplication</span> <span class="kn">import</span> <span class="n">make_app</span> <span class="n">application</span> <span class="o">=</span> <span class="n">make_app</span><span class="p">()</span> <span class="n">WSGIServer</span><span class="p">(</span><span class="n">application</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">()</span> </pre></div> </div> <p>This is enough for Apache to work, however lighttpd and nginx need a socket to communicate with the FastCGI server. For that to work you need to pass the path to the socket to the <tt class="xref py py-class docutils literal"><span class="pre">WSGIServer</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">WSGIServer</span><span class="p">(</span><span class="n">application</span><span class="p">,</span> <span class="n">bindAddress</span><span class="o">=</span><span class="s">'/path/to/fcgi.sock'</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">()</span> </pre></div> </div> <p>The path has to be the exact same path you define in the server config.</p> <p>Save the <cite>yourapplication.fcgi</cite> file somewhere you will find it again. It makes sense to have that in <cite>/var/www/yourapplication</cite> or something similar.</p> <p>Make sure to set the executable bit on that file so that the servers can execute it:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># chmod +x /var/www/yourapplication/yourapplication.fcgi</span> </pre></div> </div> </div> <div class="section" id="configuring-lighttpd"> <h2>Configuring lighttpd<a class="headerlink" href="#configuring-lighttpd" title="Permalink to this headline">¶</a></h2> <p>A basic FastCGI configuration for lighttpd looks like that:</p> <div class="highlight-python"><pre>fastcgi.server = ("/yourapplication" => "yourapplication" => ( "socket" => "/tmp/yourapplication-fcgi.sock", "bin-path" => "/var/www/yourapplication/yourapplication.fcgi", "check-local" => "disable" ) )</pre> </div> <p>This configuration binds the application to <cite>/yourapplication</cite>. If you want the application to work in the URL root you have to work around a lighttpd bug with the <a title="werkzeug.contrib.fixers.LighttpdCGIRootFix" class="reference external" href="../contrib/fixers.html#werkzeug.contrib.fixers.LighttpdCGIRootFix"><tt class="xref py py-class docutils literal"><span class="pre">LighttpdCGIRootFix</span></tt></a> middleware.</p> <p>Make sure to apply it only if you are mounting the application the URL root.</p> </div> <div class="section" id="configuring-nginx"> <h2>Configuring nginx<a class="headerlink" href="#configuring-nginx" title="Permalink to this headline">¶</a></h2> <p>Installing FastCGI applications on nginx is a bit tricky because by default some FastCGI parameters are not properly forwarded.</p> <p>A basic FastCGI configuration for nginx looks like this:</p> <div class="highlight-python"><pre>location /yourapplication/ { include fastcgi_params; if ($uri ~ ^/yourapplication/(.*)?) { set $path_url $1; } fastcgi_param PATH_INFO $path_url; fastcgi_param SCRIPT_NAME /yourapplication; fastcgi_pass unix:/tmp/yourapplication-fcgi.sock; }</pre> </div> <p>This configuration binds the application to <cite>/yourapplication</cite>. If you want to have it in the URL root it’s a bit easier because you don’t have to figure out how to calculate <cite>PATH_INFO</cite> and <cite>SCRIPT_NAME</cite>:</p> <div class="highlight-python"><pre>location /yourapplication/ { include fastcgi_params; fastcgi_param PATH_INFO $fastcgi_script_name; fastcgi_param SCRIPT_NAME ""; fastcgi_pass unix:/tmp/yourapplication-fcgi.sock; }</pre> </div> <p>Since Nginx doesn’t load FastCGI apps, you have to do it by yourself. You can either write an <cite>init.d</cite> script for that or execute it inside a screen session:</p> <div class="highlight-python"><pre>$ screen $ /var/www/yourapplication/yourapplication.fcgi</pre> </div> </div> <div class="section" id="debugging"> <h2>Debugging<a class="headerlink" href="#debugging" title="Permalink to this headline">¶</a></h2> <p>FastCGI deployments tend to be hard to debug on most webservers. Very often the only thing the server log tells you is something along the lines of “premature end of headers”. In order to debug the application the only thing that can really give you ideas why it breaks is switching to the correct user and executing the application by hand.</p> <p>This example assumes your application is called <cite>application.fcgi</cite> and that your webserver user is <cite>www-data</cite>:</p> <div class="highlight-python"><pre>$ su www-data $ cd /var/www/yourapplication $ python application.fcgi Traceback (most recent call last): File "yourapplication.fcg", line 4, in <module> ImportError: No module named yourapplication</pre> </div> <p>In this case the error seems to be “yourapplication” not being on the python path. Common problems are:</p> <ul class="simple"> <li>relative paths being used. Don’t rely on the current working directory</li> <li>the code depending on environment variables that are not set by the web server.</li> <li>different python interpreters being used.</li> </ul> </div> </div> <div style="clear: both"></div> </div> <div class="footer"> © Copyright 2008 by the <a href="http://pocoo.org/">Pocoo Team</a>, documentation generated by <a href="http://sphinx.pocoo.org/">Sphinx</a> </div> </div> </body> </html>