Linux/Unix

□未翻訳

□翻訳中

□翻訳完了(Omi Chiba)

■レビュー(中垣健志)

Linux/Unix

本番デプロイへの第一歩

One Step Production Deployment

ここではapache+python+mod_wsgi+web2py+postgresqlを最初からインストールする手順を説明します。

Here are some steps to install apache+python+mod_wsgi+web2py+postgresql from scratch.

Ubuntuの場合:

On Ubuntu:

wget http://web2py.googlecode.com/hg/scripts/setup-web2py-ubuntu.sh chmod +x setup-web2py-ubuntu.sh sudo ./setup-web2py-ubuntu.sh

Fedoraの場合:

On Fedora:

wget http://web2py.googlecode.com/hg/scripts/setup-web2py-fedora.sh chmod +x setup-web2py-fedora.sh sudo ./setup-web2py-fedora.sh

どちらのスクリプトも標準設定で動作しますが、どのLinuxインストレーションも若干仕様が異なるので、実行する前にこれらのスクリプトのコードを確認して置くようにしてください。Ubuntuについては、ほとんどの設定について以下で説明されています。以下で説明している拡張性の最適化については実施していません。

Both of these scripts should run out of the box, but every Linux installation is a bit different, so make sure you check the source code of these scripts before you run them. In the case of Ubuntu, most of what they do is explained below. They do not implement the scalability optimizations discussed below.

Apacheセットアップ

Apache setup

この節では、Ubuntu 8.04 Server Editionをプラットフォームとして使用します。設定コマンドは他のDebianベースのLinuxディストリ ビューションによく似ていますが、Fedoraベースのシステム(apt-getの代わりにyumを使う)と若干異なります。

In this section, we use Ubuntu 8.04 Server Edition as the reference platform. The configuration commands are very similar on other Debian-based Linux distribution, but they may differ for Fedora-based systems (which uses yum instead of apt-get).

始めに、次のシェルコマンドを入力して全ての必要なPythonとApacheパッケージをインストールします。

First, make sure all the necessary Python and Apache packages are installed by typing the following shell commands:

1.

2.

3.

4.

5.

6.

7.

8.

sudo apt-get update

sudo apt-get -y upgrade

sudo apt-get -y install openssh-server

sudo apt-get -y install python

sudo apt-get -y install python-dev

sudo apt-get -y install apache2

sudo apt-get -y install libapache2-mod-wsgi

sudo apt-egt -y install libapache2-mod-proxy-html

そして、ApacheのSSLモジュール、proxyモジュール、WSGIモジュールを有効にします:

Then, enable the SSL module, the proxy module, and the WSGI module in Apache:

1.

2.

3.

4.

5.

6.

sudo ln -s /etc/apache2/mods-available/proxy_http.load \

/etc/apache2/mods-enabled/proxy_http.load

sudo a2enmod ssl

sudo a2enmod proxy

sudo a2enmod proxy_http

sudo a2enmod wsgi

SSLフォルダを作成し、SSL証明書をその中に配置します:

Create the SSL folder, and put the s inside it:

1.

sudo mkdir /etc/apache2/ssl

SSL証明書はverisign.comのような認証局から取得する必要がありますが、テスト目的では、参照83にある手順に従って自身署名した証明書を作成することができます。

You should obtain your SSL certificates from a trusted Certificate Authority such as verisign.com, but, for testing purposes, you can generate your own self-signed certificates following the instructions in ref.83

そしてwebサーバを再起動します:

Then restart the web server:

1.

sudo /etc/init.d/apache2 restart

Apache設定ファイル:

The Apache configuration file is:

1.

/etc/apache2/sites-available/default

Apacheログ:

The Apache logs are in:

1.

/var/log/apache2/

mod_wsgi

mod_wsgi

上記のwebサーバをインストールしたマシンにweb2pyをダウンロードして解凍します。

Download and unzip web2py source on the machine where you installed the web server above.

例えば、web2pyを/users/www-data/配下にインストールし、www-dataユーザとwww-dataグループに権限を与えます。この手順は次のシェルコマンドで実行できます。

Install web2py under /users/www-data/, for example, and give ownership to user www-data and group www-data. These steps can be performed with the following shell commands:

