CentOS 8
Sponsored Link

Apache httpd : mod_wsgi の設定2019/12/26

 
[mod_wsgi (WSGI : Web Server Gateway Interface)] をインストールして、Python スクリプトの実行を高速化します。
[1]
[2] [mod_wsgi] をインストールします。
[root@www ~]#
dnf -y install python3-mod_wsgi
[3] 例として [/var/www/html/test_wsgi.py] が [/test_wsgi] でアクセスできるよう設定します。
[root@www ~]#
vi /etc/httpd/conf.d/python3_wsgi.conf
# 新規作成

WSGIScriptAlias /test_wsgi /var/www/html/test_wsgi.py
[root@www ~]#
systemctl restart httpd

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

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

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

<Directory /home/cent/venv/testproject>
    Require all granted
</Directory>

[root@www ~]#
systemctl restart httpd

# SELinux 有効 且つ 当例のようにユーザー領域を使用する場合は要ポリシー変更

[root@www ~]#
setsebool -P httpd_read_user_content on

関連コンテンツ