Skip to content
Myr

fastcgi.rst

This file

Path

docs/deploying/fastcgi.rst

164 lines · 5.8 KiB

Last changed by

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

History of this file

Take it

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

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

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

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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.

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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.

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

lines 31–34why, one step back
31 if __name__ == '__main__':
32 WSGIServer(app).run()3334This 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`::

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

lines 38–64why, 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::5960 fastcgi.server = ("/yourapplication.fcgi" =>61 ((62 "socket" => "/tmp/yourapplication-fcgi.sock",63 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",64 "check-local" => "disable",

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

line 65why, one step back
65 "max-procs" => 1

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

lines 66–76why, one step back
66 ))
67 )6869 alias.url = (70 "/static/" => "/path/to/your/static"71 )7273 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

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

lines 81–83why, one step back
81:class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.
8283Make 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).

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

lines 87–91why, one step back
87
8889Configuring 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.

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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`::

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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-------------------------

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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.

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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`::

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

lines 145–152why, one step back
145
146 $ su www-data147 $ cd /var/www/yourapplication148 $ python application.fcgi149 Traceback (most recent call last):150 File "yourapplication.fcgi", line 4, in <module>151 ImportError: No module named yourapplication152

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:

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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.

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

line 160why, one step back
160

Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5

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

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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/

Documented some changes in config handling.Armin Ronacherbefore adoption34a4947

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