パッケージインストール
$ sudo apt-get install nginx $ sudo apt-get install uwsgi $ sudo apt-get install uwsgi-plugin-python3
Nginx 設定
$ pwd
/etc/nginx/sites-available
$ diff -u default.Org default
--- default.Org 2012-07-13 03:55:30.000000000 +0900
+++ default 2013-04-11 17:49:41.267955155 +0900
@@ -43,6 +43,11 @@
deny all;
}
+ location /MyApp/ {
+ include uwsgi_params;
+ uwsgi_pass unix:/var/run/uwsgi/app/MyApp/socket;
+ }
+
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
ソケットファイルでなく、TCP ソケットで uWSGI サーバーと通信する場合は uwsgi_pass を以下のようにします。(対応する uWSGI サーバーの設定も変更必要です。)- uwsgi_pass unix:/var/run/uwsgi/app/MyApp/socket; + uwsgi_pass 127.0.0.1:9090;
uWSGI 設定
アプリケーション用ファイル設置$ pwd /etc/uwsgi/apps-available $ cat MyApp.ini [uwsgi] plugins = python3 python-path = /usr/share/nginx/www/MyApp file = /usr/share/nginx/www/MyApp/MyApp.pyTCP ソケットを使う場合は以下も追加socket = :9090
apps-enabled にシンボリックリンク作成$ pwd /etc/uwsgi/apps-enabled $ sudo ln -s ../apps-available/MyApp.ini ./
Python スクリプト設置
$ pwd
/usr/share/nginx/www
$ sudo mkdir MyApp
$ pwd
/usr/share/nginx/www/MyApp
$ cat MyApp.py
import sys
def GetContents(dEnviron):
yield sys.version + '\n'
yield '\n'
for sName, xValue in dEnviron.items():
yield '{}: {}\n'.format(sName, xValue)
def application(dEnviron, cStart_response):
sStatus = '200 OK'
lHeaders = [('Content-type', 'text/plain')]
cStart_response(sStatus, lHeaders)
for sLine in GetContents(dEnviron):
yield sLine.encode()
sLine.encode() というように、バイト文字列に変換しないとうまくブラウザで表示されませんでした。
http://ja.wikipedia.org/wiki/Web_Server_Gateway_Interface の「WSGIとPython 3」が関係してる気がします。
(GetContents() をイテレートするような面倒くさい作りにしたのは、将来バイト文字列に変換しなくても良くなったときに簡単に対応できるようにするためです。)
各サービス起動
$ sudo service nginx start $ sudo service uwsgi start
ブラウザで http://Server/MyApp/ にアクセスすると Python のバージョンと環境変数が表示されます。
0 件のコメント:
コメントを投稿