]> scripts.mit.edu Git - www/raw.git/blob - faq/60.mdwn
Import from TextPattern
[www/raw.git] / faq / 60.mdwn
1 [[!meta title="How do I enable FastCGI?"]]
2 ### CGI vs FastCGI
3
4 Normal CGI scripts are initialized, run, and destroyed for every
5 single browser request. The advantage of this model is that it is
6 very simple, and thus robust.
7
8 If performance or latency becomes an issue, you can enable FastCGI.
9 FastCGI is a web server module that speeds up your applications by
10 keeping one interpreter running for multiple requests, instead of
11 launching a separate interpreter for each request made while
12 loading your page.
13
14 ### When FastCGI can help
15
16 This can lead to improvements in latency (responsiveness), for
17 example if:
18
19 -   The language you are using takes a long time to start an
20     interpreter, or to load libraries you are importing
21 -   You have a script which has significant repetitive launching
22     overhead
23 -   Your scripts make the same, very large, database requests which
24     (while cached by MySQL) still have to be read from memory and
25     possibly cross an internal network
26 -   You are using a web framework like CherryPy, Turbogears,
27     Pylons, or Ruby on Rails which prefers to run like a daemon
28
29 ### Caveats
30
31 There are some complications however:
32
33 -   The script in not guaranteed to die unless it kills itself
34 -   If you update your fcgi script, the old process (using the old
35     script) may still be running with outdated code
36 -   The Scripts service automatically load-balances scripts across
37     servers, just like regular CGI; new scripts may not even be
38     launched on the servers the old scripts are running on, and thus
39     cannot “kill all older versions of myself” since they may be on
40     different servers
41
42 The canonical way around these issues is to have the script watch
43 the timestamps of the files it has loaded, and kill itself if a
44 modification timestamp changes. If you are using a framework such
45 as CherryPy, this is already handled for you.
46
47 (Minor note: This method lends itself to race conditions on very
48 short timescales during an update. It is also expensive if you are
49 watching many files. Other solutions include updating a single
50 “watch me” file with a version or hash after every update, or using
51 transactional semantics. This is almost never an issue however.)
52
53 ### Enabling FastCGI
54
55 FastCGI is enabled by default for files with the **.fcgi**
56 extension. To enable FastCGI for another extension, such as .rb,
57 add the following lines to a plain text file named **.htaccess** in
58 the top directory of your website:
59
60 **<Files \*.rb\>  
61   SetHandler fcgid-script  
62 </Files\>**
63
64 To enable FastCGI for another languages, change **rb** to your
65 language’s extension (e.g., **php**, **pl**, etc.)
66
67
68