CoreServerのPythonをFastCGIで動かす

PHP, Database, XREA.COM, LightWeight Language, Python, Perl, Ruby

08:20:19, by admin Email , 173 words, 6827 views   Japanese (JP) del.icio.us

一応いけるみたい。

(ノ・・)ン。。。。。。(((●コロコロッ

XREA.COMの新しい共有レンタルサーバCoreServer
リーズナブルでハイスペックなにくいやつ。
PHPもApacheモジュール版が使えるので、
お隣さん調査を慎重にすれば最強のレンサバといえる。

PythonはCGIでのみ利用可能だが、
CoreServerには他のレンタルサーバに無いApacheモジュールが入っている。
それは
mod_fcgi

FastCGIの利用が出来るApacheモジュールで、
これを利用すればCGIを高速化することが出来る。

FastCGIとは?
FastCGI is a language independent, scalable, open extension to CGI that provides high performance without the limitations of server specific APIs.

CGIでは毎回アクセスの度にプロセスの生成と削除が行われる。
その部分を常駐することで省略し高速化するシステム。
アクセスが多ければその差は大きく変わってくる。


PerlやRuby, Pythonは動作させるのにCGI起動という制約条件が発生する。
また、PHPもCoreServerではSafe Mode動作という成約がある。
これらをFastCGIに切り替えることにより、
高速動作させたり、自由度を上げたりと恩恵を受けることが出来る。


ということで、
今回はPythonを使ってFaxtCGI起動してみることにした。
とりあえず動かすことを目的にしている。
Pylonsやdjango, turbogearsなどのフレームワークを利用する場合は、
用意されているマウントポイントやAPI、別コンポーネントなどを経由することになる。
それはまた別の機会にしよう。



一応言っておくが、
以降の内容は自己責任でよろしく。

[More:]



mod_fcgiは初めからインストールされているので、
インストール作業は不要。

●fcgi.pyをインストール
すでに作成済みのsite-packageにfcgi.pyをインストールする。
site-packagesが未作成の人は過去記事をどうぞ。
easi_installのインストール仮定で作成される。
さくらインターネットにeasy_installをインストールする

fcgi.pyのインストールとは名ばかりに、
PYTHONPATHの通っているsite-packagesにファイルをコピーするだけ。
$ wget -P ~/local/lib/python2.4/site-packages http://alldunn.com/python/fcgi.py


●マウントポイント作成
CGI経由で起動するプログラムを作成する。
~/public_html/dispatch.fcgi
#!/usr/local/bin/python -S
# coding:UTF-8

import cgitb; cgitb.enable()
import sys; sys.setdefaultencoding('UTF-8')
import site; site.addsitedir('/virtual/xxx/local/lib/python2.4/site-packages')

def myapp(environ, start_response):
  start_response('200 OK', [('Content-Type', 'text/plain')])
  return ['Hello World!\n']

if __name__ == '__main__':
  from fcgi import WSGIServer
  WSGIServer(myapp).run()



●ハンドラ登録
.htaccessにfastcgiのハンドラを登録する。
DirectoryIndex dispatch.fcgi
AddHandler fastcgi-script .fcgi
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-D
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ /dispatch.fcgi/$1 [QSA,L]




あとはWebからアクセスすればOK。


Trackback address for this post:

http://hain.jp/htsrv/trackback.php/194

Comments, Trackbacks:

No Comments/Trackbacks for this post yet...

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

powered by b2evolution

shinobi

Neighbors
Relative
Favorites
PR

極論istの技術屋を始めて早幾年。 流れの速い業界の波にもまれながらも精一杯生きている様をとくとごらんあれ。

Archives
スポンサー

Latest bookmark
Search

Categories

Who's Online?
Misc
Syndicate this blog XML

Valid XHTML 1.0! Valid CSS! Valid RSS 2.0! Valid Atom 1.0!