Skip to content
Myr

fastcgi.rst

This file

Path

docs/deploying/fastcgi.rst

139 lines · 4.7 KiB

Last changed by

Enhanced the nginx deployment subsection of the documentation with working examples.Merlinbefore adoption7dae84f

History of this file

Take it

The walk stopped after 200 commits: lines older than 36f105c point at it.

Removed possible bashism. source -> .Armin Ronacherbefore adoption36f105c

lines 1–10why, one step back
1FastCGI
2=======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.10

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

lines 11–18why, one step back
11.. admonition:: Watch Out
1213 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.18

Removed possible bashism. source -> .Armin Ronacherbefore adoption36f105c

lines 19–45why, one step back
19Creating a `.fcgi` file
20-----------------------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 app2829 WSGIServer(app).run()3031This is enough for Apache to work, however lighttpd and nginx need a32socket to communicate with the FastCGI server. For that to work you33need to pass the path to the socket to the34:class:`~flup.server.fcgi.WSGIServer`::3536 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()3738The path has to be the exact same path you define in the server39config.4041Save the `yourapplication.fcgi` file somewhere you will find it again.42It makes sense to have that in `/var/www/yourapplication` or something43similar.4445Make sure to set the executable bit on that file so that the servers

Added troubleshooting infos. This fixes #44Armin Ronacherbefore adoptioncc8332e

lines 46–48why, one step back
46can execute it:
4748.. sourcecode:: text

Removed possible bashism. source -> .Armin Ronacherbefore adoption36f105c

lines 49–75why, one step back
49
50 # chmod +x /var/www/yourapplication/yourapplication.fcgi5152Configuring lighttpd53--------------------5455A basic FastCGI configuration for lighttpd looks like that::5657 fastcgi.server = ("/yourapplication" =>58 "yourapplication" => (59 "socket" => "/tmp/yourapplication-fcgi.sock",60 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",61 "check-local" => "disable"62 )63 )6465This configuration binds the application to `/yourapplication`. If you66want the application to work in the URL root you have to work around a67lighttpd bug with the :class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix`68middleware.6970Make sure to apply it only if you are mounting the application the URL71root.7273Configuring nginx74-----------------75

Enhanced the nginx deployment subsection of the documentation with working examples.Merlinbefore adoption7dae84f

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

Removed possible bashism. source -> .Armin Ronacherbefore adoption36f105c

line 78why, one step back
78

Enhanced the nginx deployment subsection of the documentation with working examples.Merlinbefore adoption7dae84f

line 79why, one step back
79A basic flask FastCGI configuration for nginx looks like this::

Removed possible bashism. source -> .Armin Ronacherbefore adoption36f105c

line 80why, one step back
80

Enhanced the nginx deployment subsection of the documentation with working examples.Merlinbefore adoption7dae84f

lines 81–83why, one step back
81 location = /yourapplication { rewrite ^ /yourapplication/ last; }
82 location /yourapplication { try_files $uri @yourapplication; }83 location @yourapplication {

Removed possible bashism. source -> .Armin Ronacherbefore adoption36f105c

line 84why, one step back
84 include fastcgi_params;

Enhanced the nginx deployment subsection of the documentation with working examples.Merlinbefore adoption7dae84f

lines 85–87why, one step back
85 fastcgi_split_path_info ^(/yourapplication)(.*)$;
86 fastcgi_param PATH_INFO $fastcgi_path_info;87 fastcgi_param SCRIPT_NAME $fastcgi_script_name;

Removed possible bashism. source -> .Armin Ronacherbefore adoption36f105c

lines 88–91why, one step back
88 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;
89 }9091This configuration binds the application to `/yourapplication`. If you want

Enhanced the nginx deployment subsection of the documentation with working examples.Merlinbefore adoption7dae84f

line 92why, one step back
92to have it in the URL root it's a bit simpler because you don't have to figure

Removed possible bashism. source -> .Armin Ronacherbefore adoption36f105c

lines 93–94why, one step back
93out how to calculate `PATH_INFO` and `SCRIPT_NAME`::
94

Enhanced the nginx deployment subsection of the documentation with working examples.Merlinbefore adoption7dae84f

lines 95–96why, one step back
95 location / { try_files $uri @yourapplication; }
96 location @yourapplication {

Removed possible bashism. source -> .Armin Ronacherbefore adoption36f105c

lines 97–139why, one step back
97 include fastcgi_params;
98 fastcgi_param PATH_INFO $fastcgi_script_name;99 fastcgi_param SCRIPT_NAME "";100 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;101 }102103Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You104can either write an `init.d` script for that or execute it inside a screen105session::106107 $ screen108 $ /var/www/yourapplication/yourapplication.fcgi109110Debugging111---------112113FastCGI deployments tend to be hard to debug on most webservers. Very often the114only thing the server log tells you is something along the lines of "premature115end of headers". In order to debug the application the only thing that can116really give you ideas why it breaks is switching to the correct user and117executing the application by hand.118119This example assumes your application is called `application.fcgi` and that your120webserver user is `www-data`::121122 $ su www-data123 $ cd /var/www/yourapplication124 $ python application.fcgi125 Traceback (most recent call last):126 File "yourapplication.fcg", line 4, in <module>127 ImportError: No module named yourapplication128129In this case the error seems to be "yourapplication" not being on the python130path. Common problems are:131132- relative paths being used. Don't rely on the current working directory133- the code depending on environment variables that are not set by the134 web server.135- different python interpreters being used.136137.. _lighttpd: http://www.lighttpd.net/138.. _nginx: http://nginx.net/139.. _flup: http://trac.saddi.com/flup