在Apache环境下运行/部署Python WEB Applications的三种方法

1, 传统CGI:

vim /var/www/cgi-bin/hello.py[python] view plaincopy

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. print ”Content-type: text/html\n”
  4. print ”Hello World”

chmod a+x hello.py

vim /etc/httpd/conf/httpd.conf
ScriptAlias /cgi-bin/ /var/www/cgi-bin/

2, Mod_Python (http://www.modpython.org/)

wget http://archive.apache.org/dist/httpd/modpython/mod_python-3.3.1.tgz

./configure –with-apxs=/data/apache2221/bin/apxs –with-python=/usr/bin/python
make
make install

vim /etc/httpd/conf.d/python.conf
LoadModule python_module modules/mod_python.so

<Directory /var/www/mod-python>
AddHandler mod_python .py
# mod_python.publisher
PythonHandler mod_python.publisher

PythonDebug On
</Directory>

vim /var/www/mod-python/mod_python_publisher.py[python] view plaincopy

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from mod_python import apache
  4. def index(req):
  5.     req.log_error(‘handler’)
  6.     req.content_type = ’text/html’
  7.     req.send_http_header()
  8.     req.write(‘<html><head><title>Testing mod_python</title></head><body>’)
  9.     req.write(‘Hello World! – mod_python.publisher’)
  10.     req.write(‘</body></html>’)

<Directory /var/www/mod-python>
AddHandler mod_python .py
# custom handler
PythonHandler mod_python_handler

PythonDebug On
</Directory>

vim /var/www/mod-python/mod_python_handler.py[python] view plaincopy

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from mod_python import apache
  4. def handler(req):
  5.     req.log_error(‘handler’)
  6.     req.content_type = ’text/html’
  7.     req.send_http_header()
  8.     req.write(‘<html><head><title>Testing mod_python</title></head><body>’)
  9.     req.write(‘Hello World! – custom handler’)
  10.     req.write(‘</body></html>’)
  11.     return apache.OK

3, Mod_wsgi (http://code.google.com/p/modwsgi/)
wget http://modwsgi.googlecode.com/files/mod_wsgi-3.3.tar.gz

./configure –with-apxs=/data/apache2221/bin/apxs –with-python=/usr/bin/python
make
make install

vim /data/apache2221/conf/httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so

WSGIScriptAlias /wsgi/ /var/www/wsgi/
<Directory “/var/www/wsgi”>
Order allow,deny
Allow from all
</Directory>

vim /var/www/wsgi/hello.py[python] view plaincopy

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. def application(environ, start_response):
  4.     status = ’200 OK’
  5.     content_type = ’text/html’
  6.     output = [‘Hello World’]
  7.     response_headers = [(‘Content-type’, content_type)]
  8.     start_response(status, response_headers)
  9.     return output