Skip to content
Myr

fastcgi.rst

This file

Path

docs/deploying/fastcgi.rst

139 lines · 4.7 KiB

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 54d22f1 point at it.

Typo fix. That is what was meant, right?Simon Sapinbefore adoption54d22f1

lines 1–139why, one step back
1FastCGI
2=======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 different because by default77no FastCGI parameters are forwarded.7879A basic flask FastCGI configuration for nginx looks like this::8081 location = /yourapplication { rewrite ^ /yourapplication/ last; }82 location /yourapplication { try_files $uri @yourapplication; }83 location @yourapplication {84 include fastcgi_params;85 fastcgi_split_path_info ^(/yourapplication)(.*)$;86 fastcgi_param PATH_INFO $fastcgi_path_info;87 fastcgi_param SCRIPT_NAME $fastcgi_script_name;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 simpler because you don't have to figure93out how to calculate `PATH_INFO` and `SCRIPT_NAME`::9495 location / { try_files $uri @yourapplication; }96 location @yourapplication {97 include fastcgi_params;98 fastcgi_param PATH_INFO $fastcgi_script_name;99 fastcgi_param SCRIPT_NAME "";100 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;101 }102103Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You104can either write an `init.d` script for that or execute it inside a screen105session::106107 $ screen108 $ /var/www/yourapplication/yourapplication.fcgi109110Debugging111---------112113FastCGI deployments tend to be hard to debug on most webservers. Very often the114only thing the server log tells you is something along the lines of "premature115end of headers". In order to debug the application the only thing that can116really give you ideas why it breaks is switching to the correct user and117executing the application by hand.118119This example assumes your application is called `application.fcgi` and that your120webserver user is `www-data`::121122 $ su www-data123 $ cd /var/www/yourapplication124 $ python application.fcgi125 Traceback (most recent call last):126 File "yourapplication.fcg", line 4, in <module>127 ImportError: No module named yourapplication128129In this case the error seems to be "yourapplication" not being on the python130path. Common problems are:131132- relative paths being used. Don't rely on the current working directory133- the code depending on environment variables that are not set by the134 web server.135- different python interpreters being used.136137.. _lighttpd: http://www.lighttpd.net/138.. _nginx: http://nginx.net/139.. _flup: http://trac.saddi.com/flup