您的位置:首页 > 其它

centso上边部署flask项目 ngnix+uwsgi

2018-03-08 13:19 375 查看
项目根目录:/home/rs/myproject
---|app
----------|__init__.py
----------|views.py
---|logs
---|myenv
---|run.py
---|uwsgi.ini

一、安装uwsgipip install uwsgi
二、添加uwsgi配置文件在根目录下添加uwsgi.ini,内容如下:[plain] view plain copy[uwsgi]  
socket = 127.0.0.1:8001  
pythonpath = /home/rs/myproject  
module = run  
callable = app  
processes = 4  
threads = 2  

各参数介绍:
socket:通讯端口,外界可以通过127.0.0.1:8001访问,相当于我们在本地运行flask,并通过127.0.0.1:5000访问;并负责与nginx通信。
pythonpath:项目目录。
module:启动文件的文件名,我们可以在本地用python run.py启动flask项目。
callable:程序内启用的application变量名。
processes:处理器个数。
threads:线程数。
注意:最好不要在配置文件写中文注释,别问我为什么。

三、启动uwsgi
uwsgi uwsgi.ini

四、安装nginx
yum install nginx

五、修改nginx配置文件
配置文件的路径不尽相同,我的在/etc/nginx/nginx.conf。
修改如下(只修改标红部分即可):
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/ #   * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
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;
        listen       [::]:80 default_server;
        server_name  服务器公网ip;
        root         /usr/share/nginx/html;
        #root        /var/www/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:8001;
                uwsgi_param UWSGI_PYHOME /home/rs/myproject/myenv;
                uwsgi_param UWSGI_CHDIR /home/rs/myproject;
                uwsgi_param UWSGI_SCRIPT run:app;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}
六、启动nginx

执行nginx命令。启动后,在浏览器输入http://ip即可查看部署是否成功。

附:
流程(原理):web请求--->nginx代理--->uwsgi代理--->python项目

uwsgi启动:
uwsgi XX.ini
uwsgi停止:
sudo killall -9 uwsgi

nginx启动:
nginx
nginx停止:
nginx -s stop
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: