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 fileTake it
The walk stopped after 200 commits: lines older than 2912ff6 point at it.
Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 1–2why, one step back1.. _deploying-fastcgi:2Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 3–5why, one step back3FastCGI4=======5Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 6–11why, one step back6FastCGI 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.Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 12–14why, one step back1213.. admonition:: Watch Out14Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 15–19why, one step back15 Please make sure in advance that any ``app.run()`` calls you might16 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.Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 20–29why, one step back2021Creating 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 appMerge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
line 30why, one step back30updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 31–32why, one step back31 if __name__ == '__main__':32 WSGIServer(app).run()Added some whitespaceArmin Ronacherbefore adoption2912ff6
line 33why, one step back33updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
line 34why, one step back34This is enough for Apache to work, however nginx and older versions ofMerge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 35–37why, one step back35lighttpd need a socket to be explicitly passed to communicate with the36FastCGI server. For that to work you need to pass the path to the37socket to the :class:`~flup.server.fcgi.WSGIServer`::Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 38–59why, one step back3839 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::59updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 60–61why, one step back60 fastcgi.server = ("/yourapplication.fcgi" =>61 ((Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 62–63why, one step back62 "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 back64 "check-local" => "disable",65 "max-procs" -> 166 ))Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 67–68why, one step back67 )68updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 69–71why, one step back69 alias.url = (70 "/static/" => "/path/to/your/static"71 )Added some whitespaceArmin Ronacherbefore adoption2912ff6
line 72why, one step back72updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 73–76why, one step back73 url.rewrite-once = (74 "^(/static.*)$" => "$1",75 "^(/.*)$" => "/yourapplication.fcgi$1"76Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 77–80why, one step back77Remember to enable the FastCGI, alias and rewrite modules. This78configuration binds the application to `/yourapplication`. If you want79the application to work in the URL root you have to work around a80lighttpd bug with theupdated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 81–82why, one step back81:class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.82Added some whitespaceArmin Ronacherbefore adoption2912ff6
line 83why, one step back83Make sure to apply it only if you are mounting the application the URLMerge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 84–86why, one step back84root. Also, see the Lighty docs for more information on `FastCGI and85Python <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 back87Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 88–91why, one step back8889Configuring nginx90-----------------91Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 92–93why, one step back92Installing FastCGI applications on nginx is a bit different because by93default no FastCGI parameters are forwarded.Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 94–106why, one step back9495A 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 }106Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 107–109why, one step back107This configuration binds the application to `/yourapplication`. If you108want 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`::Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 110–118why, one step back110111 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 }118Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 119–120why, one step back119Running FastCGI Processes120-------------------------Added some whitespaceArmin Ronacherbefore adoption2912ff6
line 121why, one step back121Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 122–130why, one step back122Since Nginx and others do not load FastCGI apps, you have to do it by123yourself. `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::130Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 131–136why, one step back131 $ screen132 $ /var/www/yourapplication/yourapplication.fcgi133134Debugging135---------136Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 137–141why, one step back137FastCGI deployments tend to be hard to debug on most webservers. Very138often 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.Added some whitespaceArmin Ronacherbefore adoption2912ff6
line 142why, one step back142Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 143–144why, one step back143This example assumes your application is called `application.fcgi` and144that your webserver user is `www-data`::Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 145–149why, one step back145146 $ 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 back150 File "yourapplication.fcgi", line 4, in <module>Added some whitespaceArmin Ronacherbefore adoption2912ff6
lines 151–152why, one step back151 ImportError: No module named yourapplication152Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 153–154why, one step back153In this case the error seems to be "yourapplication" not being on the154python path. Common problems are:Added some whitespaceArmin Ronacherbefore adoption2912ff6
line 155why, one step back155Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
lines 156–157why, one step back156- Relative paths being used. Don't rely on the current working directory157- The code depending on environment variables that are not set by theAdded some whitespaceArmin Ronacherbefore adoption2912ff6
line 158why, one step back158 web server.Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
line 159why, one step back159- Different python interpreters being used.Added some whitespaceArmin Ronacherbefore adoption2912ff6
line 160why, one step back160Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
line 161why, one step back161.. _nginx: http://nginx.org/Added some whitespaceArmin Ronacherbefore adoption2912ff6
line 162why, one step back162.. _lighttpd: http://www.lighttpd.net/Merge branch 'master' into blueprintsArmin Ronacherbefore adoption3add9c5
line 163why, one step back163.. _cherokee: http://www.cherokee-project.com/Added some whitespaceArmin Ronacherbefore adoption2912ff6
line 164why, one step back164.. _flup: http://trac.saddi.com/flup