ServersMan@VPS(CentOS)にWSGIインターフェース mod_wsgiをインストールする

ServersMan@VPSCentOS)にWSGI に準拠した、Python⇔Webサーバ間インタフェースmod_wsgiをインストールする。

1.httpd-develのインストール(必要な場合実施する)

Apacheのモジュールをコンパイルするのに必要なapxsが、CentOShttpdでは標準でインストールされていないのでインストール

yum install httpd-devel

2.python-devel をインストール(必要な場合実施する)

yum install python-devel

3.mod_wsgiのインストール(ダウンロードはmodwsgi – Google Codeから)

wget http://modwsgi.googlecode.com/files/mod_wsgi-3.4.tar.gz
tar xvzf mod_wsgi-3.4.tar.gz
cd mod_wsgi-3.4
./configure --with-apxs=/usr/sbin/apxs --with-python=/usr/bin/python
make
make install

4.Apacheの設定

mod_wsgiが使用出来るように、以下を内容のpython.confを作成しApacheを再起動する

vi /etc/httpd/conf.d/python.conf コマンドで、下記の行を追加する

LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonEggs /home/admin/.python-eggs

Apacheの再起動は /etc/init.d/httpd restart

5.テストプログラムの作成

Python CGIプログラミング入門さんにあったソースをmod_wsgi用に手直しさせて頂いて^^;;;;

/var/www/wsgi-scripts/配下に以下の内容でmyapp.wsgiを作成

#-*- coding:utf-8 -*-
from cgi import parse_qsl

html = '''<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<title>テキスト入力フィールドに入力された文字を取得する</title>
</head>
<body>
<h1>テキスト入力フィールドに入力された文字を取得する</h1>
<p>入力された文字は、「%s」です。</p>
<form action="http://chamu.org/myapp" method="post">
<input type="text" name="text" />
<input type="submit" />
</form>
</body>
</html>
'''


def application(environ, start_response):
	method = environ.get('REQUEST_METHOD')
	data = ''
	if method == "POST":
		query = dict(parse_qsl(environ['wsgi.input'].read()))
		if 'text' in query:
			data = query['text']

	output = html % data
	status = '200 OK'
	response_headers = [('Content-type', 'text/html; charset=utf-8'),
						('Content-Length', str(len(output)))]
	start_response(status, response_headers)
	return output

6.テスト

(1)作成したWSGIアプリケーションが実行出来るようhttpd.confにWSGIScriptAliasを追加しApacheを再起動する

WSGIScriptAlias /myapp /var/www/wsgi-scripts/myapp.wsgi

Apacheの再起動は/etc/init.d/httpd restart

(2)http://サーバーのアドレス/myapp にアクセスしてプログラムが動けばOK!

こんな感じ → http://chamu.org/myapp