1
+ .. _deploying-fastcgi :
2
+
1
3
FastCGI
2
4
=======
3
5
4
- A very popular deployment setup on servers like `lighttpd `_ and ` nginx `_
5
- is FastCGI. To use your WSGI application with any of them you will need
6
- a FastCGI server first.
7
-
8
- The most popular one is `flup `_ which we will use for this guide. Make
9
- sure to have it installed.
6
+ FastCGI is a deployment option on servers like `nginx `_, ` lighttpd `_,
7
+ and ` cherokee `_; see :ref: ` deploying-uwsgi ` and
8
+ :ref: ` deploying-other-servers ` for other options. To use your WSGI
9
+ application with any of them you will need a FastCGI server first. The
10
+ most popular one is `flup `_ which we will use for this guide. Make sure
11
+ to have it installed to follow along .
10
12
11
13
.. admonition :: Watch Out
12
14
13
- Please make sure in advance that your ``app.run() `` call you might
14
- have in your application file, is inside an ``if __name__ ==
15
- '__main__': `` or moved to a separate file. Just make sure it's not
16
- called because this will always start a local WSGI server which we do
17
- not want if we deploy that application to FastCGI.
15
+ Please make sure in advance that any ``app.run() `` calls you might
16
+ have in your application file are inside an ``if __name__ ==
17
+ '__main__': `` block or moved to a separate file. Just make sure it's
18
+ not called because this will always start a local WSGI server which
19
+ we do not want if we deploy that application to FastCGI.
18
20
19
21
Creating a `.fcgi ` file
20
22
-----------------------
@@ -25,14 +27,14 @@ First you need to create the FastCGI server file. Let's call it
25
27
#!/usr/bin/python
26
28
from flup.server.fcgi import WSGIServer
27
29
from yourapplication import app
28
-
30
+
29
31
if __name__ == '__main__':
30
32
WSGIServer(app).run()
31
33
32
34
This is enough for Apache to work, however nginx and older versions of
33
- lighttpd need a socket to be explicitly passed to communicate with the FastCGI
34
- server. For that to work you need to pass the path to the socket to the
35
- :class: `~flup.server.fcgi.WSGIServer `::
35
+ lighttpd need a socket to be explicitly passed to communicate with the
36
+ FastCGI server. For that to work you need to pass the path to the
37
+ socket to the :class: `~flup.server.fcgi.WSGIServer `::
36
38
37
39
WSGIServer(application, bindAddress='/path/to/fcgi.sock').run()
38
40
@@ -72,22 +74,23 @@ A basic FastCGI configuration for lighttpd looks like that::
72
74
"^(/static.*)$" => "$1",
73
75
"^(/.*)$" => "/yourapplication.fcgi$1"
74
76
75
- Remember to enable the FastCGI, alias and rewrite modules. This configuration
76
- binds the application to `/yourapplication `. If you want the application to
77
- work in the URL root you have to work around a lighttpd bug with the
77
+ Remember to enable the FastCGI, alias and rewrite modules. This
78
+ configuration binds the application to `/yourapplication `. If you want
79
+ the application to work in the URL root you have to work around a
80
+ lighttpd bug with the
78
81
:class: `~werkzeug.contrib.fixers.LighttpdCGIRootFix ` middleware.
79
82
80
83
Make sure to apply it only if you are mounting the application the URL
81
- root. Also, see the Lighty docs for more information on `FastCGI and Python
82
- <http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI> `_ (note that
83
- explicitly passing a socket to run() is no longer necessary).
84
+ root. Also, see the Lighty docs for more information on `FastCGI and
85
+ Python <http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModFastCGI> `_
86
+ (note that explicitly passing a socket to run() is no longer necessary).
84
87
85
88
86
89
Configuring nginx
87
90
-----------------
88
91
89
- Installing FastCGI applications on nginx is a bit different because by default
90
- no FastCGI parameters are forwarded.
92
+ Installing FastCGI applications on nginx is a bit different because by
93
+ default no FastCGI parameters are forwarded.
91
94
92
95
A basic flask FastCGI configuration for nginx looks like this::
93
96
@@ -101,9 +104,9 @@ A basic flask FastCGI configuration for nginx looks like this::
101
104
fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;
102
105
}
103
106
104
- This configuration binds the application to `/yourapplication `. If you want
105
- to have it in the URL root it's a bit simpler because you don't have to figure
106
- out how to calculate `PATH_INFO ` and `SCRIPT_NAME `::
107
+ This configuration binds the application to `/yourapplication `. If you
108
+ want to have it in the URL root it's a bit simpler because you don't
109
+ have to figure out how to calculate `PATH_INFO ` and `SCRIPT_NAME `::
107
110
108
111
location / { try_files $uri @yourapplication; }
109
112
location @yourapplication {
@@ -113,24 +116,32 @@ out how to calculate `PATH_INFO` and `SCRIPT_NAME`::
113
116
fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;
114
117
}
115
118
116
- Since Nginx doesn't load FastCGI apps, you have to do it by yourself. You
117
- can either write an `init.d ` script for that or execute it inside a screen
118
- session::
119
+ Running FastCGI Processes
120
+ -------------------------
121
+
122
+ Since Nginx and others do not load FastCGI apps, you have to do it by
123
+ yourself. `Supervisor can manage FastCGI processes.
124
+ <http://supervisord.org/configuration.html#fcgi-program-x-section-settings> `_
125
+ You can look around for other FastCGI process managers or write a script
126
+ to run your `.fcgi ` file at boot, e.g. using a SysV ``init.d `` script.
127
+ For a temporary solution, you can always run the ``.fcgi `` script inside
128
+ GNU screen. See ``man screen `` for details, and note that this is a
129
+ manual solution which does not persist across system restart::
119
130
120
131
$ screen
121
132
$ /var/www/yourapplication/yourapplication.fcgi
122
133
123
134
Debugging
124
135
---------
125
136
126
- FastCGI deployments tend to be hard to debug on most webservers. Very often the
127
- only thing the server log tells you is something along the lines of "premature
128
- end of headers". In order to debug the application the only thing that can
129
- really give you ideas why it breaks is switching to the correct user and
130
- executing the application by hand.
137
+ FastCGI deployments tend to be hard to debug on most webservers. Very
138
+ often the only thing the server log tells you is something along the
139
+ lines of "premature end of headers". In order to debug the application
140
+ the only thing that can really give you ideas why it breaks is switching
141
+ to the correct user and executing the application by hand.
131
142
132
- This example assumes your application is called `application.fcgi ` and that your
133
- webserver user is `www-data `::
143
+ This example assumes your application is called `application.fcgi ` and
144
+ that your webserver user is `www-data `::
134
145
135
146
$ su www-data
136
147
$ cd /var/www/yourapplication
@@ -139,14 +150,15 @@ webserver user is `www-data`::
139
150
File "yourapplication.fcgi", line 4, in <module>
140
151
ImportError: No module named yourapplication
141
152
142
- In this case the error seems to be "yourapplication" not being on the python
143
- path. Common problems are:
153
+ In this case the error seems to be "yourapplication" not being on the
154
+ python path. Common problems are:
144
155
145
- - relative paths being used. Don't rely on the current working directory
146
- - the code depending on environment variables that are not set by the
156
+ - Relative paths being used. Don't rely on the current working directory
157
+ - The code depending on environment variables that are not set by the
147
158
web server.
148
- - different python interpreters being used.
159
+ - Different python interpreters being used.
149
160
150
- .. _lighttpd : http://www.lighttpd.net/
151
161
.. _nginx : http://nginx.org/
162
+ .. _lighttpd : http://www.lighttpd.net/
163
+ .. _cherokee : http://www.cherokee-project.com/
152
164
.. _flup : http://trac.saddi.com/flup
0 commit comments