【Flask】flask+uwsgi+nginx环境部署

在centos上,部署flask框架的环境,我选择了uwsgi和nginx

具体步骤为:

配置nginx+uwsgi

安装nginx  nginx/1.12.2
安装Flask  0.10.1
安装uwsgi  2.0.16(64bit)
安装uwsgi-plugin-python 2.0.16

创建一个flask项目

 1 #!/usr/bin/python
 2 # coding=utf-8
 3 
 4 from flask import Flask
 5 import sys
 6 reload(sys)
 7 
 8 print sys.path
 9 sys.path.append('/home/MySite')
10 sys.setdefaultencoding('utf-8')
11 
12 app = Flask(__name__)
13 
14 
15 @app.route('/')
16 def hello_world():
17     return '澳门最大在线赌场开业了!!!美女荷官在线发牌!'
18 
19 
20 if __name__ == '__main__':
21     app.run(port=8080,debug=True)

测试一下flask是否能够显示

注意

flask直接运行,只能在一台电脑上看到,因为是单线程的。
app.run(host='0.0.0.0',port=80)
腾讯云安全组中打开80端口即可访问。
其它主机应该类似。host中填公网地址无法启动。
ps..澳门xx会被腾讯云拦截。。。请换个健康和谐的内容
 
创建ini文件
 1 [uwsgi]
 2 base =/home/MySite   
 3 callable = app     #这个必须写,不然会报找不到application
 4 socket = :8081    #开启的端口,与nginx一致
 5 chdir =/home/MySite/   #指定项目路径
 6 wsgi-file =MySite.py    #指定flask运行的文件,是一个相对路径
 7 processes = 4   #4个进程,每个进程2个线程
 8 threads = 2
 9 chmod-socket = 666
10 plugins=python   #安装uwsgi-plugin-python后需要添加的一个参数
11 daemonize = mysite.log    #让uwsgi后台运行的,输出为一个Log
12 #stats = :8082

 

修改nginx配置文件

 1 user nginx;
 2 worker_processes auto;
 3 error_log /var/log/nginx/error.log;
 4 pid /run/nginx.pid;
 5 
 6 # Load dynamic modules. See /usr/share/nginx/README.dynamic.
 7 include /usr/share/nginx/modules/*.conf;
 8 
 9 events {
10     worker_connections 1024;
11 }
12 
13 http {
14     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
15                       '$status $body_bytes_sent "$http_referer" '
16                       '"$http_user_agent" "$http_x_forwarded_for"';
17 
18     access_log  /var/log/nginx/access.log  main;
19 
20     sendfile            on;
21     tcp_nopush          on;
22     tcp_nodelay         on;
23     keepalive_timeout   65;
24     types_hash_max_size 2048;
25 
26     include             /etc/nginx/mime.types;
27     default_type        application/octet-stream;
28 
29     # Load modular configuration files from the /etc/nginx/conf.d directory.
30     # See http://nginx.org/en/docs/ngx_core_module.html#include
31     # for more information.
32     include /etc/nginx/conf.d/*.conf;
33 
34     server {
35         listen       80;
36         server_name  localhost;
37         location / {
38         include /etc/nginx/uwsgi_params;
39         uwsgi_pass 127.0.0.1:8081;    #与ini文件对应
40         uwsgi_param UWSGI_CHDIR /home/MySite;
41         #uwsgi_param UWSGI_SCRIPT View:app;
42         #uwsgi_param SCRIPT_NAME /;
43         #uwsgi_pass unix:/home/MySite/uwsgi.sock;
44         }
45 
46         error_page 404 /404.html;
47             location = /40x.html {
48         }
49 
50         error_page 500 502 503 504 /50x.html;
51             location = /50x.html {
52         }
53     }
54   }

 

uwsgi启动命令,如果不写绝对路径需要在ini文件当前路径执行
1 uwsgi --ini abc.ini

 

nginx启动命令
/usr/sbin/nginx -c /etc/nginx/nginx.conf

 

启动这两个服务,就可以正常访问你的flask内容了。
 
 
 
 

 

posted @ 2018-06-10 19:15  铠甲巨人  阅读(3030)  评论(0编辑  收藏  举报