File: node3.html

package info (click to toggle)
cherrypy 0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 10,324 kB
  • ctags: 1,759
  • sloc: python: 14,411; sh: 6,915; perl: 2,472; makefile: 76
file content (320 lines) | stat: -rw-r--r-- 11,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>2. How to run a CherryPy server behind Apache</title>
<META NAME="description" CONTENT="2. How to run a CherryPy server behind Apache">
<META NAME="keywords" CONTENT="howto">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="howto.css" type='text/css'>
<link rel="first" href="howto.html">
<link rel="contents" href="contents.html" title="Contents">

<LINK REL="next" HREF="node4.html">
<LINK REL="previous" HREF="node2.html">
<LINK REL="up" HREF="howto.html">
<LINK REL="next" HREF="node4.html">
<meta name='aesop' content='information'>
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node2.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A HREF="howto.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node4.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy HowTo</td>
<td><A HREF="node1.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node2.html">1. How to serve</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="howto.html">CherryPy HowTo</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node4.html">3. How to connect</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>

<UL CLASS="ChildLinks">
<LI><A href="node3.html#SECTION003100000000000000000">2.1 Introduction</a>
<LI><A href="node3.html#SECTION003200000000000000000">2.2 Using persistent CGI</a>
<UL>
<LI><A href="node3.html#SECTION003210000000000000000">2.2.1 Example</a>
</ul>
<LI><A href="node3.html#SECTION003300000000000000000">2.3 Using FastCGI</a>
<UL>
<LI><A href="node3.html#SECTION003310000000000000000">2.3.1 Example</a>
</ul>
<LI><A href="node3.html#SECTION003400000000000000000">2.4 Using mod_rewrite</a>
<UL>
<LI><A href="node3.html#SECTION003410000000000000000">2.4.1 Example</a>
</ul></ul>
<!--End of Table of Child-Links-->
<HR>

<H1><A NAME="SECTION003000000000000000000">
2. How to run a CherryPy server behind Apache</A>
</H1>

<P>

<H1><A NAME="SECTION003100000000000000000">
2.1 Introduction</A>
</H1>

<P>
CherryPy's built-in HTTP server is now pretty robust, but some people might still want to run CherryPy behind Apache.

<P>
Whether to run CherryPy exposed or behind Apache depends on many criteria so this question is out of the scope of this HowTo.

<P>
This HowTo will show you how to run CherryPy behind Apache, but you can probably adapt it for any other
webserver.

<P>
There are several ways to run CherryPy behind Apache:

<P>

<H1><A NAME="SECTION003200000000000000000">
2.2 Using persistent CGI</A>
</H1>

<P>
The way it works is very easy to understand. We use a small cgi script called <b>cherrypcgi.cgi</b>
as a link between Apache and CherryPy. When someone requests a page, Apache invokes this cgi script.
The script then connects to the CherryPy server, gets the page and returns it to Apache.

<P>
Of course, it takes a little extra time because a new process is created everytime the script is
called. And also because the request and the response go through the script instead of going directly
from Apache to CherryPy and back.

<P>
But that extra time is really small, so it's not a big problem. (and if you have a really high traffic
website, you can use load balancing anyway :-)

<P>

<H2><A NAME="SECTION003210000000000000000">
2.2.1 Example</A>
</H2>
The cgi script uses the HTTP protocol to talk to the CherryPy server. Therefore, the CherryPy server
doesn't make any difference between talking to a browser or talking to the cgi script.

<P>
This means that you can just start it normally.

<P>
If you're on Unix, it's probably better to run the CherryPy server on an AF_UNIX socket. This is what we will
do in this example.

<P>
To do so, edit the configuration file of the CherryPyServer and enter the following lines:
<div class="verbatim"><pre>
[server]
socketFile=socketFile.soc
</pre></div>
This means that the CherryPy server will listen on that AF_UNIX socket instead of a regular socket port.

<P>
Now, just edit the <span class="file">cherrypcgi.cgi</span> file provided with the distribution and modify the line that says:
<div class="verbatim"><pre>
socketFile='put your socket file here'
</pre></div>
Put the name of the AF_UNIX socket where the CherryPy server is listening.

<P>
The last thing to do is to configure Apache so it will call the cgi script for each request:
In Apache's configuration file (<span class="file">commonhttpd.conf</span> for instance), add the following lines (of course, you have
to adapt the path of cherrypcgi.cgi):
<div class="verbatim"><pre>
RewriteEngine on
RewriteRule ^(.*) /home/cherrypy/cherrypcgi.cgi$1 [e=HTTP_CGI_AUTHORIZATION:%1,t=application/x-httpd-cgi,l]
</pre></div>
And that's it ! Start Apache, start the CherryPy server and it should work.

<P>
It is also possible to tweak cherrypcgi.cgi so it automatically restarts the CherryPy server if it ever goes down.
There will be another HowTo on this.

