Apache httpd : mod_wsgi の設定2023/03/03 |
|
[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
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 環境の構築はこちらを参照 ) 例として、[alma] ユーザー所有で [/home/alma/testproject] 配下の [test_app] を動作させるよう設定します。 |
|
[root@www ~]#
vi /etc/httpd/conf.d/django.conf # 新規作成
WSGIDaemonProcess test_app python-path=/home/alma/testproject:/home/alma/django/lib/python3.9/site-packages
WSGIProcessGroup test_app
WSGIScriptAlias /django /home/alma/testproject/testproject/wsgi.py
<Directory /home/alma/testproject>
Require all granted
</Directory>
[root@www ~]#
systemctl reload httpd # 当例のようにユーザー領域を使用する場合 や SELinux 有効な場合は要追加変更 [root@www ~]# ll -d /home/alma drwx------. 6 alma alma 165 Mar 16 10:13 /home/alma[root@www ~]# chmod 711 /home/alma [root@www ~]# setsebool -P httpd_read_user_content on |
|
| Sponsored Link |
|
|