fastcgi.rst
This file
Path
docs/deploying/fastcgi.rst
217 lines · 7.5 KiB
Last changed by
Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption01ac057
History of this fileTake it
The walk stopped after 200 commits: lines older than 142c9c3 point at it.
Fixed typos in copy/pasted textArmin Ronacherbefore adoption142c9c3
lines 1–5why, one step back1.. _deploying-fastcgi:23FastCGI4=======5Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb
lines 6–10why, one step back6FastCGI is a deployment option on servers like `nginx`_, `lighttpd`_, and7`cherokee`_; see :ref:`deploying-uwsgi` and :ref:`deploying-wsgi-standalone`8for other options. To use your WSGI application with any of them you will need9a FastCGI server first. The most popular one is `flup`_ which we will use for10this guide. Make sure to have it installed to follow along.Fixed typos in copy/pasted textArmin Ronacherbefore adoption142c9c3
lines 11–53why, one step back1112.. admonition:: Watch Out1314 Please make sure in advance that any ``app.run()`` calls you might15 have in your application file are inside an ``if __name__ ==16 '__main__':`` block or moved to a separate file. Just make sure it's17 not called because this will always start a local WSGI server which18 we do not want if we deploy that application to FastCGI.1920Creating a `.fcgi` file21-----------------------2223First you need to create the FastCGI server file. Let's call it24`yourapplication.fcgi`::2526 #!/usr/bin/python27 from flup.server.fcgi import WSGIServer28 from yourapplication import app2930 if __name__ == '__main__':31 WSGIServer(app).run()3233This is enough for Apache to work, however nginx and older versions of34lighttpd need a socket to be explicitly passed to communicate with the35FastCGI server. For that to work you need to pass the path to the36socket to the :class:`~flup.server.fcgi.WSGIServer`::3738 WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()3940The path has to be the exact same path you define in the server41config.4243Save the `yourapplication.fcgi` file somewhere you will find it again.44It makes sense to have that in `/var/www/yourapplication` or something45similar.4647Make sure to set the executable bit on that file so that the servers48can execute it:4950.. sourcecode:: text5152 # chmod +x /var/www/yourapplication/yourapplication.fcgi53Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9
lines 54–56why, one step back54Configuring Apache55------------------56Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187
lines 57–61why, one step back57The example above is good enough for a basic Apache deployment but your `.fcgi`58file will appear in your application URL59e.g. example.com/yourapplication.fcgi/news/. There are few ways to configure60your application so that yourapplication.fcgi does not appear in the URL. A61preferable way is to use the ScriptAlias configuration directive::Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9
lines 62–67why, one step back6263 <VirtualHost *>64 ServerName example.com65 ScriptAlias / /path/to/yourapplication.fcgi/66 </VirtualHost>67Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187
lines 68–69why, one step back68If you cannot set ScriptAlias, for example on an shared web host, you can use69WSGI middleware to remove yourapplication.fcgi from the URLs. Set .htaccess::Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9
lines 70–86why, one step back7071 <IfModule mod_fcgid.c>72 AddHandler fcgid-script .fcgi73 <Files ~ (\.fcgi)>74 SetHandler fcgid-script75 Options +FollowSymLinks +ExecCGI76 </Files>77 </IfModule>7879 <IfModule mod_rewrite.c>80 Options +FollowSymlinks81 RewriteEngine On82 RewriteBase /83 RewriteCond %{REQUEST_FILENAME} !-f84 RewriteRule ^(.*)$ yourapplication.fcgi/$1 [QSA,L]85 </IfModule>86Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187
line 87why, one step back87Set yourapplication.fcgi::Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9
lines 88–102why, one step back8889 #!/usr/bin/python90 #: optional path to your local python site-packages folder91 import sys92 sys.path.insert(0, '<your_local_path>/lib/python2.6/site-packages')9394 from flup.server.fcgi import WSGIServer95 from yourapplication import app9697 class ScriptNameStripper(object):98 def __init__(self, app):99 self.app = app100101 def __call__(self, environ, start_response):102 environ['SCRIPT_NAME'] = ''Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption2e816f5
line 103why, one step back103 return self.app(environ, start_response)Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9
lines 104–109why, one step back104105 app = ScriptNameStripper(app)106107 if __name__ == '__main__':108 WSGIServer(app).run()109Fixed typos in copy/pasted textArmin Ronacherbefore adoption142c9c3
lines 110–128why, one step back110Configuring lighttpd111--------------------112113A basic FastCGI configuration for lighttpd looks like that::114115 fastcgi.server = ("/yourapplication.fcgi" =>116 ((117 "socket" => "/tmp/yourapplication-fcgi.sock",118 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",119 "check-local" => "disable",120 "max-procs" => 1121 ))122 )123124 alias.url = (125 "/static/" => "/path/to/your/static"126 )127128 url.rewrite-once = (Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption7fc0ddb
line 129why, one step back129 "^(/static($|/.*))$" => "$1",Fixed typos in copy/pasted textArmin Ronacherbefore adoption142c9c3
lines 130–131why, one step back130 "^(/.*)$" => "/yourapplication.fcgi$1"131Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187
lines 132–134why, one step back132Remember to enable the FastCGI, alias and rewrite modules. This configuration133binds the application to `/yourapplication`. If you want the application to134work in the URL root you have to work around a lighttpd bug with theFixed typos in copy/pasted textArmin Ronacherbefore adoption142c9c3
lines 135–137why, one step back135:class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.136137Make sure to apply it only if you are mounting the application the URLTouch up fastcgi doc.Ron DuPlainbefore adoptionfb01187
lines 138–140why, one step back138root. Also, see the Lighty docs for more information on `FastCGI and Python139<http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_ (note that140explicitly passing a socket to run() is no longer necessary).Fixed typos in copy/pasted textArmin Ronacherbefore adoption142c9c3
lines 141–153why, one step back141142Configuring nginx143-----------------144145Installing FastCGI applications on nginx is a bit different because by146default no FastCGI parameters are forwarded.147148A basic flask FastCGI configuration for nginx looks like this::149150 location = /yourapplication { rewrite ^ /yourapplication/ last; }151 location /yourapplication { try_files $uri @yourapplication; }152 location @yourapplication {153 include fastcgi_params;Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187
line 154why, one step back154 fastcgi_split_path_info ^(/yourapplication)(.*)$;Fixed typos in copy/pasted textArmin Ronacherbefore adoption142c9c3
lines 155–217why, one step back155 fastcgi_param PATH_INFO $fastcgi_path_info;156 fastcgi_param SCRIPT_NAME $fastcgi_script_name;157 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;158 }159160This configuration binds the application to `/yourapplication`. If you161want to have it in the URL root it's a bit simpler because you don't162have to figure out how to calculate `PATH_INFO` and `SCRIPT_NAME`::163164 location / { try_files $uri @yourapplication; }165 location @yourapplication {166 include fastcgi_params;167 fastcgi_param PATH_INFO $fastcgi_script_name;168 fastcgi_param SCRIPT_NAME "";169 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;170 }171172Running FastCGI Processes173-------------------------174175Since Nginx and others do not load FastCGI apps, you have to do it by176yourself. `Supervisor can manage FastCGI processes.177<http://supervisord.org/configuration.html#fcgi-program-x-section-settings>`_178You can look around for other FastCGI process managers or write a script179to run your `.fcgi` file at boot, e.g. using a SysV ``init.d`` script.180For a temporary solution, you can always run the ``.fcgi`` script inside181GNU screen. See ``man screen`` for details, and note that this is a182manual solution which does not persist across system restart::183184 $ screen185 $ /var/www/yourapplication/yourapplication.fcgi186187Debugging188---------189190FastCGI deployments tend to be hard to debug on most webservers. Very191often the only thing the server log tells you is something along the192lines of "premature end of headers". In order to debug the application193the only thing that can really give you ideas why it breaks is switching194to the correct user and executing the application by hand.195196This example assumes your application is called `application.fcgi` and197that your webserver user is `www-data`::198199 $ su www-data200 $ cd /var/www/yourapplication201 $ python application.fcgi202 Traceback (most recent call last):203 File "yourapplication.fcgi", line 4, in <module>204 ImportError: No module named yourapplication205206In this case the error seems to be "yourapplication" not being on the207python path. Common problems are:208209- Relative paths being used. Don't rely on the current working directory210- The code depending on environment variables that are not set by the211 web server.212- Different python interpreters being used.213214.. _nginx: http://nginx.org/215.. _lighttpd: http://www.lighttpd.net/216.. _cherokee: http://www.cherokee-project.com/217.. _flup: http://trac.saddi.com/flup