<P>

<H1><A NAME="SECTION003300000000000000000">
2.3 Using FastCGI</A>
</H1>

<P>
FastCgi works very much like persistent CGI, except the cgi script is constantly running. This means that no process
has to be created for each request, which saves a lot of time !

<P>
The current implementation of FastCGI in CherryPy is not optimized because the FastCGI script is a standalone script
that connects to the CherryPy backend, instead of being directly integrated in the backend. But this method is still a
lot faster than persistent CGI.

<P>
The FastCGI script is called <b>cherryfcgi.cgi</b>

<P>

<H2><A NAME="SECTION003310000000000000000">
2.3.1 Example</A>
</H2>
The FastCGI script uses the HTTP protocol to talk to the CherryPy server. Therefore, the CherryPy server
doesn't make any difference between talking to a browser or talking to the FastCGI script.

<P>
This means that you can just start it normally.

<P>
If you're on Unix, it's probably better to run the CherryPy server on an AF_UNIX socket. This is what we will
do in this example.

<P>
To do so, edit the configuration file of the CherryPyServer and enter the following lines:
<div class="verbatim"><pre>
[server]
socketFile=socketFile.soc
</pre></div>
This means that the CherryPy server will listen on that AF_UNIX socket instead of a regular socket port.

<P>
Now, just edit the <span class="file">cherryfcgi.cgi</span> file provided with the distribution and modify the line that says:
<div class="verbatim"><pre>
socketFile='put your socket file here'
</pre></div>
Put the name of the AF_UNIX socket where the CherryPy server is listening.

<P>
The last thing to do is to configure Apache so it will connect to the FastCGI script for each request:
In Apache's configuration file (<span class="file">commonhttpd.conf</span> for instance), add the following lines (of course, you have
to adapt the path of cherrypcgi.cgi):
<div class="verbatim"><pre>
SetHandler fastcgi-script
RewriteEngine on
RewriteRule ^(.*) /home/cherrypy/cherryfcgi.cgi/$1 [L]
</pre></div>
You also need to make sure that Apache loads the FastCGI module. The following lines should be somewhere in your Apache configuration files:
<div class="verbatim"><pre>
LoadModule fastcgi_module    modules/mod_fastcgi.so
AddModule mod_fastcgi.c
</pre></div>

<P>
And that's it ! Start Apache, start the CherryPy server and it should work.

<P>
<div class="seealso">
  <p class="heading"><b>See Also:</b></p>

    <dl compact class="seeurl">
    <dt><a href="http://www.fastcgi.com"
        class="url">http://www.fastcgi.com</a>
    <dd>For more information on FastCGI
  </dl>
</div>

<P>

<H1><A NAME="SECTION003400000000000000000">
2.4 Using mod_rewrite</A>
</H1>

<P>
It's easy to configure Apache so it just passes all requests to the CherryPy server, reads the response and
passes the response to the client.

<P>
This method is a bit faster than persistent CGI because no CGI process needs to be created for each request.

<P>

<H2><A NAME="SECTION003410000000000000000">
2.4.1 Example</A>
</H2>
In this example, we'll assume that the web site is www.cherrypy.org and that the CherryPy server is running
on the same machine as Apache, on the port 8000. Of course, you'll have to adapt it for your own configuration.

<P>
Configuring Apache is very easy:
<div class="verbatim"><pre>
RewriteEngine on
RewriteRule ^(.*) http://localhost:8000$1 [p]
</pre></div>

<P>
The last thing we have to do is tell CherryPy that it's serving pages for www.cherrypy.org (and not localhost).
All it takes is 3 lines of code in the <var>initRequest</var> special function:

<P>
<div class="verbatim"><pre>
def initRequest():
    request.headerMap['host']='www.cherrypy.org'
    request.base='http://www.cherrypy.org'
    request.browserUrl=request.browserUrl.replace('http://localhost:8000', 'http://www.cherrypy.org')
</pre></div>

<P>
And voila !

<P>
Note, this can also be done with mod_proxy instead of mod_rewrite.

<P>
Many thanks to Andreas Kostyrka for this tip.

<P>

<DIV CLASS="navigation">
<p><hr>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A HREF="node2.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A HREF="howto.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A HREF="node4.html"><img src="../icons/next.gif"
  border="0" height="32"
  alt="Next Page" width="32"></A></td>
<td align="center" width="100%">CherryPy HowTo</td>
<td><A HREF="node1.html"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
<td><img src="../icons/blank.gif"
  border="0" height="32"
  alt="" width="32"></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" HREF="node2.html">1. How to serve</A>
<b class="navlabel">Up:</b> <a class="sectref" HREF="howto.html">CherryPy HowTo</A>
<b class="navlabel">Next:</b> <a class="sectref" HREF="node4.html">3. How to connect</A>
<hr>
<span class="release-info">Release 0.10, documentation updated on 19 March 2004.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>