Skip to content
Myr

fastcgi.rst

This file

Path

docs/deploying/fastcgi.rst

238 lines · 8.3 KiB

Last changed by

Fixed a typoArmin Ronacherbefore adoptionc541f7e

History of this file

Take it

The walk stopped after 200 commits: lines older than 3de8de1 point at it.

pip > easy_installKenneth Reitzbefore adoption3de8de1

lines 1–5why, one step back
1.. _deploying-fastcgi:
23FastCGI4=======5

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption3249eeb

lines 6–10why, one step back
6FastCGI is a deployment option on servers like `nginx`_, `lighttpd`_, and
7`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.

pip > easy_installKenneth Reitzbefore adoption3de8de1

lines 11–53why, one step back
11
12.. 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.fcgi53

Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9

lines 54–56why, one step back
54Configuring Apache
55------------------56

Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187

line 57why, one step back
57The example above is good enough for a basic Apache deployment but your `.fcgi`

Update example for Apache FCGI config to use worker processes using FastCgiServer, along with some notes about FastCgiExternalServerEric Radmanbefore adoption73b4a52

lines 58–63why, one step back
58file will appear in your application URL e.g.
59example.com/yourapplication.fcgi/news/. There are few ways to configure your60application so that yourapplication.fcgi does not appear in the URL. A61preferable way is to use the ScriptAlias and SetHandler configuration directives62to route requests to the FastCGI server. The following example uses63FastCgiServer to start 5 instances of the application which will handle all

Fixed a typoArmin Ronacherbefore adoptionc541f7e

line 64why, one step back
64incoming requests:

Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9

line 65why, one step back
65

Update example for Apache FCGI config to use worker processes using FastCgiServer, along with some notes about FastCgiExternalServerEric Radmanbefore adoption73b4a52

lines 66–69why, one step back
66 LoadModule fastcgi_module /usr/lib64/httpd/modules/mod_fastcgi.so
6768 FastCgiServer /var/www/html/yourapplication/app.fcgi -idle-timeout 300 -processes 569

Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9

line 70why, one step back
70 <VirtualHost *>

Update example for Apache FCGI config to use worker processes using FastCgiServer, along with some notes about FastCgiExternalServerEric Radmanbefore adoption73b4a52

lines 71–79why, one step back
71 ServerName webapp1.mydomain.com
72 DocumentRoot /var/www/html/yourapplication7374 AddHandler fastcgi-script fcgi75 ScriptAlias / /var/www/html/yourapplication/app.fcgi/7677 <Location />78 SetHandler fastcgi-script79 </Location>

Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9

line 80why, one step back
80 </VirtualHost>

Update example for Apache FCGI config to use worker processes using FastCgiServer, along with some notes about FastCgiExternalServerEric Radmanbefore adoption73b4a52

lines 81–87why, one step back
81
82These processes will be managed by Apache. If you're using an standalone FastCGI83server, you can use the FastCgiExternalServer directive instead. Note that in84the following the path is not real, it's simply used as an identifier to other85directives such as AliasMatch:8687 FastCgiServer /var/www/html/yourapplication -host 127.0.0.1:3000

Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9

line 88why, one step back
88

Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187

lines 89–90why, one step back
89If you cannot set ScriptAlias, for example on an shared web host, you can use
90WSGI middleware to remove yourapplication.fcgi from the URLs. Set .htaccess::

Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9

lines 91–107why, one step back
91
92 <IfModule mod_fcgid.c>93 AddHandler fcgid-script .fcgi94 <Files ~ (\.fcgi)>95 SetHandler fcgid-script96 Options +FollowSymLinks +ExecCGI97 </Files>98 </IfModule>99100 <IfModule mod_rewrite.c>101 Options +FollowSymlinks102 RewriteEngine On103 RewriteBase /104 RewriteCond %{REQUEST_FILENAME} !-f105 RewriteRule ^(.*)$ yourapplication.fcgi/$1 [QSA,L]106 </IfModule>107

Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187

line 108why, one step back
108Set yourapplication.fcgi::

Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9

lines 109–123why, one step back
109
110 #!/usr/bin/python111 #: optional path to your local python site-packages folder112 import sys113 sys.path.insert(0, '<your_local_path>/lib/python2.6/site-packages')114115 from flup.server.fcgi import WSGIServer116 from yourapplication import app117118 class ScriptNameStripper(object):119 def __init__(self, app):120 self.app = app121122 def __call__(self, environ, start_response):123 environ['SCRIPT_NAME'] = ''

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption2e816f5

line 124why, one step back
124 return self.app(environ, start_response)

Add detailed Apache httpd fastcgi configuration.Vadim Pestovnikovbefore adoption34380b9

lines 125–130why, one step back
125
126 app = ScriptNameStripper(app)127128 if __name__ == '__main__':129 WSGIServer(app).run()130

pip > easy_installKenneth Reitzbefore adoption3de8de1

lines 131–149why, one step back
131Configuring lighttpd
132--------------------133134A basic FastCGI configuration for lighttpd looks like that::135136 fastcgi.server = ("/yourapplication.fcgi" =>137 ((138 "socket" => "/tmp/yourapplication-fcgi.sock",139 "bin-path" => "/var/www/yourapplication/yourapplication.fcgi",140 "check-local" => "disable",141 "max-procs" => 1142 ))143 )144145 alias.url = (146 "/static/" => "/path/to/your/static"147 )148149 url.rewrite-once = (

Merge branch 'master' of github.com:mitsuhiko/flaskArmin Ronacherbefore adoption7fc0ddb

line 150why, one step back
150 "^(/static($|/.*))$" => "$1",

pip > easy_installKenneth Reitzbefore adoption3de8de1

lines 151–152why, one step back
151 "^(/.*)$" => "/yourapplication.fcgi$1"
152

Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187

lines 153–155why, one step back
153Remember to enable the FastCGI, alias and rewrite modules. This configuration
154binds the application to `/yourapplication`. If you want the application to155work in the URL root you have to work around a lighttpd bug with the

pip > easy_installKenneth Reitzbefore adoption3de8de1

lines 156–158why, one step back
156:class:`~werkzeug.contrib.fixers.LighttpdCGIRootFix` middleware.
157158Make sure to apply it only if you are mounting the application the URL

Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187

lines 159–161why, one step back
159root. Also, see the Lighty docs for more information on `FastCGI and Python
160<http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI>`_ (note that161explicitly passing a socket to run() is no longer necessary).

pip > easy_installKenneth Reitzbefore adoption3de8de1

lines 162–174why, one step back
162
163Configuring nginx164-----------------165166Installing FastCGI applications on nginx is a bit different because by167default no FastCGI parameters are forwarded.168169A basic flask FastCGI configuration for nginx looks like this::170171 location = /yourapplication { rewrite ^ /yourapplication/ last; }172 location /yourapplication { try_files $uri @yourapplication; }173 location @yourapplication {174 include fastcgi_params;

Touch up fastcgi doc.Ron DuPlainbefore adoptionfb01187

line 175why, one step back
175 fastcgi_split_path_info ^(/yourapplication)(.*)$;

pip > easy_installKenneth Reitzbefore adoption3de8de1

lines 176–238why, one step back
176 fastcgi_param PATH_INFO $fastcgi_path_info;
177 fastcgi_param SCRIPT_NAME $fastcgi_script_name;178 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;179 }180181This configuration binds the application to `/yourapplication`. If you182want to have it in the URL root it's a bit simpler because you don't183have to figure out how to calculate `PATH_INFO` and `SCRIPT_NAME`::184185 location / { try_files $uri @yourapplication; }186 location @yourapplication {187 include fastcgi_params;188 fastcgi_param PATH_INFO $fastcgi_script_name;189 fastcgi_param SCRIPT_NAME "";190 fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;191 }192193Running FastCGI Processes194-------------------------195196Since Nginx and others do not load FastCGI apps, you have to do it by197yourself. `Supervisor can manage FastCGI processes.198<http://supervisord.org/configuration.html#fcgi-program-x-section-settings>`_199You can look around for other FastCGI process managers or write a script200to run your `.fcgi` file at boot, e.g. using a SysV ``init.d`` script.201For a temporary solution, you can always run the ``.fcgi`` script inside202GNU screen. See ``man screen`` for details, and note that this is a203manual solution which does not persist across system restart::204205 $ screen206 $ /var/www/yourapplication/yourapplication.fcgi207208Debugging209---------210211FastCGI deployments tend to be hard to debug on most webservers. Very212often the only thing the server log tells you is something along the213lines of "premature end of headers". In order to debug the application214the only thing that can really give you ideas why it breaks is switching215to the correct user and executing the application by hand.216217This example assumes your application is called `application.fcgi` and218that your webserver user is `www-data`::219220 $ su www-data221 $ cd /var/www/yourapplication222 $ python application.fcgi223 Traceback (most recent call last):224 File "yourapplication.fcgi", line 4, in <module>225 ImportError: No module named yourapplication226227In this case the error seems to be "yourapplication" not being on the228python path. Common problems are:229230- Relative paths being used. Don't rely on the current working directory231- The code depending on environment variables that are not set by the232 web server.233- Different python interpreters being used.234235.. _nginx: http://nginx.org/236.. _lighttpd: http://www.lighttpd.net/237.. _cherokee: http://www.cherokee-project.com/238.. _flup: http://trac.saddi.com/flup