fastcgi.rst
This file
Path
docs/deploying/fastcgi.rst
152 lines · 5.2 KiB
Last changed by
updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
History of this fileTake it
The walk stopped after 200 commits: lines older than e84ca8e point at it.
Clearified thatArmin Ronacherbefore adoptione84ca8e
lines 1–27why, one step back1FastCGI2=======34A very popular deployment setup on servers like `lighttpd`_ and `nginx`_5is FastCGI. To use your WSGI application with any of them you will need6a FastCGI server first.78The most popular one is `flup`_ which we will use for this guide. Make9sure to have it installed.1011.. admonition:: Watch Out1213 Please make sure in advance that your ``app.run()`` call you might14 have in your application file, is inside an ``if __name__ ==15 '__main__':`` or moved to a separate file. Just make sure it's not16 called because this will always start a local WSGI server which we do17 not want if we deploy that application to FastCGI.1819Creating a `.fcgi` file20-----------------------2122First you need to create the FastCGI server file. Let's call it23`yourapplication.fcgi`::2425 #!/usr/bin/python26 from flup.server.fcgi import WSGIServer27 from yourapplication import appupdated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 28–30why, one step back28 29 if __name__ == '__main__':30 WSGIServer(app).run()Clearified thatArmin Ronacherbefore adoptione84ca8e
line 31why, one step back31updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 32–34why, one step back32This is enough for Apache to work, however nginx and older versions of33lighttpd need a socket to be explicitly passed to communicate with the FastCGI34server. For that to work you need to pass the path to the socket to theClearified thatArmin Ronacherbefore adoptione84ca8e
lines 35–57why, one step back35:class:`~flup.server.fcgi.WSGIServer`::3637 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()3839The path has to be the exact same path you define in the server40config.4142Save the `yourapplication.fcgi` file somewhere you will find it again.43It makes sense to have that in `/var/www/yourapplication` or something44similar.4546Make sure to set the executable bit on that file so that the servers47can execute it:4849.. sourcecode:: text5051 # chmod +x /var/www/yourapplication/yourapplication.fcgi5253Configuring lighttpd54--------------------5556A basic FastCGI configuration for lighttpd looks like that::57updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 58–59why, one step back58 fastcgi.server = ("/yourapplication.fcgi" =>59 ((Clearified thatArmin Ronacherbefore adoptione84ca8e
lines 60–61why, one step back60 "socket" => "/tmp/yourapplication-fcgi.sock",61 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 62–64why, one step back62 "check-local" => "disable",63 "max-procs" -> 164 ))Clearified thatArmin Ronacherbefore adoptione84ca8e
lines 65–66why, one step back65 )66updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 67–69why, one step back67 alias.url = (68 "/static/" => "/path/to/your/static"69 )Clearified thatArmin Ronacherbefore adoptione84ca8e
line 70why, one step back70updated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 71–79why, one step back71 url.rewrite-once = (72 "^(/static.*)$" => "$1",73 "^(/.*)$" => "/yourapplication.fcgi$1"7475Remember to enable the FastCGI, alias and rewrite modules. This configuration76binds the application to `/yourapplication`. If you want the application to77work in the URL root you have to work around a lighttpd bug with the78:class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.79Clearified thatArmin Ronacherbefore adoptione84ca8e
line 80why, one step back80Make sure to apply it only if you are mounting the application the URLupdated docs on FastCGI deployment with Lightyfmwbefore adoption94f90bc
lines 81–84why, one step back81root. Also, see the Lighty docs for more information on `FastCGI and Python82<http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_ (note that83explicitly passing a socket to run() is no longer necessary).84Clearified thatArmin Ronacherbefore adoptione84ca8e
lines 85–138why, one step back8586Configuring nginx87-----------------8889Installing FastCGI applications on nginx is a bit different because by default90no FastCGI parameters are forwarded.9192A basic flask FastCGI configuration for nginx looks like this::9394 location = /yourapplication { rewrite ^ /yourapplication/ last; }95 location /yourapplication { try_files $uri @yourapplication; }96 location @yourapplication {97 include fastcgi_params;98 fastcgi_split_path_info ^(/yourapplication)(.*)$;99 fastcgi_param PATH_INFO $fastcgi_path_info;100 fastcgi_param SCRIPT_NAME $fastcgi_script_name;101 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;102 }103104This configuration binds the application to `/yourapplication`. If you want105to have it in the URL root it's a bit simpler because you don't have to figure106out how to calculate `PATH_INFO` and `SCRIPT_NAME`::107108 location / { try_files $uri @yourapplication; }109 location @yourapplication {110 include fastcgi_params;111 fastcgi_param PATH_INFO $fastcgi_script_name;112 fastcgi_param SCRIPT_NAME "";113 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;114 }115116Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You117can either write an `init.d` script for that or execute it inside a screen118session::119120 $ screen121 $ /var/www/yourapplication/yourapplication.fcgi122123Debugging124---------125126FastCGI deployments tend to be hard to debug on most webservers. Very often the127only thing the server log tells you is something along the lines of "premature128end of headers". In order to debug the application the only thing that can129really give you ideas why it breaks is switching to the correct user and130executing the application by hand.131132This example assumes your application is called `application.fcgi` and that your133webserver user is `www-data`::134135 $ su www-data136 $ cd /var/www/yourapplication137 $ python application.fcgi138 Traceback (most recent call last):Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption5ecf01d
line 139why, one step back139 File "yourapplication.fcgi", line 4, in <module>Clearified thatArmin Ronacherbefore adoptione84ca8e
lines 140–152why, one step back140 ImportError: No module named yourapplication141142In this case the error seems to be "yourapplication" not being on the python143path. Common problems are:144145- relative paths being used. Don't rely on the current working directory146- the code depending on environment variables that are not set by the147 web server.148- different python interpreters being used.149150.. _lighttpd: http://www.lighttpd.net/151.. _nginx: http://nginx.net/152.. _flup: http://trac.saddi.com/flup