Apache2 : mod_wsgi の設定2025/09/09 |
|
mod_wsgi (WSGI : Web Server Gateway Interface) をインストールして、Python スクリプトの実行を高速化します。 |
|
| [1] | [mod_wsgi] をインストールします。 |
|
root@www:~# apt -y install libapache2-mod-wsgi-py3
|
| [2] | 例として [/var/www/wsgi/test_wsgi.py] が [/test_wsgi] でアクセスできるよう設定します。 |
|
root@www:~#
vi /etc/apache2/conf-available/wsgi.conf # 新規作成
WSGIScriptAlias /test_wsgi /var/www/wsgi/test_wsgi.py
a2enconf wsgi Enabling conf wsgi. To activate the new configuration, you need to run: systemctl reload apache2root@www:~# systemctl reload apache2
|
| [3] | [2] で設定したテストスクリプトを作成して動作確認します。 |
|
root@www:~# mkdir /var/www/wsgi
root@www:~#
vi /var/www/wsgi/test_wsgi.py # 新規作成
def application(environ, start_response):
status = '200 OK'
html = '<html>\n' \
'<body>\n' \
'<h1 style="width: 100%; font-size: 40px; 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]
|
|
| [4] | Django を利用する場合は以下のように設定します。( Django 環境の構築はこちらを参照 ) 例として、[debian] ユーザー所有で [/home/debian/testproject] 配下の [test_app] を動作させるよう設定します。 |
|
root@www:~#
vi /etc/apache2/conf-available/django.conf # 新規作成
WSGIDaemonProcess testapp python-path=/home/debian/testproject:/home/debian/django/lib/python3.13/site-packages
WSGIProcessGroup testapp
WSGIScriptAlias /django /home/debian/testproject/testproject/wsgi.py
<Directory /home/debian/testproject>
Require all granted
</Directory>
a2enconf django Enabling conf django. To activate the new configuration, you need to run: systemctl reload apache2
root@www:~#
systemctl reload apache2
# 当例のようにユーザー領域を使用する場合は要追加変更 root@www:~# ll -d /home/debian drwx------ 5 debian debian 4096 Sep 9 10:01 /home/debian root@www:~# chmod 711 /home/debian |
|
| Sponsored Link |
|
|