これは Mac OS X 10.4.7 上の Turbogears 0.9.9 と 1.1a で動作することが確かめられました (つまり、 1.0b リリースでは動かない理由はありません)。
TGにプロキシするためにNGINXをどう使うかを説明する Turbogears trac wiki と、近くの FastCGI の例 ページから情報が引き出され、後者はPHP/FCGIプロセスを詳しく述べています。
これは NGINX/FCGI/Turbogears のためのものです。
Substitute thoughout with the values relevant to your own set-up:
${HOST} = localhost
- (or whatever you choose)${PORT} = 8080
- (or whatever you choose)${NGINX} = /usr/local/nginx
- location of NGINX installation${PROJECTBASE} /opt/projects/wiki20
- location of Turbogears project${PROJECTNAME} wiki20
2つのファイルが生成される必要があります: ${NGINX}/scripts/fcgi.py
と ${NGINX}/scripts/${PROJECTNAME}.fcgi
.
${NGINX}/scripts/fcgi.py
を生成するには:
$ mkdir ${NGINX}/scripts
$ curl -o ${NGINX}/scripts/fcgi.py http://www.saddi.com/software/py-lib/py-lib/fcgi.py
${NGINX}/scripts/${PROJECTNAME}.fcgi
... を生成するには
以下を ${NGINX}/scripts/${PROJECTNAME}.fcgi
にコピー&ペーストします。ファイルを編集し、"USER EDIT SECTION"
に進み、${PROJECTBASE}
と ${PROJECTNAME}
をプロジェクトのために対応する値に置き換えます。
#!/usr/bin/python
#
# File name: project.fcgi
#
# This module provides the glue for running TurboGears applications behind
# FastCGI-enabled web servers. The code in this module depends on the fastcgi
# module downloadable from here:
#
# http://www.saddi.com/software/py-lib/py-lib/fcgi.py
#
# NOTE: The fcgi.py file needs to be placed in a location that is on the
# system path, such as the same the directory as the tg_fastcgi.py file
# or in the base directory of the TG app code.
#
# To configure this module, please edit the three variables in the "USER EDIT
# SECTION" before starting the TG application. Also remember to edit the
# top of this file with the correct Python installation information.
import cherrypy
import sys
import os
from os.path import *
import pkg_resources
import turbogears
pkg_resources.require("TurboGears")
# -- START USER EDIT SECTION
# -- Users must edit this section --
code_dir = '${PROJECTBASE}' # (Required) The base directory of the TG app code.
root_class_name = '${PROJECTNAME}.controllers.Root' # (Required) The fully qualified Root class name.
project_module_name = '${PROJECTNAME}.config' # (Required) The config module name.
log_dir = '' # (Optional) The log directory. Default = code_dir.
# -- END USER EDIT SECTION
class VirtualPathFilter(object):
def on_start_resource(self):
if not cherrypy.config.get('virtual_path_filter.on', False):
return
prefix = cherrypy.config.get('virtual_path_filter.prefix', '')
if not prefix:
return
path = cherrypy.request.object_path
if path == prefix:
path = '/'
elif path.startswith(prefix):
path = path[len(prefix):]
else:
raise cherrypy.NotFound(path)
cherrypy.request.object_path = path
def tg_init():
""" Checks for the required data and initializes the application. """
global code_dir
global root_class_name
global log_dir
global project_module_name
last_mark = 0
# Input checks
if not code_dir or not isdir(code_dir):
raise ValueError("""The code directory setting is missing.
The fastcgi code will be unable to find
the TG code without this setting.""")
if not root_class_name:
raise ValueError("""The fully qualified root class name must
be provided.""")
last_mark = root_class_name.rfind('.')
if last_mark < 1 or last_mark + 1 == len(root_class_name):
raise ValueError("""The user-defined class name is invalid.
Please make sure to include a fully
qualified class name for the root_class
value (e.g. wiki20.controllers.Root).""")
sys.path.append(code_dir)
# Change the directory so the TG log file will not be written to the
# web app root.
if log_dir and isdir(log_dir):
os.chdir(log_dir)
else:
os.chdir(code_dir)
log_dir = code_dir
sys.stdout = open(join(log_dir, 'stdout.log'),'a')
sys.stderr = open(join(log_dir, 'stderr.log'),'a')
if exists(join(code_dir, "setup.py")):
turbogears.update_config(configfile=join(code_dir, "dev.cfg"),modulename=project_module_name)
else:
turbogears.update_config(configfile=join(code_dir, "prod.cfg"),modulename=project_module_name)
# Set environment to production to disable auto-reload and
# add virutal path information.
cherrypy.config.update({
'global': {'server.environment': 'production'},
'/' : { 'virtual_path_filter.on' : True,
'virtual_path_filter.prefix' : '/bel.fcgi' }
})
# Parse out the root class information for Cherrypy Root class.
package_name = root_class_name[:last_mark]
class_name = root_class_name[last_mark+1:]
_temp = __import__(package_name, globals(), locals(), [class_name], -1)
Root = getattr(_temp, class_name)
Root._cp_filters = [VirtualPathFilter()]
cherrypy.root = Root()
# Main section -
# Initialize the application, then start the server.
tg_init()
from fcgi import WSGIServer
cherrypy.server.start(initOnly=True, serverClass=None)
from cherrypy._cpwsgi import wsgiApp
WSGIServer(application=wsgiApp).run()
(使用しているどちらかの)${PROJECTBASE}/dev.cfg
あるいは ${PROJECTBASE}/prod.cfg
ファイルを編集し、server.socket_port
の指定をコメントアウトし、${PORT}
を選択した値に置き換えます (ポート上でそれ以外何も実行していないようにします。Tomcatのデフォルトは8080で、Jettyもそうです。Save yourself some time and check first with a telnet localhost 8080
, you should see Connection refused).
prod/dev.cfgの関係する行は:
server.socket_port=${PORT}
lighttpd の "spawn-fcgi" スクリプトが便利です: ダウンロードし、コンパイルし、lighttpdをインストールします。そして(${HOST}
および ${PORT}
値を適切に置き換え)以下を実行します:
/usr/local/bin/spawn-fcgi -a ${HOST} -p ${PORT} -u nobody -f ${NGINX}/scripts/${PROJECTNAME}.fcgi
以下を${NGINX}/conf/fastcgi_params
に保存します
#fastcgi.conf
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
以下を${HOST}
と ${PORT}
を適切に変更して、${NGINX}/conf/nginx.conf
設定ファイルのserverセクションに追加します:
# static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root ${PROJECTBASE}/${PROJECTNAME}/static;
}
location = /favicon.ico {
root ${PROJECTBASE}/${PROJECTNAME}/static/images;
}
# pass all requests to FastCGI TG server listening on ${HOST}:${PORT}
#
location / {
fastcgi_pass ${HOST}:${PORT};
fastcgi_index index;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include conf/fastcgi_params;
}
${NGINX}/sbin/nginx
を使ってNGINXを開始します。ブラウザをhttp://${HOST}:${PORT}/
に向けると、TurboearsプロジェクトがFastCGIを使って提供されるはずです。そうであれば...おめでとうございます。
Basic but usefully free http://www.hpl.hp.com/research/linux/httperf/
幸運を祈ります。
注意
私にとって0.0.0.0
がうまく動作したため、IPアドレスをそれにしています。127.0.0.1
は動作しませんでした。0.0.0.0:8080
に接続することが困難であれば、以下が別の選択肢になります: localhost:8080
, 127.0.0.1:8080
.