centos7上部署flask项目 ngnix+uwsgi

匿名 (未验证) 提交于 2019-12-02 22:56:40

项目根目录:/home/lora/test/test1

---|test1

------|test1.py

------|uwsgi.ini

一、安装uwsgi

 pip install uwsgi

二、添加uwsgi配置文件

在根目录下添加uwsgi.ini,内容如下:

 [uwsgi] socket = 127.0.0.1:8001   pythonpath = /home/lora/test/test1   module = test1 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。
修改如下(主要在server{......}):

 # 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;         # listen       [::]:80 default_server;         # server_name  服务器公网ip;         server_name  localhost;         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/lora/test/test1;                 uwsgi_param UWSGI_SCRIPT test1: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 xxx.ini


uwsgi停止:

 sudo killall -9 uwsgi


nginx启动:

 nginx -c nginx.conf


nginx停止:

 nginx -s stop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!