1.

2.

3.

4.

cd /users/www-data/

sudo wget http://web2py.com/examples/static/web2py_src.zip

sudo unzip web2py_src.zip

sudo chown -R www-data:www-data /user/www-data/web2py

web2pyをmod_wsgiでセットアップするには、新しいApache設定ファイルを作成して:

To set up web2py with mod_wsgi, create a new Apache configuration file:

1.

/etc/apache2/sites-available/web2py

次のコードを追加します:

and include the following code:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.

28.

29.

30.

31.

32.

33.

34.

<VirtualHost *:80>

ServerName web2py.example.com

WSGIDaemonProcess web2py user=www-data group=www-data \

display-name=%{GROUP}

WSGIProcessGroup web2py

WSGIScriptAlias / /users/www-data/web2py/wsgihandler.py

<Directory /users/www-data/web2py>

AllowOverride None

Order Allow,Deny

Deny from all

<Files wsgihandler.py>

Allow from all

</Files>

</Directory>

AliasMatch ^/([^/]+)/static/(.*) \

/users/www-data/web2py/applications/$1/static/$2

<Directory /users/www-data/web2py/applications/*/static/>

Order Allow,Deny

Allow from all

</Directory>

<Location /admin>

Deny from all

</Location>

<LocationMatch ^/([^/]+)/appadmin>

Deny from all

</LocationMatch>

CustomLog /private/var/log/apache2/access.log common

ErrorLog /private/var/log/apache2/error.log

</VirtualHost>

Apacheを再起動すると、web2pyへの全てのリクエストはRocket wsgiserverを経由しないで渡されます。

When you restart Apache, it should pass all the requests to web2py without going through the Rocket wsgiserver.

説明すると:

Here are some explanations:

1.

2.

WSGIDaemonProcess web2py user=www-data group=www-data

display-name=%{GROUP}

上のコードは、"web2py.example.com"に関連するデーモンプロセスグループを定義しています。バーチャルホスト内にこれを定義することで、このバーチャルホストだけが同じサーバ上で異なるポートを使用している他のバーチャルホストを含めてWSGIProcessGroupを使って接続できます。"user"と"group"オプションにはweb2pyをセットアップしたディレクトリに書き込み権限があるユーザをセットするべきです。もしApacheを実行するデフォルトユーザにweb2pyインストレーションディレクトリへの書き込み権限を設定していたら、"user"と"group"を設定する必要はありません。"display-name"オプションによってApache webサーバ実行ファイルの名前の代わりに、psに表示されるプロセス名を"(wsgi:web2py)"とすることができます。"processes"や"threads"オプションを指定しないことで、デーモンプロセスグループは単一のプロセス内で15のスレッドを実行します。これは通常ほとんどのサイトで十分過ぎる設定なのでそのまま使用するべきです。もし設定を上書くとしても、"processes=1"は使用しないでください、そうしてしまうと、"wsgi.multiprocess"フラグをチェックするブラウザ上で動作するWSGIデバッグツールが無効になります。これはどのような"processes"オプションの使用方法も単一プロセスの場合でさえフラグをtrueにセットしてしまうにもかかわらず、それらのツールはfalseにセットされていることが前提になるからです。注意:もしアプリケーションコードやサードパーティ性の拡張モジュールがスレッドセーフで無い場合は、"processes=5 threads-1"オプションを使用してください。これは各プロセスのスレッドが一つのデーモンプロセスグループに五つのプロセスを作成します。ガベージコレクトを適切に実施できずにアプリケーションがPythonオブジェクトを放置する場合は、"maximum-requests=1000"を使うことも検討してください。

defines a daemon process group in context of "web2py.example.com". By defining this inside of the virtual host, only this virtual host can access this using WSGIProcessGroup, including any virtual host with the same server name but on a different port. The "user" and "group" options should be set to the user who has write access to the directory where web2py was setup. You do not need to set "user" and "group" if you made the web2py installation directory writable by the default user that Apache runs as. The "display-name" option makes the process name appears in ps output as "(wsgi:web2py)" instead of as name of Apache web server executable. As no "processes" or "threads" options are specified, the daemon process group will have a single process with 15 threads running within that process. This is usually more than adequate for most sites and should be left as is. If overriding it, do not use "processes=1" as doing so will disable any in-browser WSGI debugging tools that check the "wsgi.multiprocess" flag. This is because any use of the "processes" option will cause that flag to be set to true, even a single process, and such tools expect that it be set to false. Note: if your application code or third party extension module is not thread safe, use options "processes=5 threads=1" instead. This will create five processes in the daemon process group where each process is single threaded. You might consider using "maximum-requests=1000" if your application leaks Python objects because it is unable to garbage collect properly.

1.

WSGIProcessGroup web2py

は、全てのWSGIアプリケーションの実行をWSGIDaemonProcessディレクティブで設定されたデーモンプロセスグループに委譲します。

delegates running of all WSGI applications to the daemon process group that was configured using the WSGIDaemonProcess directive.

1.

WSGIScriptAlias / /users/www-data/web2py/wsgihandler.py

は、web2pyアプリケーションをマウントします。この場合はwebサイトのrootにマウントされます。

mounts the web2py application. In this case it is mounted at the root of the web site.

1.

2.

3.

<Directory /users/www-data/web2py>

...

</Directory>

は、ApacheにWSGIスクリプトファイルにアクセスする権限を与えます。

gives Apache permission to access the WSGI script file.

1.

2.

3.

4.

<Directory /users/www-data/web2py/applications/*/static/>

