uWSGI命令
- 查看uWSGI的进程:
ps aux | grep uwsgi
- 查看Nginx的进程:
ps aux | grep ngnix
- 列出端口占用:
netstat -lpnt
- 启动uWSGI:
uwsgi --ini uwsgi.ini
- Nginx错误日志:
vim /usr/local/webserver/nginx/logs/error.log
端口命令
- 显示系统端口情况:
netstat -anp
练习
1、建立一个文件flask_app.py
[root@linux ~]# vi flask_app.py
##### file start ####
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hi,This is a Python Web program use FLASK and Nginx by uWSGI!'
##### file end ###
2、配置Nginx文件
location / {
include uwsgi_params;
#uwsgi_pass unix:/home/two/two.sock; #uWSGI与Nginx交流的方式一
uwsgi_pass 127.0.0.1:3031; #uWSGI与Nginx交流的方式二
}
3、运行uWSGI命令
方式一
uwsgi -s 127.0.0.1:3031 -t 30 -M -p 4 --limit-as 128 -R 10000 -w flask_app:app -d /home/two/run.log
该命令行下不会出现运行信息,即后台运行,因为运行信息已经被写入到run.log里面去了。
结束方式
- 查询uWSGIPID:
ps -ef | grep uwsgi
- 关闭uWSGI:
kill -9 查询到的PID号
方式二
uwsgi -s 127.0.0.1:3031 -t 30 -M -p 4 --limit-as 128 -R 10000 -w flask_app:app
当前命令行下会打印运行信息,前台运行。
关闭方式:Ctrl + c
命令解释:
-s 使用默认协议绑定到指定的 UNIX/TCP 套接字
-p 产生指定数量进程
-w 加载一个 WSGI 模块
-d 以守护进程方式运行,访问日志文件/var/log/uwsgi_run.log
-t 30 设置 30s 的超时时间, 超时后, 自动放弃该链接
--limit-as 128 将进程的总内存量控制在 128M
-R 10000 开启 10000 个进程后, 自动 respawn 下
————————————————————
配置uWSGI的另一种办法
Nginx配置
http {
include mime.types;
default_type application/octet-stream;
#上传数据设置
client_max_body_size 100M;
client_body_buffer_size 128k;
sendfile on;
keepalive_timeout 100;
server {
listen 80; #监听端口
server_name liefyuan.top; #域名
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
uwsgi_param UWSGI_PYHOME /root/.virtualenvs/env; # 指向虚拟环境目录
uwsgi_param UWSGI_CHDIR /home/four; # 指向网站根目录
uwsgi_param UWSGI_SCRIPT main:app; # 指定启动程序
}
location /static {
alias /home/four/static;
}
uWSGI配置
uwsgi.ini
[uwsgi]
socket = 127.0.0.1:5000
processes = 4 #注:跑几个进程,这里用4个进程
threads = 2
master = true
pythonpath = /home/four
module = main
callable = app
memory-report = true
vacuum = true
wsgi-file = main.py#启动文件
stats = 127.0.0.1:5001#状态查询端口
post-buffering = 65536
buffer-size = 65536
daemonize = /home/four/logs/run.log
- 进程数设置
processes = 4
- 线程数设置
threads = 40
- 超时设置
harakiri-verbose = true
harakiri = 300
- 缓冲区设置
post-buffering = 65536
# 不设置会导致上传大文件失败
buffer-size = 65536
- pythonpath设置,可以有多个
pythonpath = /path1
pythonpath = /path2
pythonpath = /path3
uid 、gid :以哪个用户、组启动服务
socket:监听的ip及端口
master: 启动主进程
workes:服务器启动的进程数
reload-mercy:平滑的重启
pidfile :启动时的pid 文件。
daemonize :启动日志
module :web应用的入口模块名称
enable-threads : 启用线程模式