本番環境への展開(Deploy to Production) Deploy to Production

チュートリアルのこのパートでは、自分のアプリケーションを展開(deploy)したいサーバを自分で持っていると仮定しています。配付(distribution)ファイルを作成してインストールするやり方の概要を示しますが、どのサーバおよびソフトウェアを使用するかについて詳細は述べません。開発用のコンピュータで新しい環境を準備して以下の手順を試すことは可能ですが、本当に公開されるアプリケーションをホスティングするときは(以下の手順は)使うべきではないでしょう。アプリケーションを動かす多くの異なるやり方のリストについては、展開の選択肢(Deployment Options)を確認してください。 This part of the tutorial assumes you have a server that you want to deploy your application to. It gives an overview of how to create the distribution file and install it, but won't go into specifics about what server or software to use. You can set up a new environment on your development computer to try out the instructions below, but probably shouldn't use it for hosting a real public application. See :doc:`/deploying/index` for a list of many different ways to host your application.

構築とインストール(Build and Install) Build and Install

どこかへ自分のアプリケーションを展開(deploy)したいときは、配付ファイルを構築(build)します。現時点の標準的なPythonの配付ファイルは、拡張子が.whlwheel形式です。始めに、wheelライブラリを確実にインストールしておきます: When you want to deploy your application elsewhere, you build a distribution file. The current standard for Python distribution is the *wheel* format, with the ``.whl`` extension. Make sure the wheel library is installed first:

$ pip install wheel

Pythonを使ってsetup.pyを実行すると、ビルド関連のコマンドを行うコマンドラインツールになります。bdist_wheelコマンドはwheel配付ファイルをビルドします。 Running ``setup.py`` with Python gives you a command line tool to issue build-related commands. The ``bdist_wheel`` command will build a wheel distribution file.

$ python setup.py bdist_wheel

その(ビルドした)ファイルはdist/flaskr-1.0.0-py3-none-any.whlで見つけられます。ファイル名はプロジェクト名、バージョン、それとファイルがインストール可能であるかに関する、いくらかのタグになります。 You can find the file in ``dist/flaskr-1.0.0-py3-none-any.whl``. The file name is the name of the project, the version, and some tags about the file can install.

このファイルを別のマシンへコピーし、新しい仮想環境を準備し、それからpipを使ってそのファイルをインストールします。 Copy this file to another machine, :ref:`set up a new virtualenv <install-create-env>`, then install the file with ``pip``.

$ pip install flaskr-1.0.0-py3-none-any.whl

pipは依存対象と一緒にプロジェクトをインストールします。 Pip will install your project along with its dependencies.

これは別のマシンであるため、もう一度init-dbを実行して、インスタンスフォルダにデータベースを作成する必要があります。 Since this is a different machine, you need to run ``init-db`` again to create the database in the instance folder.

$ export FLASK_APP=flaskr
$ flask init-db

Flaskが(編集可能モードではなく)インストールされていると検知したときは、Flaskはインスタンスフォルダに(インストールされた場所とは)別のディレクトリを使用します。それは、代わりにvenv/var/flaskr-instanceで見つけられます。 When Flask detects that it's installed (not in editable mode), it uses a different directory for the instance folder. You can find it at ``venv/var/flaskr-instance`` instead.

秘密鍵(Secret Key)の設定 Configure the Secret Key

このチュートリアルの始めに、SECRET_KEYへは標準設定の値を与えていました。これは本番環境ではなにかしら規則性のない(random)byteへ変更するべきです。さもないと、攻撃者が公開された'dev'(に設定されている)キーを使用してセッションのクッキーの変更や、その他の秘密鍵を使ったあらゆることをできるようになります。 In the beginning of the tutorial that you gave a default value for :data:`SECRET_KEY`. This should be changed to some random bytes in production. Otherwise, attackers could use the public ``'dev'`` key to modify the session cookie, or anything else that uses the secret key.

randomな秘密鍵を出力するには以下のコマンドが使用できます: You can use the following command to output a random secret key:

$ python -c 'import os; print(os.urandom(16))'

b'_5#y2L"F4Q8z\n\xec]/'

もし存在していたらfactoryが読み取りにいくconfig.pyファイルを、インスタンスフォルダに作成します。生成した値(秘密の鍵)をその中にコピーします。 Create the ``config.py`` file in the instance folder, which the factory will read from if it exists. Copy the generated value into it.

venv/var/flaskr-instance/config.py
SECRET_KEY = b'_5#y2L"F4Q8z\n\xec]/'

その他にも、必要な設定は何でもここで設定できますが、Flaskrが必要とするのはSECRET_KEYだけです。 You can also set any other necessary configuration here, although ``SECRET_KEY`` is the only one needed for Flaskr.

本番環境サーバでの実行 Run with a Production Server

開発環境ではなく公開されるように(Flaskを)実行するときは、組み込みの開発サーバを使用(flask run)するべきではありません。開発サーバは便利なようにWerkzeugによって提供されていますが、効率性、安定性、セキュリティを特別意識して設計されてはいません。 When running publicly rather than in development, you should not use the built-in development server (``flask run``). The development server is provided by Werkzeug for convenience, but is not designed to be particularly efficient, stable, or secure.

代わりに、本番環境のWSGIサーバを使ってください。例えば、Waitressを使うには、まずは仮想環境にインストールします: Instead, use a production WSGI server. For example, to use `Waitress`_, first install it in the virtual environment:

$ pip install waitress

Waitressへ自分のアプリケーションについて伝える必要がありますが、それはflask runのようにFLASK_APPを使いはしません。Flaskアプリケーションのオブジェクトを取得するためにはimportしてapplication factoryを呼び出すということをWaitressへ伝える必要があります。 You need to tell Waitress about your application, but it doesn't use ``FLASK_APP`` like ``flask run`` does. You need to tell it to import and call the application factory to get an application object.

$ waitress-serve --call 'flaskr:create_app'

Serving on http://0.0.0.0:8080

自分のアプリケーションをホストする多くの異なるやり方のリストについては展開の選択肢(Deployment Options)を見てください。Waitressはひとつの例に過ぎず、このチュートリアルではWindowsとLinuxを両方サポートするという理由から選ばれました。WSGIサーバと、自分のプロジェクトで選択できる展開(deployment)のオプションは、より数多く存在しています。 See :doc:`/deploying/index` for a list of many different ways to host your application. Waitress is just an example, chosen for the tutorial because it supports both Windows and Linux. There are many more WSGI servers and deployment options that you may choose for your project.

開発を続けましょう!へ続きます。 Continue to :doc:`next`.