mod_wsgi

mod_wsgiは、Apache httpdサーバと統合されたWSGIサーバです。最近のmod_wsgi-expressコマンドは、Apache httpdの設定を書く必要なく、サーバの設定と開始を容易にできるようにします。 `mod_wsgi`_ is a WSGI server integrated with the `Apache httpd`_ server. The modern `mod_wsgi-express`_ command makes it easy to configure and start the server without needing to write Apache httpd configuration.

  • Apache httpdと密接に統合されています。 Tightly integrated with Apache httpd.

  • Windowsを直接サポートします(訳注: WSLでなくても動きます)。 Supports Windows directly.

  • コンパイラとApacheの開発用ヘッダー(訳注: Apacheのモジュールなどを開発するとき、プログラミング言語の処理などで必要になるファイルであり、HTTPのヘッダーとは異なります)がインストールに必要です。 Requires a compiler and the Apache development headers to install.

  • リバースプロキシの用意が不要です。 Does not require a reverse proxy setup.

このページはmod_wsgi-expressを実行する基本について説明し、httpdに伴うより複雑なインストールと設定は説明しません。mod_wsgi-expressmod_wsgi、そしてApache httpdのドキュメントを読み、どの目玉機能(feature)が利用可能か理解しておいてください。 This page outlines the basics of running mod_wsgi-express, not the more complex installation and configuration with httpd. Be sure to read the `mod_wsgi-express`_, `mod_wsgi`_, and `Apache httpd`_ documentation to understand what features are available.

インストール Installing

mod_wsgiのインストールにはコンパイラとApacheのサーバと開発用ヘッダーがインストールされている必要があります。もしそれらがインストールされていない場合、エラーが発生するでしょう。それらをどのようにインストールするかは、使っているOSとパッケージ管理ツール(package manager)に依存します。 Installing mod_wsgi requires a compiler and the Apache server and development headers installed. You will get an error if they are not. How to install them depends on the OS and package manager that you use.

virtualenvを作成し、アプリケーションをインストールし、それからmod_wsgiをインストールします。 Create a virtualenv, install your application, then install ``mod_wsgi``.

$ cd hello-app
$ python -m venv venv
$ . venv/bin/activate
$ pip install .  # install your application
$ pip install mod_wsgi

実行 Running

mod_wsgi-expressへの唯一つの引数は、Flaskアプリケーションを含むスクリプトを指定し、(スクリプト内では)Flaskアプリケーションはapplicationと呼ばれる必要があります(訳注: 以下の例のように「application」という変数にFlaskアプリケーションのインスタンスを格納するような意味合いです)。この名前(「application」)で、自分のアプリケーションをimportするか、もしfactory patternを使っている場合はFlaskアプリケーションを作成する、小さなスクリプトを書くことができます。 The only argument to ``mod_wsgi-express`` specifies a script containing your Flask application, which must be called ``application``. You can write a small script to import your app with this name, or to create it if using the app factory pattern.

wsgi.py
from hello import app

application = app
wsgi.py
from hello import create_app

application = create_app()

この時点でmod_wsgi-express start-serverコマンドを実行します。 Now run the ``mod_wsgi-express start-server`` command.

$ mod_wsgi-express start-server wsgi.py --processes 4

--processesオプションは走らせるworkerプロセスの数を指定します; 開始時の値は`` CPU * 2``にできるでしょう。 The ``--processes`` option specifies the number of worker processes to run; a starting value could be ``CPU * 2``.

各リクエストのログは端末には表示されません。もしエラーが起きた場合、その情報は、サーバ開始時に表示されるエラーログのファイルに書かれます。 Logs for each request aren't show in the terminal. If an error occurs, its information is written to the error log file shown when starting the server.

外部への結び付け(Binding Externally) Binding Externally

このドキュメントの他のWSGIサーバとは違い、mod_wsgiは80番や443番のような結び付け(bind)にroot権限が必要なポートに結びつけるために、rootとして実行できます。しかしながら、workerプロセス用にはrootとは異なるユーザとグループに権限を落とすよう設定される必要があります。 Unlike the other WSGI servers in these docs, mod_wsgi can be run as root to bind to privileged ports like 80 and 443. However, it must be configured to drop permissions to a different user and group for the worker processes.

例えば、もしhelloユーザとグループを作成した場合、virtualenvとアプリケーションをそのユーザでインストールし、それからmod_wsgiに開始後はhelloユーザに(権限を)落とすよう伝えるべきです。 For example, if you created a ``hello`` user and group, you should install your virtualenv and application as that user, then tell mod_wsgi to drop to that user after starting.

$ sudo /home/hello/venv/bin/mod_wsgi-express start-server \
    /home/hello/wsgi.py \
    --user hello --group hello --port 80 --processes 4