TurboGears on ubuntu
TurboGearsは、まだドキュメントの管理がしっかりできていない。特に、運用環境へのデプロイ周りのドキュメントが未整備なので、経験上からの手順をまとめておきたい。
- バージョン
- TurboGears 1.0b1
- ubuntu ja 6.06 (Desktop)
TurboGearsインストール
ubuntu desktopにはデフォルトで開発関連のパッケージがないので、事前にインストールしておく。
$ sudo apt-get install gcc libc6-dev python-dev
TurboGears公式のインストール手順のとおりにインストール。
$ wget http://www.turbogears.org/download/ez_setup.py $ sudo python ez_setup.py -f http://www.turbogears.org/download/index.html --script-dir /usr/local/bin TurboGears
アプリケーション作成
$ tg-admin quickstart Enter project name: testtest Enter package name [testtest]: Do you need Identity in this project? [no] yes
tg-adminコマンドを実行するたびに「Pythonにprofileモジュールがない」という警告メッセージが出るが、これはubuntuにpython-profileがないせいで、動作上は特に問題ないので無視してもかまわない。
ここまでで、TurboGearsのアプリを作る開発環境が整った。アプリを作り終えたら、次章でApacheのmod_python上の実運用環境へデプロイ(配備)する。
Apache2+mod_pythonへのデプロイ
Apache2とmod_pythonパッケージのインストール
$ suto apt-get install libapache2-mod-python
$ wget http://projects.amor.org/misc/svn/modpython_gateway.py $ sudo mv modpython_gateway.py /usr/lib/python2.4/site-packages/
ディレクトリ, URL構成
- ソースファイル
- /usr/lib/python2.4/site-packages/testtest-1.0-py2.4.egg/...
- setup.pyでインストールします。
- データディレクトリ
- /var/local/testtest
- data.sqlite (データベース)
- prod.conf (設定ファイル)
- testtest_modpython.py (起動スクリプト)
- static/*
- /var/local/testtest
ファイルの作成
/var/local/testtest/prod.cfgは、~/testtest/sample-prod.cfgをコピーして、下記の項目を追加or変更する。
データベースファイル
sqlobject.dburi="sqlite:///var/local/testtest/data.sqlite"
自動再ロードの抑止(これをしないと、エラーになります)
autoreload.on=False
ログファイル
[[access_out]]
args="('/var/local/testtest/server.log',)"/var/local/testtest/testtest_modpython.pyは、~/testtest/start-testtest.pyをコピーして、下記の変更を加える。
変更前
...
if len(sys.argv) > 1:
turbogears.update_config(configfile=sys.argv[1],
modulename="testtest.config")
elif exists(join(dirname(__file__), "setup.py")):
turbogears.update_config(configfile="dev.cfg",
modulename="testtest.config")
else:
turbogears.update_config(configfile="prod.cfg",
modulename="testtest.config")
from testtest.controllers import Root
turbogears.start_server(Root())
...
変更後
...
turbogears.update_config(configfile="/var/local/testtest/prod.cfg",
modulename="testtest.config")
from testtest.controllers import Root
cherrypy.root = Root()
cherrypy.server.start(initOnly=True, serverClass=None)
...
インストール
staticディレクトリのコピー
$ sudo cp -r testtest/static /var/local/testtest/
TurboGearsアプリ本体のインストール
$ sudo python setup.py install
Apacheの設定
/etc/apache2/sites-available/testtestを新規作成する。
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
ServerAdmin root@localhost
ServerSignature Off
AddDefaultCharset utf-8
<Location />
SetHandler python-program
PythonPath "sys.path + ['/var/local/testtest']"
PythonHandler modpython_gateway::handler
PythonOption wsgi.application cherrypy._cpwsgi::wsgiApp
PythonFixupHandler testtest_modpython
PythonDebug On
</Location>
Alias /static /var/local/testtest/static
Alias /favicon.ico /var/local/testtest/static/images/favicon.ico
<Location /static>
SetHandler None
</Location>
<Directory /var/local/testtest/static>
Options -ExecCGI -Indexes -Multiviews +FollowSymLinks
Order allow,deny
allow from all
</Directory>
</VirtualHost>パーミッションの設定
chown -R www-data:www-data /var/www
$ sudo a2ensite testtest $ sudo /etc/init.d/apache2 restart
http://localhost/ で作ったアプリにアクセスできるはず。
今回は、/var/local/testtestの下にデータ置き場を作ったりと、いろいろ面倒なことをしたけど、ここまでする必要はなかったかもしれない。
ルート以外のURLで使う
ここでは、http://hostname/のルートをURLとして使うことを前提としているが、http://hostname/subdir/等をルートとしたい場合は、下記の変更が必要。
- prod.cfgに server.webpath="/subdir" を追加。
- 全てのテンプレートのリンク部分を、 /index 等から ${std.url("/index")} にする。
DBのメンテナンス
DBは、~/testtest/devdata.sqliteを/var/local/testtest/へコピーする。
DBの内容を変更するには、
$ sudo tg-admin toolbox --conf=/var/local/testtest/prod.cfg
とすれば、開発環境と同じくWeb上のToolboxを使って編集できる。