0x00 前言
如果用于生产环境假如用户量不大的话,我个人还是喜欢直接在服务器上运行 mamage.py 简单粗暴。
但是,连官方文档都好像介意你这么做……
有一种不好的编码方式就不要用 uwsgi 来部署了,哪一种?比方我做的这种:def Default():
rc = redis.Redis(host='127.0.0.1',port='6379')
while True:
time.sleep(30)
rc.publish("APPSUB_MODINFO_PUBLIC", '{"form":"WEB_Default","data":{"time":"'+ str(time.clock()) +'"}}')
if __name__ == '__main__':
HOST = environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
_thread.start_new_thread(Default,())
app.run(HOST,PORT)
如果你时这样不按常理出牌写的代码,uwsgi可能真的无能为力。 __name__ 里的 _thread 一定不会执行。
0x01 系统环境
我有个不按常理出牌的底层环境,Docker 当 VM 用。
所以,如果你是虚拟机或者是真机请确保是 centos7 或最新的系统。
以下开始无废话,全是重点:
系统环境:centos7.2
软件环境安装:yum install gcc
yum install python36
yum install python3-devel
pip3 install uwsgi
yum install nginx
0x02 uwsgi 配置vi /var/html/uwsgi.ini #uwsgi配置文件,你可以创建到其他地方
[uwsgi]
socket = 127.0.0.1:5000 #监听端口
chdir = /var/html #flask网站目录
wsgi-file = manage.py #flask启动文件
callable = app #flask的命名空间,默认就是app
processes = 2 #没性能要求默认1
threads = 2 #没性能要求默认1
stats = 127.0.0.1:5001 #状态端口
logto = /var/log/ser.log #日志文件
0x03 Nginx 配置# Nginx yum 安装的默认配置文件: /etc/nginx/nginx.conf
vi /etc/nginx/nginx.conf
#修改:
server{
#原始配置不用改,只需要改下面这个即可:
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:5000;
}
}
完整配置文件:# 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 _;
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:5000;
}
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 {
# }
# }
}
0x04 启动服务uwsgi /var/html/uwsgi.ini # uwsgi [config file]
nginx # 启动Nginx
0x05 简要说明
uwsgi 是启动flask网站的,Nginx 通过 wsgi 来访问 flask网站。