Skip to content
Myr

fastcgi.rst

This file

Path

docs/deploying/fastcgi.rst

163 lines · 5.8 KiB

Last changed by

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb

History of this file

Take it

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

Flask will now give you an error in debug mode if a post request caused a redirect by the routing system.Armin Ronacherbefore adoption6847329

lines 1–5why, one step back
1.. _deploying-fastcgi:
23FastCGI4=======5

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb

lines 6–10why, one step back
6FastCGI is a deployment option on servers like `nginx`_, `lighttpd`_, and
7`cherokee`_; see :ref:`deploying-uwsgi` and :ref:`deploying-wsgi-standalone`8for other options. To use your WSGI application with any of them you will need9a FastCGI server first. The most popular one is `flup`_ which we will use for10this guide. Make sure to have it installed to follow along.

Flask will now give you an error in debug mode if a post request caused a redirect by the routing system.Armin Ronacherbefore adoption6847329

lines 11–163why, one step back
11
12.. admonition:: Watch Out1314 Please make sure in advance that any ``app.run()`` calls you might15 have in your application file are inside an ``if __name__ ==16 '__main__':`` block or moved to a separate file. Just make sure it's17 not called because this will always start a local WSGI server which18 we do not want if we deploy that application to FastCGI.1920Creating a `.fcgi` file21-----------------------2223First you need to create the FastCGI server file. Let's call it24`yourapplication.fcgi`::2526 #!/usr/bin/python27 from flup.server.fcgi import WSGIServer28 from yourapplication import app2930 if __name__ == '__main__':31 WSGIServer(app).run()3233This is enough for Apache to work, however nginx and older versions of34lighttpd need a socket to be explicitly passed to communicate with the35FastCGI server. For that to work you need to pass the path to the36socket to the :class:`~flup.server.fcgi.WSGIServer`::3738 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()3940The path has to be the exact same path you define in the server41config.4243Save the `yourapplication.fcgi` file somewhere you will find it again.44It makes sense to have that in `/var/www/yourapplication` or something45similar.4647Make sure to set the executable bit on that file so that the servers48can execute it:4950.. sourcecode:: text5152 # chmod +x /var/www/yourapplication/yourapplication.fcgi5354Configuring lighttpd55--------------------5657A basic FastCGI configuration for lighttpd looks like that::5859 fastcgi.server = ("/yourapplication.fcgi" =>60 ((61 "socket" => "/tmp/yourapplication-fcgi.sock",62 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",63 "check-local" => "disable",64 "max-procs" => 165 ))66 )6768 alias.url = (69 "/static/" => "/path/to/your/static"70 )7172 url.rewrite-once = (73 "^(/static.*)$" => "$1",74 "^(/.*)$" => "/yourapplication.fcgi$1"7576Remember to enable the FastCGI, alias and rewrite modules. This77configuration binds the application to `/yourapplication`. If you want78the application to work in the URL root you have to work around a79lighttpd bug with the80:class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.8182Make sure to apply it only if you are mounting the application the URL83root. Also, see the Lighty docs for more information on `FastCGI and84Python <http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_85(note that explicitly passing a socket to run() is no longer necessary).868788Configuring nginx89-----------------9091Installing FastCGI applications on nginx is a bit different because by92default no FastCGI parameters are forwarded.9394A basic flask FastCGI configuration for nginx looks like this::9596 location = /yourapplication { rewrite ^ /yourapplication/ last; }97 location /yourapplication { try_files $uri @yourapplication; }98 location @yourapplication {99 include fastcgi_params;100 fastcgi_split_path_info ^(/yourapplication)(.*)$;101 fastcgi_param PATH_INFO $fastcgi_path_info;102 fastcgi_param SCRIPT_NAME $fastcgi_script_name;103 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;104 }105106This configuration binds the application to `/yourapplication`. If you107want to have it in the URL root it's a bit simpler because you don't108have to figure out how to calculate `PATH_INFO` and `SCRIPT_NAME`::109110 location / { try_files $uri @yourapplication; }111 location @yourapplication {112 include fastcgi_params;113 fastcgi_param PATH_INFO $fastcgi_script_name;114 fastcgi_param SCRIPT_NAME "";115 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;116 }117118Running FastCGI Processes119-------------------------120121Since Nginx and others do not load FastCGI apps, you have to do it by122yourself. `Supervisor can manage FastCGI processes.123<http://supervisord.org/configuration.html#fcgi-program-x-section-settings>`_124You can look around for other FastCGI process managers or write a script125to run your `.fcgi` file at boot, e.g. using a SysV ``init.d`` script.126For a temporary solution, you can always run the ``.fcgi`` script inside127GNU screen. See ``man screen`` for details, and note that this is a128manual solution which does not persist across system restart::129130 $ screen131 $ /var/www/yourapplication/yourapplication.fcgi132133Debugging134---------135136FastCGI deployments tend to be hard to debug on most webservers. Very137often the only thing the server log tells you is something along the138lines of "premature end of headers". In order to debug the application139the only thing that can really give you ideas why it breaks is switching140to the correct user and executing the application by hand.141142This example assumes your application is called `application.fcgi` and143that your webserver user is `www-data`::144145 $ su www-data146 $ cd /var/www/yourapplication147 $ python application.fcgi148 Traceback (most recent call last):149 File "yourapplication.fcgi", line 4, in <module>150 ImportError: No module named yourapplication151152In this case the error seems to be "yourapplication" not being on the153python path. Common problems are:154155- Relative paths being used. Don't rely on the current working directory156- The code depending on environment variables that are not set by the157 web server.158- Different python interpreters being used.159160.. _nginx: http://nginx.org/161.. _lighttpd: http://www.lighttpd.net/162.. _cherokee: http://www.cherokee-project.com/163.. _flup: http://trac.saddi.com/flup