Skip to content
Myr

fastcgi.rst

This file

Path

docs/deploying/fastcgi.rst

164 lines · 5.8 KiB

Last changed by

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

History of this file

Take it

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

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 1–2why, one step back
1.. _deploying-fastcgi:
2

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

lines 3–5why, one step back
3FastCGI
4=======5

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 6–11why, one step back
6FastCGI is a deployment option on servers like `nginx`_, `lighttpd`_,
7and `cherokee`_; see :ref:`deploying-uwsgi` and8:ref:`deploying-other-servers` for other options. To use your WSGI9application with any of them you will need a FastCGI server first. The10most popular one is `flup`_ which we will use for this guide. Make sure11to have it installed to follow along.

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

lines 12–14why, one step back
12
13.. admonition:: Watch Out14

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 15–19why, one step back
15 Please make sure in advance that any ``app.run()`` calls you might
16 have in your application file are inside an ``if __name__ ==17 '__main__':`` block or moved to a separate file. Just make sure it's18 not called because this will always start a local WSGI server which19 we do not want if we deploy that application to FastCGI.

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

lines 20–29why, one step back
20
21Creating a `.fcgi` file22-----------------------2324First you need to create the FastCGI server file. Let's call it25`yourapplication.fcgi`::2627 #!/usr/bin/python28 from flup.server.fcgi import WSGIServer29 from yourapplication import app

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

line 30why, one step back
30

updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc

lines 31–32why, one step back
31 if __name__ == '__main__':
32 WSGIServer(app).run()

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

line 33why, one step back
33

updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc

line 34why, one step back
34This is enough for Apache to work, however nginx and older versions of

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 35–37why, one step back
35lighttpd need a socket to be explicitly passed to communicate with the
36FastCGI server. For that to work you need to pass the path to the37socket to the :class:`~flup.server.fcgi.WSGIServer`::

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

lines 38–59why, one step back
38
39 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()4041The path has to be the exact same path you define in the server42config.4344Save the `yourapplication.fcgi` file somewhere you will find it again.45It makes sense to have that in `/var/www/yourapplication` or something46similar.4748Make sure to set the executable bit on that file so that the servers49can execute it:5051.. sourcecode:: text5253 # chmod +x /var/www/yourapplication/yourapplication.fcgi5455Configuring lighttpd56--------------------5758A basic FastCGI configuration for lighttpd looks like that::59

updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc

lines 60–61why, one step back
60 fastcgi.server = ("/yourapplication.fcgi" =>
61 ((

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

lines 62–63why, one step back
62 "socket" => "/tmp/yourapplication-fcgi.sock",
63 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",

updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc

lines 64–66why, one step back
64 "check-local" => "disable",
65 "max-procs" -> 166 ))

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

lines 67–68why, one step back
67 )
68

updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc

lines 69–71why, one step back
69 alias.url = (
70 "/static/" => "/path/to/your/static"71 )

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

line 72why, one step back
72

updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc

lines 73–76why, one step back
73 url.rewrite-once = (
74 "^(/static.*)$" => "$1",75 "^(/.*)$" => "/yourapplication.fcgi$1"76

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 77–80why, one step back
77Remember to enable the FastCGI, alias and rewrite modules. This
78configuration binds the application to `/yourapplication`. If you want79the application to work in the URL root you have to work around a80lighttpd bug with the

updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc

lines 81–82why, one step back
81:class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.
82

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

line 83why, one step back
83Make sure to apply it only if you are mounting the application the URL

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 84–86why, one step back
84root. Also, see the Lighty docs for more information on `FastCGI and
85Python <http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_86(note that explicitly passing a socket to run() is no longer necessary).

updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc

line 87why, one step back
87

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

lines 88–91why, one step back
88
89Configuring nginx90-----------------91

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 92–93why, one step back
92Installing FastCGI applications on nginx is a bit different because by
93default no FastCGI parameters are forwarded.

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

lines 94–106why, one step back
94
95A basic flask FastCGI configuration for nginx looks like this::9697 location = /yourapplication { rewrite ^ /yourapplication/ last; }98 location /yourapplication { try_files $uri @yourapplication; }99 location @yourapplication {100 include fastcgi_params;101 fastcgi_split_path_info ^(/yourapplication)(.*)$;102 fastcgi_param PATH_INFO $fastcgi_path_info;103 fastcgi_param SCRIPT_NAME $fastcgi_script_name;104 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;105 }106

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 107–109why, one step back
107This configuration binds the application to `/yourapplication`. If you
108want to have it in the URL root it's a bit simpler because you don't109have to figure out how to calculate `PATH_INFO` and `SCRIPT_NAME`::

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

lines 110–118why, one step back
110
111 location / { try_files $uri @yourapplication; }112 location @yourapplication {113 include fastcgi_params;114 fastcgi_param PATH_INFO $fastcgi_script_name;115 fastcgi_param SCRIPT_NAME "";116 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;117 }118

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 119–120why, one step back
119Running FastCGI Processes
120-------------------------

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

line 121why, one step back
121

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 122–130why, one step back
122Since Nginx and others do not load FastCGI apps, you have to do it by
123yourself. `Supervisor can manage FastCGI processes.124<http://supervisord.org/configuration.html#fcgi-program-x-section-settings>`_125You can look around for other FastCGI process managers or write a script126to run your `.fcgi` file at boot, e.g. using a SysV ``init.d`` script.127For a temporary solution, you can always run the ``.fcgi`` script inside128GNU screen. See ``man screen`` for details, and note that this is a129manual solution which does not persist across system restart::130

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

lines 131–136why, one step back
131 $ screen
132 $ /var/www/yourapplication/yourapplication.fcgi133134Debugging135---------136

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 137–141why, one step back
137FastCGI deployments tend to be hard to debug on most webservers. Very
138often the only thing the server log tells you is something along the139lines of "premature end of headers". In order to debug the application140the only thing that can really give you ideas why it breaks is switching141to the correct user and executing the application by hand.

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

line 142why, one step back
142

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 143–144why, one step back
143This example assumes your application is called `application.fcgi` and
144that your webserver user is `www-data`::

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

lines 145–149why, one step back
145
146 $ su www-data147 $ cd /var/www/yourapplication148 $ python application.fcgi149 Traceback (most recent call last):

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

line 150why, one step back
150 File "yourapplication.fcgi", line 4, in <module>

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

lines 151–152why, one step back
151 ImportError: No module named yourapplication
152

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 153–154why, one step back
153In this case the error seems to be "yourapplication" not being on the
154python path. Common problems are:

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

line 155why, one step back
155

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

lines 156–157why, one step back
156- Relative paths being used. Don't rely on the current working directory
157- The code depending on environment variables that are not set by the

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

line 158why, one step back
158 web server.

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

line 159why, one step back
159- Different python interpreters being used.

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

line 160why, one step back
160

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

line 161why, one step back
161.. _nginx: http://nginx.org/

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

line 162why, one step back
162.. _lighttpd: http://www.lighttpd.net/

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

line 163why, one step back
163.. _cherokee: http://www.cherokee-project.com/

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

line 164why, one step back
164.. _flup: http://trac.saddi.com/flup