Python uWSGI部署服务

什么是uWSGI

uWSGI旨在为部署分布式集群的网络应用开发一套完整的解决方案。主要面向web及其标准服务。由于其可扩展性,能够被无限制的扩展用来支持更多平台和语言。uWSGI是一个web服务器,实现了WSGI协议,uwsgi协议,http协议等。
uWSGI的主要特点是:

  • 超快的性能
  • 低内存占用
  • 多app管理
  • 详尽的日志功能
  • 高度可定制

uWSGI服务器自己实现了基于uwsgi协议的server部分,我们只需要在uwsgi的配置文件中指定application的地址,uWSGI就能直接和应用框架中的WSGI application通信

安装uWSGI

pip install uwsgi

uWSGI配置文件

这里给个样例作为参考

[uwsgi]
# 指向项目目录
chdir = /XXX/XXX/XXXX

# 外部访问地址,主要是指定端口号。可以指定多种协议:http 或 socket
socket = 127.0.0.1:8888
# http = 127.0.0.1:8888

# flask启动程序文件
wsgi-file = XXX.py

# flask在manage.py文件中的app名
callable = app

# 处理器数
processes = 4

# 存储日志信息
logto = /XXX/XXX/XXX.log

# 线程数
threads = 8

# 设置uwsgi的pid文件
pidfile = /XXX/XXX.pid

# 设置uwsgi的status文件
statusfile = /XXX/XXX.status

Nginx反向代理配置文件

这里给个样例作为参考

# the upstream component nginx needs to connect to
upstream XXX {
    server 127.0.0.1:7777;
}

# configuration of the server
server {
    # the port your site will be served on
    listen     1002;
    # the domain name it will serve for
    #server_name .example.com; # substitute your machine's IP address or FQDN
    server_name XXXX;
    charset     utf-8;

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  XXX;
        include     uwsgi_params; 
        # the uwsgi_params file you installed
    }
}

PS. 对于新建的配置文件,需要在nginx.conf里面加入引入路径:include vhosts/*.conf

user  work;
worker_processes  auto;

#error_log  logs/error.log;
error_log  /data/logs/nginx/host/logs/host_error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    use epoll;
    accept_mutex off;
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent $request_body "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';

    access_log  /data/logs/nginx/host/logs/host_access.log  main;

    server_tokens   off;
    sendfile        on;
    #tcp_nopush     on;
    tcp_nodelay    on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    client_header_buffer_size 4k;
    client_max_body_size 20M;

    open_file_cache max=102400 inactive=30s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 0;

    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript  application/javascript application/json;

include vhosts/*.conf;
}

常见的命令

  • 启动uwsgi服务
    uwsgi --ini uwsgi.ini
    
  • 查看uwsgi的pid号
    cat uwsgi/uwsgi.pid
    
  • 查看一下uwsgi的进程
    ps aux | grep uwsgi
    
  • 重启uwsgi
    uwsgi --reload uwsgi/uwsgi.pid
    
  • 停止uwsgi
    uwsgi --stop uwsgi/uwsgi.pid
    
  • 查看uwsgi的版本
    uwsgi --version
    
  • 测试配置文件是否有语法错误
    nginx -t 
    
  • 重启Nginx
    nginx -s reload
    
  • 测试配置文件是否有语法错误
    nginx -t 
    
  • 强制停止Nginx服务
    nginx -s stop 
    
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值