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
1.. _deploying-fastcgi:23FastCGI4=======56FastCGI 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.1213.. admonition:: Watch Out1415 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.2021Creating 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 app3031 if __name__ == '__main__':32 WSGIServer(app).run()3334This is enough for Apache to work, however nginx and older versions of35lighttpd 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`::3839 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",65 "max-procs" -> 166 ))67 )6869 alias.url = (70 "/static/" => "/path/to/your/static"71 )7273 url.rewrite-once = (74 "^(/static.*)$" => "$1",75 "^(/.*)$" => "/yourapplication.fcgi$1"7677Remember 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 the81:class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.8283Make sure to apply it only if you are mounting the application the URL84root. 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).878889Configuring nginx90-----------------9192Installing FastCGI applications on nginx is a bit different because by93default no FastCGI parameters are forwarded.9495A 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 }106107This 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`::110111 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 }118119Running FastCGI Processes120-------------------------121122Since 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::130131 $ screen132 $ /var/www/yourapplication/yourapplication.fcgi133134Debugging135---------136137FastCGI 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.142143This example assumes your application is called `application.fcgi` and144that your webserver user is `www-data`::145146 $ 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 yourapplication152153In this case the error seems to be "yourapplication" not being on the154python path. Common problems are:155156- Relative paths being used. Don't rely on the current working directory157- The code depending on environment variables that are not set by the158 web server.159- Different python interpreters being used.160161.. _nginx: http://nginx.org/162.. _lighttpd: http://www.lighttpd.net/163.. _cherokee: http://www.cherokee-project.com/164.. _flup: http://trac.saddi.com/flup