Apache httpd : mod_wsgi の設定2024/02/01 |
|
mod_wsgi (WSGI : Web Server Gateway Interface) をインストールして、Python スクリプトの実行を高速化します。 |
|
| [1] | [mod_wsgi] をインストールします。 |
|
root@www:~ # pkg install -y ap24-py39-mod_wsgi
|
| [2] | 例として [/usr/local/www/apache24/data/test_wsgi.py] が [/test_wsgi] でアクセスできるよう設定します。 |
|
root@www:~ #
vi /usr/local/etc/apache24/modules.d/270_mod_wsgi.conf # コメント解除 LoadModule wsgi_module libexec/apache24/mod_wsgi.so
root@www:~ #
vi /usr/local/etc/apache24/Includes/wsgi.conf # 新規作成
WSGIScriptAlias /test_wsgi /usr/local/www/apache24/data/test_wsgi.py
service apache24 reload
|
| [3] | [2] で設定したテストスクリプトを作成して動作確認します。 |
|
root@www:~ #
vi /usr/local/www/apache24/data/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 Test Page\n' \
'</div>\n' \
'</body>\n' \
'</html>\n'.encode("utf-8")
response_header = [('Content-type','text/html')]
start_response(status,response_header)
return [html]
|
|
| [4] | Django を利用する場合は以下のように設定します。( Django 環境の構築はこちらを参照 ) 例として、[freebsd] ユーザー所有で [/home/freebsd/testproject] 配下の [test_app] を動作させるよう設定します。 |
|
root@www:~ #
vi /usr/local/etc/apache24/Includes/django.conf # 新規作成
WSGIDaemonProcess testapp python-path=/home/freebsd/testproject:/home/freebsd/django/lib/python3.9/site-packages
WSGIProcessGroup testapp
WSGIScriptAlias /django /home/freebsd/testproject/testproject/wsgi.py
<Directory /home/freebsd/testproject>
Require all granted
</Directory>
root@www:~ #
service apache24 reload
# 当例のようにユーザー領域を使用する場合は要追加変更 root@www:~ # ls -ld /home/freebsd drwx------ 5 freebsd freebsd 14 Feb 1 11:47 /home/freebsd root@www:~ # chmod 711 /home/freebsd |
|
| Sponsored Link |
|
|