目次
Web3関連の開発を行っています。分散型アプリケーションが研究テーマの学生です。
WSGI (Web Server Gateway Interface) は PSGI に影響を与えた python の標準化された Web インターフェースです。
WSGI の実装としては上記二つの選択肢があります。前者は Apache に依存しており更新も活発ではないため、後者の uwsgi を利用します。
参考ドキュメント
事前準備
必須ではありませんが、今回は pyenv/virtualenv を用いて 2.7.10 をシステムインストールした状況を想定します。
インストール
$ pyenv shell 2.7.10
$ pyenv exec pip install uwsgi
$ pyenv exec pip install uwsgitop
サンプルアプリケーション
wsgi.py
#!/bin/env python
# -*- coding: utf-8 -*-
import sys
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return ["Hello World, python version: \n%s\n" % (sys.version)]
wsgi.ini
[uwsgi]
http = :8080
stats = 127.0.0.1:8181
wsgi-file = wsgi.py
master = true
processes = 4
threads = 2
実行
$ pyenv exec uwsgi wsgi.ini
動作検証
$ ps auxw | grep wsgi
$ pyenv exec uwsgitop localhost:8181
$ curl http://localhost:8080/
複数アプリケーションの管理
/etc/uwsgi/vassals/wsgi-001.ini
[uwsgi]
http = :8080
stats = 127.0.0.1:8181
chdir = /usr/local/uwsgi/apps
wsgi-file = wsgi-001.py
master = true
processes = 4
threads = 2
logto = /var/log/uwsgi/wsgi-001.log
/usr/local/uwsgi/apps/wsgi-001.py
#!/bin/env python
# -*- coding: utf-8 -*-
import sys
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return ["Hello World, python version: \n%s\n" % (sys.version)]
/etc/uwsgi/emperor.ini
[uwsgi]
emperor = /etc/uwsgi/vassals
uid = nobody
gid = nobody
起動手順
$ sudo su -l
$ chown nobody: /var/log/uwsgi
$ cd /etc/uwsgi/
$ pyenv shell 2.7.10
$ pyenv exec uwsgi emperor.ini
nginx リバースプロキシ
/etc/nginx/nginx.conf
で uwsgi_pass
するためには /etc/uwsgi/vassals/wsgi-001.ini
において http
ではなく socket
を使用します。
/etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
access_log /var/log/nginx/access.log combined;
server {
listen 80 default_server;
include uwsgi_params;
location / {
uwsgi_pass 127.0.0.1:8080;
}
location /001 {
uwsgi_pass 127.0.0.1:8080;
}
location /002 {
uwsgi_pass 127.0.0.1:9090;
}
}
}
/etc/uwsgi/vassals/wsgi-001.ini
[uwsgi]
socket = 127.0.0.1:8080
stats = 127.0.0.1:8181
chdir = /usr/local/uwsgi/apps
wsgi-file = wsgi-001.py
master = true
processes = 4
threads = 2
logto = /var/log/uwsgi/wsgi-001.log
/usr/local/uwsgi/apps/wsgi-001.py
#!/bin/env python
# -*- coding: utf-8 -*-
import sys
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return ["Hello World, python version: \n%s\n" % (sys.version)]
/etc/uwsgi/emperor.ini
[uwsgi]
emperor = /etc/uwsgi/vassals
uid = nobody
gid = nobody
起動手順
$ sudo su -l
$ chown nobody: /var/log/uwsgi
$ cd /etc/uwsgi/
$ pyenv shell 2.7.10
$ pyenv exec uwsgi emperor.ini
動作検証
$ ps auxw | grep wsgi
$ sudo less /var/log/uwsgi/wsgi-001.log
$ pyenv exec uwsgitop localhost:8181
$ curl http://localhost/
記事の執筆者にステッカーを贈る
有益な情報に対するお礼として、またはコメント欄における質問への返答に対するお礼として、 記事の読者は、執筆者に有料のステッカーを贈ることができます。
さらに詳しく →Feedbacks
ログインするとコメントを投稿できます。
関連記事
- Python コードスニペット (条件分岐)if-elif-else sample.py #!/usr/bin/python # -*- coding: utf-8 -*- # コメント内であっても、ASCII外の文字が含まれる場合はエンコーディング情報が必須 x = 1 # 一行スタイル if x==0: print 'a' # 参考: and,or,notが使用可能 (&&,||はエラー) elif x==1: p...
- Python コードスニペット (リスト、タプル、ディクショナリ)リスト range 「0から10まで」といった範囲をリスト形式で生成します。 sample.py print range(10) # for(int i=0; i<10; ++i) ← C言語などのfor文と比較 print range(5,10) # for(int i=5; i<10; ++i) print range(5,10,2) # for(int i=5; i<10;...
- ZeroMQ (zmq) の Python サンプルコードZeroMQ を Python から利用する場合のサンプルコードを記載します。 Fixing the World To fix the world, we needed to do two things. One, to solve the general problem of "how to connect any code to any code, anywhere". Two, to wra...
- Matplotlib/SciPy/pandas/NumPy サンプルコードPython で数学的なことを試すときに利用される Matplotlib/SciPy/pandas/NumPy についてサンプルコードを記載します。 Matplotlib SciPy pandas [NumPy](https://www.numpy
- pytest の基本的な使い方pytest の基本的な使い方を記載します。 適宜参照するための公式ドキュメントページ Full pytest documentation API Reference インストール 適当なパッケージ