openSUSE Leap 16

Apache httpd : mod_wsgi の設定2025/11/13

 

[mod_wsgi (WSGI : Web Server Gateway Interface)] をインストールして、Python スクリプトの実行を高速化します。

[1]

こちらを参考に Python 3 をインストールしておきます

[2] [mod_wsgi] をインストールします。
www:~ #
zypper -n install apache2-mod_wsgi
[3] 例として [/srv/www/htdocs/test_wsgi.py] が [/test_wsgi] でアクセスできるよう設定します。
www:~ #
vi /etc/apache2/conf.d/python3_wsgi.conf
# 新規作成

WSGIScriptAlias /test_wsgi /srv/www/htdocs/test_wsgi.py
www:~ #
a2enmod wsgi

www:~ #
systemctl restart apache2

[4] [2] で設定したテストスクリプトを作成して動作確認します。
www:~ #
vi /srv/www/htdocs/test_wsgi.py
# 新規作成

def application(environ, start_response):
    status = '200 OK'
    html = '<html>\n' \
           '<body>\n' \
           '<h1 style="width: 100%; font-size: 48px; text-align: center;">\n' \
           'WSGI Test Page\n' \
           '</h1>\n' \
           '</body>\n' \
           '</html>\n'.encode("utf-8")
    response_header = [('Content-type','text/html')]
    start_response(status,response_header)
    return [html]
[5] Django を利用する場合は以下のように設定します。( Django 環境の構築はこちらを参照 )
例として、[suse] ユーザー所有で [/home/suse/testproject] 配下の [test_app] を動作させるよう設定します。
www:~ #
vi /etc/apache2/conf.d/django.conf
# 新規作成

WSGIDaemonProcess test_app python-path=/home/suse/testproject:/home/suse/django/lib/python3.13/site-packages
WSGIProcessGroup test_app
WSGIScriptAlias /django /home/suse/testproject/testproject/wsgi.py

<Directory /home/suse/testproject>
    Require all granted
</Directory>

www:~ #
systemctl reload apache2

# 当例のようにユーザー領域を使用する場合 や SELinux 有効な場合は要追加変更

www:~ #
ll -d /home/suse

drwx------. 6 suse suse 194 Nov 13 13:47 /home/suse
www:~ #
chmod 711 /home/suse

www:~ #
setsebool -P httpd_read_user_content on

関連コンテンツ