Skip to content
Myr

fastcgi.rst

This file

Path

docs/deploying/fastcgi.rst

138 lines · 4.5 KiB

Last changed by

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

History of this file

Take it

1FastCGI2=======34A very popular deployment setup on servers like `lighttpd`_ and `nginx`_5is FastCGI.  To use your WSGI application with any of them you will need6a FastCGI server first.78The most popular one is `flup`_ which we will use for this guide.  Make9sure to have it installed.1011.. admonition:: Watch Out1213   Please make sure in advance that your ``app.run()`` call you might14   have in your application file, is inside an ``if __name__ ==15   '__main__':`` or moved to a separate file.  Just make sure it's not16   called because this will always start a local WSGI server which we do17   not want if we deploy that application to FastCGI.1819Creating a `.fcgi` file20-----------------------2122First you need to create the FastCGI server file.  Let's call it23`yourapplication.fcgi`::2425    #!/usr/bin/python26    from flup.server.fcgi import WSGIServer27    from yourapplication import app2829    WSGIServer(app).run()3031This is enough for Apache to work, however lighttpd and nginx need a32socket to communicate with the FastCGI server.  For that to work you33need to pass the path to the socket to the34:class:`~flup.server.fcgi.WSGIServer`::3536    WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()3738The path has to be the exact same path you define in the server39config.4041Save the `yourapplication.fcgi` file somewhere you will find it again.42It makes sense to have that in `/var/www/yourapplication` or something43similar.4445Make sure to set the executable bit on that file so that the servers46can execute it:4748.. sourcecode:: text4950    # chmod +x /var/www/yourapplication/yourapplication.fcgi5152Configuring lighttpd53--------------------5455A basic FastCGI configuration for lighttpd looks like that::5657    fastcgi.server = ("/yourapplication" =>58        "yourapplication" => (59            "socket" => "/tmp/yourapplication-fcgi.sock",60            "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",61            "check-local" => "disable"62        )63    )6465This configuration binds the application to `/yourapplication`.  If you66want the application to work in the URL root you have to work around a67lighttpd bug with the :class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix`68middleware.6970Make sure to apply it only if you are mounting the application the URL71root.7273Configuring nginx74-----------------7576Installing FastCGI applications on nginx is a bit tricky because by default77some FastCGI parameters are not properly forwarded.7879A basic FastCGI configuration for nginx looks like this::8081    location /yourapplication/ {82        include fastcgi_params;83        if ($uri ~ ^/yourapplication/(.*)?) {84            set $path_url $1;85        }86        fastcgi_param PATH_INFO $path_url;87        fastcgi_param SCRIPT_NAME /yourapplication;88        fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;89    }9091This configuration binds the application to `/yourapplication`.  If you want92to have it in the URL root it's a bit easier because you don't have to figure93out how to calculate `PATH_INFO` and `SCRIPT_NAME`::9495    location /yourapplication/ {96        include fastcgi_params;97        fastcgi_param PATH_INFO $fastcgi_script_name;98        fastcgi_param SCRIPT_NAME "";99        fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;100    }101102Since Nginx doesn't load FastCGI apps, you have to do it by yourself.  You103can either write an `init.d` script for that or execute it inside a screen104session::105106    $ screen107    $ /var/www/yourapplication/yourapplication.fcgi108109Debugging110---------111112FastCGI deployments tend to be hard to debug on most webservers.  Very often the113only thing the server log tells you is something along the lines of "premature114end of headers".  In order to debug the application the only thing that can115really give you ideas why it breaks is switching to the correct user and116executing the application by hand.117118This example assumes your application is called `application.fcgi` and that your119webserver user is `www-data`::120121    $ su www-data122    $ cd /var/www/yourapplication123    $ python application.fcgi124    Traceback (most recent call last):125      File "yourapplication.fcg", line 4, in <module>126    ImportError: No module named yourapplication127128In this case the error seems to be "yourapplication" not being on the python129path.  Common problems are:130131-   relative paths being used.  Don't rely on the current working directory132-   the code depending on environment variables that are not set by the133    web server.134-   different python interpreters being used.135136.. _lighttpd: http://www.lighttpd.net/137.. _nginx: http://nginx.net/138.. _flup: http://trac.saddi.com/flup