Order Allow,Deny

Allow from all

</Directory>

静的ファイルを検索する際にweb2pyをバイパスするようにApacheを設定します。

Instructs Apache to bypass web2py when searching static files.

1.

2.

3.

<Location /admin>

Deny from all

</Location>

そして、

and

1.

2.

3.

<LocationMatch ^/([^/]+)/appadmin>

Deny from all

</LocationMatch>

adminとappadminへのパブリックアクセスを制限します

blocks public access to admin and appadmin

通常はWSGIスクリプトがある全てのディレクトリにアクセスを許可するだけでいいですが、web2pyは管理者パスワードを含むその他のソースコードが入っているディレクトリにWSGIスクリプトを配置します。技術的にApacheはマップされたURL経由でどのユーザに対しても全てのファイル間を行き来する権限を与えるので、全てのディレクトリを公開することは、セキュリティ上の問題になります。その問題を避けるため、WSGIスクリプトファイルを除くディレクトリ内のファイルで明示的にアクセスを拒否し、より安全にするためにユーザーが.htaccessファイルを上書くことを禁止します。

Normally we would just allow permission to the whole directory where the WSGI script file is located, but web2py places the WSGI script file in a directory which contains other source code, including the admin interface password. Opening up the whole directory would cause security issues, because technically Apache would be given permission to serve all the files up to any user who traversed to that directory via a mapped URL. To avoid security problems, explicitly deny access to the contents of the directory, except for the WSGI script file, and prohibit a user from doing any overrides from a .htaccess file to be extra safe.

完全で、コメント付のApache wsgi設定ファイルは以下にあります:

You can find a completed, commented, Apache wsgi configuration file in:

1.

scripts/web2py-wsgi.conf

この節はmod_wsgi開発者のGraham Dumpletonの協力を得て作成されました。

This section was created with help from Graham Dumpleton, developer of mod_wsgi.

mod_wsgiとSSL

mod_wsgi and SSL

アプリケーション(例えばadminとappadmin)で強制的にHTTPSを利用させるには、SSL証明書と秘密鍵を以下に保存し:

To force some applications (for example admin and appadmin) to go over HTTPS, store the SSL certificate and key files:

1.

2.

/etc/apache2/ssl/server.crt

/etc/apache2/ssl/server.key

Apacheの設定ファイルweb2py.confを編集して以下を追加します:

and edit the Apache configuration file web2py.conf and append:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.

28.

29.

30.

31.

<VirtualHost *:443>

ServerName web2py.example.com

SSLEngine on

SSLCertificateFile /etc/apache2/ssl/server.crt

SSLCertificateKeyFile /etc/apache2/ssl/server.key

WSGIProcessGroup web2py

