揮発性のメモ2

http://d.hatena.ne.jp/iww/

WSGIでpythonを動かすやつ

linux4.9, debian9.5, apache2.4, python3.5

WSGI
Web Server Gateway Interface

pythonを動かすために作られたFastCGIのパチモン。
拡張子は .wsgi にしておくとよい。
要求に応じて.wsgiファイルの中の application関数*1 が呼び出される。
デーモンモードと組込みモードとがあり、なにもしないと組込みモードになる。
組込みモードではapache2自らが処理をし、デーモンモードでは専用のプロセスが処理をする。
HTTPヘッダは自力で出力するか適当なフレームワーク使う

WSGIモジュールをインストール

apt install libapache2-mod-wsgi-py3

壁に当たるまではpipでなくaptだけで済ませたい

apacheの設定ファイル(組込みモード)

; /etc/apache2/conf-enabled/jikkenwsgi.conf

Alias "/wsgihoge" "/home/unko/project/wsgihoge"
<Directory "/home/unko/project/wsgihoge">
    AddHandler wsgi-script .wsgi

    Options ExecCGI FollowSymLinks Indexes
    AllowOverride Options AuthConfig FileInfo Limit Indexes
    Require all granted
</Directory>

wsgiソース

#!/usr/bin/python3
# coding:utf-8

def application(environ, start_response):
    status = '200 OK'
    output = b'Hello,World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

*1:*main関数的扱い。名前は好きに変えられる