~ubuntu-branches/ubuntu/lucid/twisted-web2/lucid

« back to all changes in this revision

Viewing changes to doc/deployment.txt

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-02-23 00:38:42 UTC
  • mfrom: (0.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060223003842-rcpl8v09a91wfpvr
Tags: 0.1.0.20060222-1ubuntu1
Synchronize with Debian unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Twisted.web2 Deployment
 
2
=======================
 
3
 
 
4
There are a number of possibilities for deploying twisted.web2: as standalone HTTP[S] server, HTTP proxied behind another server, SCGI, FastCGI, or CGI.
 
5
 
 
6
Deploying as a standalone HTTP/HTTPS server is by far the simplest. Unless you have a reason not to, it is recommended that you choose this option. However, many people already run web servers on their computer and are not willing or able to completely blow it away and replace it with twisted.web2. The next best option is to run twisted.web2 as a server proxied behind your existing webserver, using either HTTP or SCGI. 
 
7
 
 
8
Standalone HTTP
 
9
---------------
 
10
For completeness, here is a simple standalone HTTP server again.
 
11
 
 
12
.. pythonfile:: examples/deployment/standalone.tac
 
13
 
 
14
HTTP behind Apache2
 
15
-------------------
 
16
If you use HTTP proxying, you must inform twisted.web2 of the real URL it is being accessed by, or else any URLs it generates will be incorrect. You can do this via the AutoVHostURIRewrite resource when using apache2 as the main server.
 
17
 
 
18
On the apache side, configure as follows. Apache automatically sends the original host in the X-Forwarded-Host header, and the original remote IP address in the X-Forwarded-For header. You must additionally send along the original path, and the original scheme.
 
19
 
 
20
For proxying a subdirectory::
 
21
 
 
22
  <Location /whatever/>
 
23
  ProxyPass http://localhost:8538/
 
24
  RequestHeader set X-App-Location /whatever/
 
25
  RequestHeader set X-App-Scheme http
 
26
 
 
27
  </Location>
 
28
 
 
29
Or, for serving an entire HTTPS virtual host::
 
30
 
 
31
  <VirtualHost myip:443>
 
32
  ServerName example.com
 
33
  ProxyPass / http://localhost:8538/
 
34
  RequestHeader set X-App-Location /
 
35
  RequestHeader set X-App-Scheme https
 
36
  </VirtualHost>
 
37
 
 
38
Now, on the twisted.web2 side
 
39
 
 
40
.. pythonfile:: examples/deployment/apache2.tac
 
41
 
 
42
 
 
43
HTTP behind Apache1
 
44
-------------------
 
45
Apache 1 doesn't provide the X-Forwarded-Host or X-Forwarded-For headers, or the ability to set custom headers in the outgoing proxy request. Therefore, you must provide that information to twisted.web2 directly. This is accomplished by the VHostURIRewrite resource.
 
46
 
 
47
Setup apache as follows::
 
48
 
 
49
  <VirtualHost myip>
 
50
  ServerName example.com
 
51
  ProxyPass /foo/ http://localhost:8538/
 
52
  </VirtualHost>
 
53
  
 
54
And twisted like so
 
55
 
 
56
.. pythonfile:: examples/deployment/apache1.tac
 
57
 
 
58
Because vhost.VHostURIRewrite can exist anywhere in the resource tree, you can have multiple applications running on a single twisted port by making them siblings of a root resource and referencing their full path in the ProxyPass directive.
 
59
 
 
60
Setup apache as follows::
 
61
 
 
62
  <VirtualHost foo.myhostname.com>
 
63
  ProxyPass / http://localhost:8538/foo/
 
64
  ServerName example.com
 
65
  </VirtualHost>
 
66
 
 
67
  <VirtualHost bar.myhostname.com>
 
68
  ProxyPass / http://localhost:8538/bar/
 
69
  ServerName example.com
 
70
  </VirtualHost>
 
71
 
 
72
And twisted like so
 
73
 
 
74
.. pythonfile:: examples/deployment/apache1_twohosts.tac
 
75
 
 
76
SCGI
 
77
----
 
78
SCGI is an alternative to HTTP proxying. SCGI should work instead of HTTP proxying from servers which support it. Additionally, if all you have access to from the web server is CGI, but are able to run long-running processes, you can use the cgi2scgi_ C program to channel CGI requests to your twisted.web2 SCGI port. This won't be as efficient as mod_scgi or http proxying, but it will be much better than using twisted directly as a CGI.
 
79
 
 
80
FIXME:Someone who has installed mod_scgi in apache should write a bit on it.
 
81
 
 
82
Configure Twisted as follows
 
83
 
 
84
.. pythonfile:: examples/deployment/scgi.tac
 
85
 
 
86
FastCGI
 
87
-------
 
88
FastCGI is another popular way to run a web application. Blah blah.
 
89
 
 
90
CGI
 
91
---
 
92
CGI is the worst possible deployment environment, yet in some cases it may be all that is possible. It allows only a single request to be served from a process, so any kind of in-memory storage is impossible. Also, the overhead of starting up a new python interpreter for every request can get quite high. You should only consider using it if your hosting provider does not allow you to keep a process running.
 
93
 
 
94
However, if it's your only choice, you can deploy a twisted.web2 app using it. Unlike the other examples, where we create a .tac file for running with twistd, in this case, a standalone python script is necessary
 
95
 
 
96
.. python::
 
97
  #!/usr/bin/env python
 
98
  from twisted.web2 import channel, server, static
 
99
  toplevel = static.File("/tmp")
 
100
  site = server.Site(toplevel)
 
101
  channel.startCGI(site)
 
102
 
 
103
.. _cgi2scgi: http://www.mems-exchange.org/software/scgi/scgi-1.2.tar.gz/scgi-1.2/cgi2scgi.c
 
 
b'\\ No newline at end of file'