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

在centos上,部署flask框架的環境,我選擇了uwsgi和nginxhtml

具體步驟爲:node

配置nginx+uwsgi

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

建立一個flask項目nginx

 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

注意centos

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內容了。
相關文章
相關標籤/搜索