Python apache2 + mod_wsgi で Pylons を動かす



とりあえずローカルでPylonsが動くようにはなってくれました。

作成したアプリケーションを公開する際、apache上で動いて欲しいので、
debian + apache2 + mod_wsgiな環境でPylonsを動かす方法を調べてみます。

まずは、debianにPylonsをインストール。


[Pylonsインストール]
easy_installが動かなかったので、ez_setup.pyを使ってインストールします。


#wget http://peak.telecommunity.com/dist/ez_setup.py
#python ez_setup.py Pylons==0.9.6.2


※プロキシ経由でez_setup.pyを動かす場合は、~/.bashrcを編集
#vi ~/.bashrc

末尾に
export HTTP_PROXY=http://プロキシサーバ:ポート

を追記する
#source ~/.bashrc

で設定を反映
【参考URL】
http://python.matrix.jp/modules/setuptools.html


#paster

を実行して
--------------------------------------------------------------
Usage: /usr/bin/paster COMMAND
usage: paster [paster_options] COMMAND [command_options]

options:
--version show program's version number and exit
--plugin=PLUGINS Add a plugin to the list of commands (plugins are Egg
specs; will also require() the Egg)
-h, --help Show this help message

Commands:
create Create the file layout for a Python distribution
help Display help
make-config Install a package and create a fresh config file/directory
points Show information about entry points
serve Serve the described application
setup-app Setup an application, given a config file
--------------------------------------------------------------


と表示されたら、インストール完了です。



次に、mod_wsgiのインストール


[mod_wsgiのインストール]
apt-getする方法がわからなかったので、ソースからインストールしました。
http://code.google.com/p/modwsgi/
ここでソースが公開されています。
※2008/12/01時点の最新版はmod_wsgi-2.3

#wget http://modwsgi.googlecode.com/files/mod_wsgi-2.3.tar.gz
#tar zxvf mod_wsgi-2.3.tar.gz
#cd mod_wsgi-2.3
#./configure
#make
#make install


--------------------------------------------------------------
Libraries have been installed in:
/usr/lib/apache2/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
--------------------------------------------------------------


最後にmod_wsgiを有効にします。
#a2enmod mod_wsgi



これで、Pylonsとmod_wsgiのインストールは完了です。


次にPylonsアプリケーションの公開です。

実は、クライアントで作成したファイルをサーバにアップロードし、
動かそうとしたのですが・・・

raise DistributionNotFound(req)  # XXX put more info here 
pkg_resources.DistributionNotFound: helloworld


というエラーが発生し、動作させることができませんでした。

そのため、サーバでpasterコマンドを実行し、アプリケーションを
作成しています。
いずれ、クライアントで開発したモジュールをサーバで動作させる
方法を調べたいと思います。。。



/usr/local/pylons/

というディレクトリを作成し、
#cd /usr/local/pylons/
#paster create -t pylons helloworld
#cd helloworld/
#paster controller hello


helloworldアプリケーションと、helloコントローラーを作成します。

次に、
#mkdir apache

を実行して、
/usr/local/pylons/helloworld/apache

ディレクトリを作成します。

#vi /usr/local/pylons/helloworld/apache/helloworld.wsgi
import os, sys
sys.path.append('/usr/local/pylons/helloworld')
os.environ['PYTHON_EGG_CACHE'] = '/usr/local/pylons/python-eggs'

from paste.deploy import loadapp
application = loadapp('config:/usr/local/pylons/helloworld/development.ini')



次は、apacheの設定ファイルを作成します。
#vi /etc/apache2/sites-available/helloworld
WSGIScriptAlias /helloworld /usr/local/pylons/helloworld/apache/helloworld.wsgi

<Directory /usr/local/pylons/helloworld/apache>
Order deny,allow
Allow from all
</Directory>



作成した設定ファイルを反映させます。
#a2ensite helloworld


apacheを再起動
#/etc/init.d/apache2 restart



早速、
http://www.example.com/helloworld/
にアクセスしてみると・・・

The EvalException middleware is not usable in a multi-process environment

というエラーが発生し、動きません。

http://cgi39.plala.or.jp/klove/w/k.cgi?page=Diary%2F2008-2-23
ここを参考にさせていただき、

#vi /usr/local/pylons/helloworld/development.ini
[DEFAULT]
#debug = true
debug = false

[app:main]
set debug = false



として、apacheを再起動すると・・・

やっと動いてくれました。




helloコントローラーも動いてくれています。




最終的な階層はこんな感じ。

/usr/local/pylons/helloworld/
 apache/
  helloworld.wsgi
 data/
 docs/
 helloworld/
 helloworld.egg-info/
 development.ini
 ...



【参考URL】
http://code.google.com/p/modwsgi/



もどる