LoginSignup
6
12

More than 5 years have passed since last update.

ApacheでPythonを実行する

Posted at

概要

ApacheでPythonを実行できる環境を整えます。

環境

  • Arch Linux 5.0.6
  • Apache 2.4
  • Python 3.7

mod_wsgiとは

WSGIとはWeb Server Gateway Interfaceの略で、mod_wsgiは、Graham Dumpletonにより開発されたWebサーバとPythonで作成したコードをつなげるためのインターフェイスです。以前はmod_pythonが主流でしたが、開発が終了したため、現在はmod_wsgiが使われています。

mod_wsgiのインストール

# pacman -S mod_wsgi

Apacheの設定ファイルの変更

/etc/httpd/conf/httpd.confに以下の行を追加します。

/etc/httpd/conf/httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so

動作確認

テストファイルを作成し、Apacheの設定ファイルに以下を追加します。

/srv/http/wsgi_app.py
#-*- coding: utf-8 -*-
def wsgi_app(environ, start_response):
    import sys
    output = sys.version.encode('utf8')
    status = '200 OK'
    headers = [('Content-type', 'text/plain'),
               ('Content-Length', str(len(output)))]
    start_response(status, headers)
    yield output

application = wsgi_app
/etc/httpd/conf/httpd.conf
WSGIScriptAlias /wsgi_app /srv/http/wsgi_app.py

Apacheを再起動します。

# systemctl restart httpd

curlコマンドで確認

$ curl http://localhost/wsgi_app
3.7.3 (default, Mar 26 2019, 21:43:19) 
[GCC 8.2.1 20181127]
6
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
12