centos环境下使用uwsgi&nginx部署flask

  1. 安装Python虚拟环境
    在centos中,系统已经集成了Python环境,直接可以使用,不用手动安装。

        1)pip install virtualenv             进行这一步要保证pip可以使用
        若在此报错gcc,则需要安装python-devel
        2)virtualenv   name    ——————创建虚拟环境
        3)source name/activate ————激活虚拟环境
        4)根据flask项目中需要的Python包安装
    
  2. 安装uwsgi

    1)pip install uwsgi
    2)在项目的根目录创建文件名为config.ini文件,用于启动uwsgi
    
    [uwsgi]
    
    socket = 127.0.0.1:5001     //启动程序时所使用的地址和端口,通常在本地运行flask项目,
                                //地址和端口是127.0.0.1:5000,
                                //不过在服务器上是通过uwsgi设置端口,通过uwsgi来启动项目,
                                //也就是说启动了uwsgi,也就启动了项目。
    chdir = /root/pyenv/AjaxExample    //项目目录
    
    wsgi-file = run.py      //flask程序的启动文件,通常在本地是通过运行  
                            //python run.py 来启动项目的
    
    callable = app      //程序内启用的application变量名
    
    processes = 4     //处理器个数
    
    threads = 2     //线程个数
    
    stats = 127.0.0.1:9191      //获取uwsgi统计信息的服务地址 用于和nginx通信
  3. 安装nginx

        *nginx的一些命令
        *启动: nginx
        *停止: nginx -s -stop
        *强制停止: pkill -9 nginx
        *查看nginx是否应用配置文件: nginx -t
        *查看版本: nginx -v
    
     1)nginx 可以使用yum直接安装
             yum install yum-fastestmirror————安装源
             yum install nginx
    
     2)配置nginx
             配置文件默认在/etc/nginx/nginx.conf
    
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;     //错误日志
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;  //web服务端口
        listen       [::]:80 default_server;  
        server_name  39.106.56.137;  //公网ip
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        location / {
        include        uwsgi_params;
                uwsgi_pass     127.0.0.1:5001;    //需要和uwsgi的配置文件里socket项的地址
                                                  //相同,否则无法让uwsgi接收到请求。
                uwsgi_param UWSGI_PYHOME /root/pyenv;  //python的位置(虚拟环境下)
                uwsgi_param UWSGI_CHDIR /root/pyenv/AjaxExample;   //项目根目录
                uwsgi_param UWSGI_SCRIPT run:app;    //启动项目的主程序(在本地上运行
                                                     //这个主程序可以在flask内置的
                                                     //服务器上访问你的项目)
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

至此,各项配置已经完成,不出意外的话,你就可以在浏览器中访问你的网站了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值