CGI

もし他のすべてのデプロイ手法で動かない場合、CGIは確実に動くでしょう。CGIはすべての主要なサーバでサポートされていますが、普通は性能的に最適化されていません。 If all other deployment methods do not work, CGI will work for sure. CGI is supported by all major servers but usually has a sub-optimal performance.

これは、CGIに似た環境で実行される、GoogleのApp Engine上でFlaskアプリケーションを使用できるやり方でもあります。 This is also the way you can use a Flask application on Google's `App Engine`_, where execution happens in a CGI-like environment.

注意 Watch Out

予め自分のアプリケーションのファイル内にあるかもしれない全てのapp.run()呼び出しは、if __name__ == '__main__':ブロックの内側にあるか、別のファイルに移動していることを確実にしておいてください。CGI/appエンジンへアプリケーションをデプロイするときは望まないローカルWSGIサーバを常に開始してしまうために、app.run()が呼び出されないことを、まずは確認してください。 Please make sure in advance that any ``app.run()`` calls you might have in your application file are inside an ``if __name__ == '__main__':`` block or moved to a separate file. Just make sure it's not called because this will always start a local WSGI server which we do not want if we deploy that application to CGI / app engine.

CGIでは、自分のコードの中にあらゆるprint文を含まないこと、またはsys.stdoutを何かHTTPレスポンスの中へ書き込まないものへ上書きすることを確実にしてください。 With CGI, you will also have to make sure that your code does not contain any ``print`` statements, or that ``sys.stdout`` is overridden by something that doesn't write into the HTTP response.

.cgiファイルの作成 Creating a `.cgi` file

最初に、CGIアプリケーションのファイルを作成する必要があります。それをyourapplication.cgiと呼ぶことにしましょう: First you need to create the CGI application file. Let's call it :file:`yourapplication.cgi`::

#!/usr/bin/python
from wsgiref.handlers import CGIHandler
from yourapplication import app

CGIHandler().run(app)

サーバの準備(Server Setup) Server Setup

普通、サーバを設定する2つのやり方があります。単に.cgicgi-binの中へコピーする(そしてmod_rewriteまたはURLを書き変える何か似たものを使用する)か、サーバが直接そのファイルを指し示すようにします。 Usually there are two ways to configure the server. Either just copy the ``.cgi`` into a :file:`cgi-bin` (and use `mod_rewrite` or something similar to rewrite the URL) or let the server point to the file directly.

例えばApacheでは、以下に似たものを設定の中に置きます: In Apache for example you can put something like this into the config:

ScriptAlias /app /path/to/the/application.cgi

共有されているwebホスティングでは、しかしながら、Apacheの設定にアクセスできないかもしれません。そのケースでは、自分のappが使用可能な公開ディレクトリの中にある.htaccessと呼ばれるファイルでも機能しますが、ScriptAliasディレクティブは機能しないでしょう: On shared webhosting, though, you might not have access to your Apache config. In this case, a file called ``.htaccess``, sitting in the public directory you want your app to be available, works too but the ``ScriptAlias`` directive won't work in that case:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f # Don't interfere with static files
RewriteRule ^(.*)$ /path/to/the/application.cgi/$1 [L]

さらなる情報については、自分のwebサーバのドキュメントを確認してください。 For more information consult the documentation of your webserver.