uWSGI

提供: ArchWiki
2016年9月25日 (日) 10:39時点におけるKusakata (トーク | 投稿記録)による版 (Kusakata がページ「Uwsgi」を「UWSGI」に移動しました)
ナビゲーションに移動 検索に移動

uWSGI は高速で自己回復機能のある、開発者とシステム管理人に優しいアプリケーションコンテナサーバーです。C だけで書かれています。

インストール

公式リポジトリuwsgi パッケージをインストールしてください。パッケージをコンパクトにするために、プラグインは付属していません。外部プラグインは別にインストールする必要があります。C で書かれているためとても効率的なソフトウェアです。gunicornAUR など Python で書かれた代替も存在しますが、どうしても動作は遅くなってしまいます。

サービスの起動

ノート: アプリケーション (Python アプリケーション) ごとにそれぞれ uwsgi サービスのインスタンスを作成するのが一番シンプルな構成です。uwsgi サービスを有効化・起動する前に、/etc/uwsgi/ の中に同一の名前の設定ファイルを作成してください。以下では /etc/uwsgi/helloworld.ini を使っています。

スタートアップ時にデフォルトで uwsgi サービスを有効にするには、次を実行:

# systemctl enable uwsgi@helloworld

これで /etc/uwsgi/helloworld.ini 設定されたアプリケーションのサービスが有効になります。また、以下のコマンドを使ってソケットインターフェイスで有効にすることも可能です:

# systemctl enable uwsgi@helloworld.socket

もしくは、Emperor モード サービスを実行することができます。このモードを使うと uwsgi インスタンスで (emperor と呼ばれる) メインスーパーバイザーを使って様々なアプリ (vassal と呼ばれます) が動くようになります。有効にするには、次を入力:

# systemctl enable emperor.uwsgi

ソケットを使うこともできます:

# systemctl enable emperor.uwsgi.socket

Emperor の設定は /etc/uwsgi/emperor.ini にあります。

設定

ノート: 標準インストールでは /etc/uwsgi/archlinux.ini は付属していません。

/etc/uwsgi/ 内のファイルを編集することで設定ができます。パッケージに含まれているビルドファイルは /etc/uwsgi/archlinux.ini に存在します。

詳しくは uwsgi のドキュメント を見て下さい。

ノート: 全てのオプションのリストは次の資料を参照してください: http://uwsgi-docs.readthedocs.org/en/latest/Options.html
アプリケーションの設定

以下は python をサポートするシンプルな例です。pacman で community リポジトリから uwsgi-plugin-python または uwsgi-plugin-python2 プラグインをインストールする必要があります。

[uwsgi]
chdir = /srv/http/helloworld
module = helloworld
plugins = python

以下のような構文を使うことで uwsgi を別個に実行することも可能です:

ノート: pacman でインストールした uwsgi から --wsgi-file オプションを使うことはできません。公式ガイドではソースからビルドすることを推奨しています (http://uwsgi-docs.readthedocs.org/en/latest/WSGIquickstart.html#installing-uwsgi-with-python-support を参照)。
uwsgi --socket 127.0.0.1:3031 --plugin python2 --wsgi-file ~/foo.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 --uid --gid

上記のコマンドを root で実行するのは止めてください。

Php アプリケーション

uwsgi の php プラグインをインストール: uwsgi-plugin-php

/etc/uwsgi/mysite.ini
[uwsgi]
; maximum number of worker processes
processes = 4
; the user and group id of the process once it’s started
uid = http
gid = http
socket = /run/uwsgi/%n.sock
master = true
chdir = /srv/http/%n
; php
plugins = php
; jail our php environment
php-docroot = /srv/http/%n
php-index = index.php
; clear environment on exit
vacuum = true

Nginx 設定:

location = /index.php {
    include uwsgi_params;
    uwsgi_modifier1 14;
    uwsgi_pass unix:/run/uwsgi/mysite.sock;
}
Nginx の設定
location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    include uwsgi_params;
    # uwsgi_pass unix:/var/run/uwsgi/helloworld.sock;
    uwsgi_pass 127.0.0.1:3031;
}

参照