WSGIScriptAlias / /users/www-data/web2py/wsgihandler.py

<Directory /users/www-data/web2py>

AllowOverride None

Order Allow,Deny

Deny from all

<Files wsgihandler.py>

Allow from all

</Files>

</Directory>

AliasMatch ^/([^/]+)/static/(.*) \

/users/www-data/web2py/applications/$1/static/$2

<Directory /users/www-data/web2py/applications/*/static/>

Order Allow,Deny

Allow from all

</Directory>

CustomLog /private/var/log/apache2/access.log common

ErrorLog /private/var/log/apache2/error.log

</VirtualHost>

Apacheを再起動すると以下にアクセスできますが:

Restart Apache and you should be able to access:

https://www.example.com/admin

https://www.example.com/examples/appadmin

http://www.example.com/examples

以下にはアクセスできません:

but not:

http://www.example.com/admin

http://www.example.com/examples/appadmin

mod_proxy

Unix/LinuxディストリビューションによってはApacheを実行できるが、mod_wsgiをサポートしてないものがあります。この場合、一番簡単な方法はApacheをプロキシとして実行し静的ファイルだけをApacheに処理させることです。

Some Unix/Linux distributions can run Apache, but do not support mod_wsgi. In this case, the simplest solution is to run Apache as a proxy and have Apache deal with static files only.

以下は最低限のApacheの設定です:

Here is a minimalist Apache configuration:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

NameVirtualHost *:80

### deal with requests on port 80

<VirtualHost *:80>

Alias / /users/www-data/web2py/applications

### serve static files directly

<LocationMatch "^/welcome/static/.*">

Order Allow, Deny

Allow from all

</LocationMatch>

### proxy all the other requests

<Location "/welcome">

Order deny,allow

Allow from all

ProxyRequests off

ProxyPass http://localhost:8000/welcome

ProxyPassReverse http://localhost:8000/

ProxyHTMLURLMap http://127.0.0.1:8000/welcome/ /welcome

</Location>

LogFormat "%h %l %u %t "%r" %>s %b" common

CustomLog /var/log/apache2/access.log common

</VirtualHost>

上記のスクリプトは"welcome"アプリケーションだけを公開しています。他のアプリケーションを公開したい場合は、対応する<Location>...</Location>に"welcome"アプリケーションでしたような構文を追加する必要があります。

The above script exposes only the "welcome" application. To expose other applications, you need to add the corresponding <Location>...</Location> with the same syntax as done for the "welcome" app.

スクリプトはweb2pyサーバーがポート8000を使用することを前提にしています。Apacheを再起動する前に、この点を再確認してください。

The script assumes there is a web2py server running on port 8000. Before restarting Apache, make sure this is the case:

1.

nohup python web2py.py -a '<recycle>' -i 127.0.0.1 -p 8000 &

-aオプションでパスワードを指定するか、パスワードの代わりに"<recycle>"パラメータを指定することができます。後者の場合は、前回保存されたパスワードが再利用されシェルの履歴にパスワードが保存されることはありません。

You can specify a password with the -a option or use the "<recycle>" parameter instead of a password. In the latter case, the previously stored password is reused and the password is not stored in the shell history.

"<ask>"パラメータを利用しパスワード入力を表示させることもできます。

You can also use the parameter "<ask>", to be prompted for a password.

nohupコマンドはシェルが終了した時にサーバが落ちないようにします。nohupnohup.outに全てのログを出力します。

The nohup commands makes sure the server does not die when you close the shell. nohup logs all output into nohup.out.

adminとappadminにHTTPSを強制的に利用させるには代わりに次のApacheの設定ファイルを使用します。

To force admin and appadmin over HTTPS use the following Apache configuration file instead:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.

28.

29.

30.

31.

32.

33.

34.

35.

36.

37.

38.

39.

40.

41.

NameVirtualHost *:80

NameVirtualHost *:443

### deal with requests on port 80

<VirtualHost *:80>

Alias / /usres/www-data/web2py/applications

### admin requires SSL

<LocationMatch "^/admin">

SSLRequireSSL

</LocationMatch>

### appadmin requires SSL

<LocationMatch "^/welcome/appadmin/.*">

SSLRequireSSL

</LocationMatch>

### serve static files directly

<LocationMatch "^/welcome/static/.*">

Order Allow,Deny

Allow from all

</LocationMatch>

### proxy all the other requests

<Location "/welcome">

Order deny,allow

Allow from all

ProxyPass http://localhost:8000/welcome

ProxyPassReverse http://localhost:8000/

</Location>

LogFormat "%h %l %u %t "%r" %>s %b" common

CustomLog /var/log/apache2/access.log common

</VirtualHost>

<VirtualHost *:443>

SSLEngine On

SSLCertificateFile /etc/apache2/ssl/server.crt

SSLCertificateKeyFile /etc/apache2/ssl/server.key

<Location "/">

Order deny,allow

Allow from all

ProxyPass http://localhost:8000/

ProxyPassReverse http://localhost:8000/

</Location>

LogFormat "%h %l %u %t "%r" %>s %b" common

CustomLog /var/log/apache2/access.log common

</VirtualHost>

web2pyをmod_proxyのホスティングで動かす場合、管理画面は無効にしておく必要があります。そうしないと他のユーザに公開されてしまいます。

The administrative interface must be disabled when web2py runs on a shared host with mod_proxy, or it will be exposed to other users.

Linux Daemoとして起動

Start as Linux Daemon

mod_wsgiを使用する場合を除き、web2pyがLinuxのデーモンとして起動/停止/再起動するように設定するべきで、そうすることでコンピュータのブート時に自動で起動されます。

Unless you are using mod_wsgi, you should setup the web2py server so that it can be started/stopped/restarted as any other Linux daemon, and so it can start automatically at the computer boot stage.

これを設定する方法はLinux/Unixディストリビューションごとによって異なります。

The process to set this up is specific to various Linux/Unix distributions.

web2pyフォルダにはこの設定のために二つのスクリプトが用意されています。

In the web2py folder, there are two scripts which can be used for this purpose:

1.

2.

scripts/web2py.ubuntu.sh

scripts/web2py.fedora.sh

Ubuntuやその他のDebianベースのLinuxディストリビューションでは、"web2py.ubunu.sh"を編集し"usr/lib/web2py"パスをあなたのweb2pyインストレーションパスに置き換え、次のシェルコマンドでふぁいを適切なフォルダに移動し、スタートアップサービスに登録して、起動します:

On Ubuntu, or other Debian-based Linux distribution, edit "web2py.ubuntu.sh" and replace the "/usr/lib/web2py" path with the path of your web2py installation, then type the following shell commands to move the file into the proper folder, register it as a startup service, and start it:

1.

2.

3.

sudo cp scripts/web2py.ubuntu.sh /etc/init.d/web2py

sudo update-rc.d web2py defaults

sudo /etc/init.d/web2py start

Fedoraやその他のFedoraベースのディストリビューションでは、"web2py.fedora.sh"を編集し"usr/lib/web2py"パスをあなたのweb2pyインストレーションパスに置き換え、次のシェルコマンドでファルを適切なフォルダに移動し、スタートアップサービスに登録して、起動します:

On Fedora, or any other distributions based on Fedora, edit "web2py.fedora.sh" and replace the "/usr/lib/web2py" path with the path of your web2py installation, then type the following shell commands to move the file into the proper folder, register it as a startup service and start it:

1.

2.

3.

sudo cp scripts/web2py.fedora.sh /etc/rc.d/init.d/web2pyd

sudo chkconfig --add web2pyd

sudo service web2py start

Lighttpd

Lighttpd

FastCGI

fcgihandler

次のシェルコマンドでUbuntuやその他のDebianベースLinuxディストリビューションにLighttpdをインストールできます。

You can install Lighttpd on a Ubuntu or other Debian-based Linux distribution with the following shell command:

1.

apt-get -y install lighttpd

インストールされたら、/etc/rc.localを編集しfcgi web2pyバックグランドプロセスを作成します

Once installed, edit /etc/rc.local and create a fcgi web2py background process

1.

cd /var/www/web2py && sudo -u www-data nohup python fcgihandler.py &

そして、Lighttpd設定ファイルを編集する必要があります。

Then, you need to edit the Lighttpd configuration file

1.

/etc/lighttpd/lighttpd.conf

これ、上記のプロセスで作成されたソケットを見つけることができます。設定ファイルに以下のように記述します:

so that it can find the socket created by the above process. In the config file, write something like:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

18.

19.

20.

21.

22.

23.

24.

25.

26.

27.

28.

29.

30.

31.

32.

33.

34.

server.modules = (

"mod_access",

"mod_alias",

"mod_compress",

"mod_rewrite",

"mod_fastcgi",

"mod_redirect",

"mod_accesslog",

"mod_status",

)

server.port = 80

server.bind = "0.0.0.0"

server.event-handler = "freebsd-kqueue"

server.error-handler-404 = "/test.fcgi"

server.document-root = "/users/www-data/web2py/"

server.errorlog = "/tmp/error.log"

fastcgi.server = (

"/handler_web2py.fcgi" => (

"handler_web2py" => ( #name for logs

"check-local" => "disable",

"socket" => "/tmp/fcgi.sock"

)

),

)

$HTTP["host"] = "(^|\.)example\.com$" {

server.document-root="/var/www/web2py"

url.rewrite-once = (

"^(/.+?/static/.+)$" => "/applications$1",

"(^|/.*)$" => "/handler_web2py.fcgi$1",

)

}

次に文法エラーをチェックします。

Now check for syntax errors:

1.

lighttpd -t -f /etc/lighttpd/lighttpd.conf

そして次のようにwebサーバーを(再)起動します:

and (re)start the web server with:

1.

/etc/init.d/lighttpd restart

FastCGIはweb2pyをIPソケットで無く、Unixソケットにバインドする点に注意してください:

Notice that FastCGI binds the web2py server to a Unix socket, not to an IP socket:

1.

/tmp/fcgi.sock

これがLighttpdがHTTP送受信をおこなうところです。UnixソケットはIPソケットより軽量で、それがLighttpd+FastCGI+web2pyが高速に動作する一つの理由です。Apacheの場合は、静的ファイルLighttpdに直接処理させ、アプリケーションを強制的にHTTPS経由にする設定も可能です。詳細はLighttpdのドキュメントを参照してください。

This is where Lighttpd forwards the HTTP requests to and receives responses from. Unix sockets are lighter than Internet sockets, and this is one of the reasons Lighttpd+FastCGI+web2py is fast. As in the case of Apache, it is possible to setup Lighttpd to deal with static files directly, and to force some applications over HTTPS. Refer to the Lighttpd documentation for details.

この節の例はweb2pyslicesに投稿されたJohn Heenanの記事から引用されました。

Examples in this section were taken from John Heenan's post in web2pyslices.

web2pyをFastCGIのホスティングで動かす場合、管理画面は無効にしておく必要があり、そうしないと他のユーザに公開されてしまいます。

The administrative interface must be disabled when web2py runs on a shared host with FastCGI, or it will be exposed to the other users.

mod_pythonのホスティング

Shared Hosting with mod_python

しばしば、特にホスティング環境で、Apche設定ファイルを直接編集する権限が無い場合があります。この本を書いている現在、mod_wsgiの登場でメンテナンスされていないが、ほとんどのホストがmod_pythonを動かしています。

There are times, specifically on shared hosts, when one does not have the permission to configure the Apache config files directly. At the time of writing most of these hosts still run mod_python even if it is not maintained any more in favor of mod_wsgi.

このような環境でもweb2pyを動かすことができます。ここではその設定方法の例をお見せします。

You can still run web2py. Here we show an example of how to set it up.

web2pyの中身を"htdocs"フォルダにいれます。

Place contents of web2py into the "htdocs" folder.

web2pyフォルダ内に、次の内容で"web2py_modpython.py"を作成します。

In the web2py folder, create a file "web2py_modpython.py" file with the following contents:

1.

2.

3.

4.

5.

6.

from mod_python import apache

import modpythonhandler

def handler(req):

req.subprocess_env['PATH_INFO'] = req.subprocess_env['SCRIPT_URL']

return modpythonhandler.handler(req)

次の内容で".htaccess"ファイルを作成/更新します:

Create/update the file ".htaccess" with the following contents:

1.

2.

3.

SetHandler python-program

PythonHandler web2py_modpython

#PythonDebug On

この例はNiktarによって提供されました。

This example was provided by Niktar.

CherokeeとFastCGI

Cherokee with FastGGI

Cherokee

FastCGI

Cherokeeは高速はwebサーバで、web2pyのように、設定にAJAXが有効なwebベースの管理画面を提供します。管理画面はPythonで書かれており、ほとんどの変更はサーバーの再起動を必要としません。

Cherokee is a very fast web server and, like web2py, it provides an AJAX-enabled web-based interface for its configuration. Its web interface is written in Python. In addition, there is no restart required for most of the changes.

こちらがweb2pyをCherokeeでセットアップするのに必要な手順です:

Here are the steps required to setup web2py with Cherokee:

Cherokee86をダウンロード

Download Cherokee86

tarの展開、ビルド、そしてインストール:

Untar, build, and install:

1.

2.

3.

4.

tar -xzf cherokee-0.9.4.tar.gz

cd cherokee-0.9.4

./configure --enable-fcgi && make

make install

web2pyを通常通り起動し、少なくとも一つの"applications"フォルダを作成しておいてください。

Start web2py normally at least once to make sure it creates the "applications" folder.

次のコードで"startweb2py.sh"という名前のシェルコマンドを書いてください:

Write a shell script named "startweb2py.sh" with the following code:

1.

2.

3.

#!/bin/bash

cd /var/web2py

python /var/web2py/fcgihandler.py &

そして、スクリプトに実行権限を与えて実行してください。これでweb2pyはFastCGIハンドラで起動します。

and give the script execute privileges and run it. This will start web2py under FastCGI handler.

Cherokeeとchroke-adminを起動:

Start Cherokee and cherokee-admin:

1.

2.

sudo nohup cherokee &

sudo nohup cherokee-admin &

デフォルトで、chrokee-adminはローカルのポート9090だけを使用します。もしそのマシンへ完全な物理的アクセス権があれば問題ではありません。もうしそうでない場合は、次のオプションを利用して、強制的にIPアドレスとポートにバインドします。

By default, cherokee-admin only listens at local interface on port 9090. This is not a problem if you have full, physical access on that machine. If this is not the case, you can force it to bind to an IP address and port by using the following options:

1.

2.

-b, --bind[=IP]

-p, --port=NUM

またはSSHポートフォワード(より安全で、推奨される)をします:

or do an SSH port-forward (more secure, recommended):

1.

ssh -L 9090:localhost:9090 remotehost

ブラウザで"http://localhost:9090"を開き、もし全ての設定がokであれば、cherokee-admingが開きます。

Open "http://localhost:9090" in your browser. If everything is ok, you will get cherokee-admin.

chrokee-admin webインターフェイスで、"info sources"をクリックします。"Local Interpreter"をクリックします。次のコードを記述して"Add New"をクリックします。

In cherokee-admin web interface, click "info sources". Choose "Local Interpreter". Write in the following code, then click "Add New".

1.

2.

3.

Nick: web2py

Connection: /tmp/fcgi.sock

Interpreter: /var/web2py/startweb2py.sh

最後に、次の残作業を実行します:

Finally, perform the following remaining steps:

    • "Virtual Servers"をクリックし、"Default”をクリック。

    • "Behaviorをクリックし、その下の、"default"をクリック。

    • リストボックスから"List and Send"の代わりに"FastCGI"を選択。

    • 一番下で、"web2py"を"Application Server"として選択。

    • 全てのチェックボックス(Allow-x-sendfileは飛ばしても良い)をチェックします。もし警告表示がでたら、どれか一つのチェックボックスを無効にしてから有効にします。(そうすることでアプリケーションサーバのパラメータを自動で再送信します。バグで上手く行かないときがあります。)

    • ブラウザで"http://あなたのサイト"を開くと"Welcom to web2py"が表示されます。

    • Click "Virtual Servers", then click "Default".

    • Click "Behavior", then, under that, click "default".

    • Choose "FastCGI" instead of "List and Send" from the list box.

    • At the bottom, select "web2py" as "Application Server"

    • Put a check in all the checkboxes (you can leave Allow-x-sendfile). If there is a warning displayed, disable and enable one of the checkboxes. (It will automatically re-submit the application server parameter. Sometimes it doesn't, which is a bug).

    • Point your browser to "http://yoursite", and "Welcome to web2py" will appear.

PostgreSQL

PostgreSQLは無償のオープンソースのデータベースで、トランザクションの多い本番環境で使用されており、例を挙げると、.orgドメイン名を保存するデータベースに使用されて、何百テラバイトに拡張できることが証明されています。非常に高速で安定したトランザクションサポートを持ち、多くのデータベース保守タスクから管理者を解放するauto-vacuumという機能があります。

PostgreSQL is a free and open source database which is used in demanding production environments, for example, to store the .org domain name database, and has been proven to scale well into hundreds of terabytes of data. It has very fast and solid transaction support, and provides an auto-vacuum feature that frees the administrator from most database maintenance tasks.

Ubuntuや他のDebianベースLinuxディストリビューションでは、PostgreSQLとそのPython APIを間単にインストールできます:

On an Ubuntu or other Debian-based Linux distribution, it is easy to install PostgreSQL and its Python API with:

1.

2.

sudo apt-get -y install postgresql

sudo apt-get -y install python-psycopg2

webサーバとデータベースは異なるマシンで運用したほうが賢明です。この場合、webサーバを動かしているマシンは安全な内部(物理)ネットワーク、またはSSLトンネルでデータベースサーバと接続されるべきです。

It is wise to run the web server(s) and the database server on different machines. In this case, the machines running the web servers should be connected with a secure internal (physical) network, or should establish SSL tunnels to securely connect with the database server.

PostgreSQLの設定ファイルを編集します

Edit the PostgreSQL configuration file

1.

sudo nano /etc/postgresql/8.4/main/postgresql.conf

次の二行を記述します

and make sure it contains these two lines

1.

2.

3.

4.

5.

...

track_counts = on

...

autovacuum = on # Enable autovacuum subprocess? 'on'

...

データベースサーバを起動します:

Start the database server with:

1.

sudo /etc/init.d/postgresql restart

PostgreSQLサーバーの再起動時に、どのポートで実行されているか表示されます。データベースサーバが複数で構成される場合を除き、5432のはずです。

When restarting the PostgreSQL server, it should notify which port it is running on. Unless you have multiple database servers, it should be 5432.

PostgreSQLのログはここにあります:

The PostgreSQL logs are in:

1.

/var/log/postgresql/

データベースサーバが立ち上がって動き出したら、web2pyアプリケーションに必要なユーザとデータベースを作成します:

Once the database server is up and running, create a user and a database so that web2py applications can use it:

1.

2.

3.

4.

5.

sudo -u postgres createuser -PE -s myuser

postgresql> createdb -O myself -E UTF8 mydb

postgresql> echo 'The following databases have been created:'

postgresql> psql -l

postgresql> psql mydb

最初のコマンドは新規作成ユーザにmyuserというスーパーユーザ権限を与えます。パスワードを入力するよう促します。

The first of the commands will grant superuser-access to the new user, called myuser. It will prompt you for a password.

どのweb2pyアプリケーションでも以下のコマンドでこのデータベースに接続できます:

Any web2py application can connect to this database with the command:

1.

db = DAL("postgres://myuser:mypassword@localhost:5432/mydb")

mypasswordはプロンプト時にあなたが入力したパスワードで、5432はデータベースサーバーが実行されているポートです。

where mypassword is the password you entered when prompted, and 5432 is the port where the database server is running.

通常はアプリケーションごとにひとつのデータベースを作成し、同じアプリケーションの複数のインスタンスが同じデータベースに接続します。ことなるアプリケーション間で同じデータベースを共有することもできます。

Normally you use one database for each application, and multiple instances of the same application connect to the same database. It is also possible for different applications to share the same database.

データベースバックアップについては、PostgreSQLドキュメントにある;pg_dumppg_restoreコマンドを読んでください。

For database backup details, read the PostgreSQL documentation; specifically the commands pg_dump and pg_restore.