fastcgi.rst
This file
Path
docs/deploying/fastcgi.rst
128 lines · 4.1 KiB
Last changed by
Break up deployment docs into separate documents.Armin Ronacherbefore adoptionc64a4e0
History of this fileTake it
1FastCGI2=======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.1011Creating a `.fcgi` file12-----------------------1314First you need to create the FastCGI server file. Let's call it15`yourapplication.fcgi`::1617 #!/usr/bin/python18 from flup.server.fcgi import WSGIServer19 from yourapplication import app2021 WSGIServer(app).run()2223This is enough for Apache to work, however lighttpd and nginx need a24socket to communicate with the FastCGI server. For that to work you25need to pass the path to the socket to the26:class:`~flup.server.fcgi.WSGIServer`::2728 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()2930The path has to be the exact same path you define in the server31config.3233Save the `yourapplication.fcgi` file somewhere you will find it again.34It makes sense to have that in `/var/www/yourapplication` or something35similar.3637Make sure to set the executable bit on that file so that the servers38can execute it::3940 # chmod +x /var/www/yourapplication/yourapplication.fcgi4142Configuring lighttpd43--------------------4445A basic FastCGI configuration for lighttpd looks like that::4647 fastcgi.server = ("/yourapplication" =>48 "yourapplication" => (49 "socket" => "/tmp/yourapplication-fcgi.sock",50 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",51 "check-local" => "disable"52 )53 )5455This configuration binds the application to `/yourapplication`. If you56want the application to work in the URL root you have to work around a57lighttpd bug with the :class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix`58middleware.5960Make sure to apply it only if you are mounting the application the URL61root.6263Configuring nginx64-----------------6566Installing FastCGI applications on nginx is a bit tricky because by default67some FastCGI parameters are not properly forwarded.6869A basic FastCGI configuration for nginx looks like this::7071 location /yourapplication/ {72 include fastcgi_params;73 if ($uri ~ ^/yourapplication/(.*)?) {74 set $path_url $1;75 }76 fastcgi_param PATH_INFO $path_url;77 fastcgi_param SCRIPT_NAME /yourapplication;78 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;79 }8081This configuration binds the application to `/yourapplication`. If you want82to have it in the URL root it's a bit easier because you don't have to figure83out how to calculate `PATH_INFO` and `SCRIPT_NAME`::8485 location /yourapplication/ {86 include fastcgi_params;87 fastcgi_param PATH_INFO $fastcgi_script_name;88 fastcgi_param SCRIPT_NAME "";89 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;90 }9192Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You93can either write an `init.d` script for that or execute it inside a screen94session::9596 $ screen97 $ /var/www/yourapplication/yourapplication.fcgi9899Debugging100---------101102FastCGI deployments tend to be hard to debug on most webservers. Very often the103only thing the server log tells you is something along the lines of "premature104end of headers". In order to debug the application the only thing that can105really give you ideas why it breaks is switching to the correct user and106executing the application by hand.107108This example assumes your application is called `application.fcgi` and that your109webserver user is `www-data`::110111 $ su www-data112 $ cd /var/www/yourapplication113 $ python application.fcgi114 Traceback (most recent call last):115 File "yourapplication.fcg", line 4, in <module>116 ImportError: No module named yourapplication117118In this case the error seems to be "yourapplication" not being on the python119path. Common problems are:120121- relative paths being used. Don't rely on the current working directory122- the code depending on environment variables that are not set by the123 web server.124- different python interpreters being used.125126.. _lighttpd: http://www.lighttpd.net/127.. _nginx: http://nginx.net/128.. _flup: http://trac.saddi.com/flup