uWSGI¶
uWSGIはnginx, lighttpd, cherokeeのようなサーバでのデプロイの選択肢です;他の選択肢についてはFastCGIと独立したWSGIコンテナ(Standalone WSGI Containers)を見てください。自分のWSGIアプリケーションをuWSGIプロトコルと一緒に使うためには、最初にuWSGIサーバが必要になるでしょう。uWSGIはプロトコルでもありアプリケーションサーバでもあります;アプリケーションサーバはuWSGI、fastcgi、そしてHTTPプロトコルに対応可能です。 uWSGI is a deployment option on servers like `nginx`_, `lighttpd`_, and `cherokee`_; see :doc:`fastcgi` and :doc:`wsgi-standalone` for other options. To use your WSGI application with uWSGI protocol you will need a uWSGI server first. uWSGI is both a protocol and an application server; the application server can serve uWSGI, FastCGI, and HTTP protocols.
usgiは最も人気のあるuWSGIサーバで、このガイドで扱います。ガイドに沿ってフォローしていくには、usgiを確実にインストールしておいてください。 The most popular uWSGI server is `uwsgi`_, which we will use for this guide. Make sure to have it installed to follow along.
注意 Watch Out
予め自分のアプリケーションのファイル内にあるかもしれない全てのapp.run()
呼び出しは、if __name__ == '__main__':
ブロックの内側にあるか、別のファイルに移動していることを確実にしておいてください。uWSGIへアプリケーションをデプロイするときは望まないローカル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 uWSGI.
uwsgiを使ったappの開始 Starting your app with uwsgi¶
uwsgiはpythonモジュール内で見つかる呼び出し可能なWSGIを操作するように設計されています。 `uwsgi` is designed to operate on WSGI callables found in python modules.
myapp.pyの中にflaskアプリケーションがある場合は、以下のコマンドを使用します: Given a flask application in myapp.py, use the following command:
$ uwsgi -s /tmp/yourapplication.sock --manage-script-name --mount /yourapplication=myapp:app
--manage-script-name
はSCRIPT_NAME
の処理を、より賢く行うuwsgiへと移動します。それは、/yourapplication
へのリクエストをmyapp:app
へ向かわせる--mount
指示と一緒に使用されます。もし自分のアプリケーションがルートのレベルでアクセス可能な場合、/application
の代わりに単なる/
を使用可能です。myapp
はflaskアプリケーションのファイル名(拡張子を除く)またはapp
を提供するモジュール名を参照します。app
はアプリケーションの中で呼び出し可能なもの(callable)(普通はapp = Flask(__name__)
という行)です。
The ``--manage-script-name`` will move the handling of ``SCRIPT_NAME`` to uwsgi, since it is smarter about that. It is used together with the ``--mount`` directive which will make requests to ``/yourapplication`` be directed to ``myapp:app``. If your application is accessible at root level, you can use a single ``/`` instead of ``/yourapplication``. ``myapp`` refers to the name of the file of your flask application (without extension) or the module which provides ``app``. ``app`` is the callable inside of your application (usually the line reads ``app = Flask(__name__)``.
もしvirtual environmentの内側のflaskアプリケーションをデプロイしたい場合、--virtualenv /path/to/virtual/environment
も追加する必要があります。自分のプロジェクトで使うpythonのバージョンによっては--plugin python
または--plugin python3
を追加する必要があるかもしれません。
If you want to deploy your flask application inside of a virtual environment, you need to also add ``--virtualenv /path/to/virtual/environment``. You might also need to add ``--plugin python`` or ``--plugin python3`` depending on which python version you use for your project.
nginxの設定 Configuring nginx¶
基本的なflaskのnginxの設定は以下のようなものです: A basic flask nginx configuration looks like this::
location = /yourapplication { rewrite ^ /yourapplication/; }
location /yourapplication { try_files $uri @yourapplication; }
location @yourapplication {
include uwsgi_params;
uwsgi_pass unix:/tmp/yourapplication.sock;
}
上記の設定はアプリケーションを/application
に結び付けます。もしURLのルートにしたい場合は、もう少しシンプルになります:
This configuration binds the application to ``/yourapplication``. If you want to have it in the URL root its a bit simpler::
location / { try_files $uri @yourapplication; }
location @yourapplication {
include uwsgi_params;
uwsgi_pass unix:/tmp/yourapplication.sock;
}