centos 配置 nginx 和 uwsgi 运行 python 脚本

安装 uwsgit

pip install uwsgi

添加响应文件。

vim /data/install/bin/pptserver.py

内容是:

cat /data/install/bin/pptserver.py 
from flask import Flask,render_template

app = Flask(__name__)

@app.route('/pptserver')
def index():
 return "Test message, you get it"

if __name__ == '__main__':
 app.run()

然后配置文件修改:

cat /data/install/conf/uwsgi.conf

[root@iZ23srfk8ttZ bin]# cat /data/install/conf/uwsgi.conf 
[uwsgi]
## For directlly http access
http-socket=127.0.0.1:8888

### For nginx proxy
#socket=127.0.0.1:8888
wsgi-file=/data/install/bin/pptserver.py
#plugins = python
callable = app
#chdir = /data/install/bin
touch-reload=/data/install/conf/
processes = 2
threads = 2
stats = 127.0.0.1:9191
post-buffering = 8192
buffer-size = 65535
socket-timeout = 10
uid = apache
gid = apache
master = true
#protocol = uwsgi

运行 uwsgi:
注意:当直接curl测试uwsgi的时候,配置为 http-socket=xxx
当使用nginx进行代理时,要配置为 socket=xxx

nohup uwsgi --ini /data/install/conf/uwsgi.conf &

检查服务:

#netstat -atnp | grep uwsgi
tcp        0      0 127.0.0.1:9191              0.0.0.0:*                   LISTEN      584/uwsgi           
tcp        0      0 127.0.0.1:8888              0.0.0.0:*                   LISTEN      584/uwsgi 

可以看到 9191端口是健康检查服务, 8888 端口是服务端口

测试下业务口:

#curl http://localhost:8888/pptserver
Test message, you get it

测试监控

#curl "http://localhost:9191"
 "version":"2.0.17.1",
 "listen_queue":0,
 "listen_queue_errors":0,
 "signal_queue":0,
 "load":0,
 "pid":584,
 "uid":48,
 "gid":0,
 "cwd":"/root",
 "locks":[
  {
   "user 0":0
  },
  {
   "signal":0
  },
  {
   "filemon":0
  },
  {
   "timer":0
  },
  {
   "rbtimer":0
  },
  {
   "cron":0
  },
  {
   "rpc":0
  },
  {
   "snmp":0
  }
 ],
 "sockets":[
  {
   "name":"127.0.0.1:8888",
   "proto":"http",
   "queue":0,
   "max_queue":100,
   "shared":0,
   "can_offload":0
  }
 ],
 "workers":[
  {
   "id":1,
   "pid":589,
   "accepting":1,
   "requests":0,
   "delta_requests":0,
   "exceptions":0,
   "harakiri_count":0,
   "signals":0,
   "signal_queue":0,
   "status":"idle",
   "rss":0,
   "vsz":0,
   "running_time":0,
   "last_spawn":1532316757,
   "respawn_count":1,
   "tx":0,
   "avg_rt":0,
   "apps":[
    {
     "id":0,
     "modifier1":0,
     "mountpoint":"",
     "startup_time":0,
     "requests":0,
     "exceptions":0,
     "chdir":""
    }
   ],
   "cores":[
    {
     "id":0,
     "requests":0,
     "static_requests":0,
     "routed_requests":0,
     "offloaded_requests":0,
     "write_errors":0,
     "read_errors":0,
     "in_request":0,
     "vars":[

     ],
     "req_info":     {

     }
    },
    {
     "id":1,
     "requests":0,
     "static_requests":0,
     "routed_requests":0,
     "offloaded_requests":0,
     "write_errors":0,
     "read_errors":0,
     "in_request":0,
     "vars":[

     ],
     "req_info":     {

     }
    }
   ]
  },
  {
   "id":2,
   "pid":590,
   "accepting":1,
   "requests":2,
   "delta_requests":2,
   "exceptions":0,
   "harakiri_count":0,
   "signals":0,
   "signal_queue":0,
   "status":"idle",
   "rss":0,
   "vsz":0,
   "running_time":2914,
   "last_spawn":1532316757,
   "respawn_count":1,
   "tx":206,
   "avg_rt":889,
   "apps":[
    {
     "id":0,
     "modifier1":0,
     "mountpoint":"",
     "startup_time":0,
     "requests":2,
     "exceptions":0,
     "chdir":""
    }
   ],
   "cores":[
    {
     "id":0,
     "requests":1,
     "static_requests":0,
     "routed_requests":0,
     "offloaded_requests":0,
     "write_errors":0,
     "read_errors":0,
     "in_request":0,
     "vars":[

     ],
     "req_info":     {

     }
    },
    {
     "id":1,
     "requests":1,
     "static_requests":0,
     "routed_requests":0,
     "offloaded_requests":0,
     "write_errors":0,
     "read_errors":0,
     "in_request":0,
     "vars":[

     ],
     "req_info":     {

     }
    }
   ]
  }
 ]
curl: (56) Failure when receiving data from the peer
}

配置 nginx 转发

配置 nginx 文件内容为:

location ~ /pptserver {
        include      uwsgi_params;
        uwsgi_pass   127.0.0.1:8888; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
    }

修改 uwsgi的配置为:

[root@iZ23srfk8ttZ bin]# cat /data/install/conf/uwsgi.conf 
[uwsgi]
## For directlly http access
#http-socket=127.0.0.1:8888

### For nginx proxy
socket=127.0.0.1:8888
wsgi-file=/data/install/bin/pptserver.py
#plugins = python
callable = app
#chdir = /data/install/bin
touch-reload=/data/install/conf/
processes = 2
threads = 2
stats = 127.0.0.1:9191
post-buffering = 8192
buffer-size = 65535
socket-timeout = 10
uid = apache
gid = apache
master = true
#protocol = uwsgi

然后测试nginx的接口:

curl "http://ginkgoo.org/pptserver"
Test message, you get it
This entry was posted in 运维相关. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Both comments and trackbacks are currently closed.