安装uWSGI
pip3 install uwsgi
启动命令
/usr/local/python3/bin/uwsgi --socket 0.0.0.0:8889 --workers run_server:app_server --master --processes 4 --threads 2 --stats 0.0.0.0:9191
在项目目录下新建
[uwsgi]
# web应用的入口模块名称
module = run_server:app
# 启动主进程
master = true
# 说明要启动5个子进程处理请求
processes = 3
# 程序内启用的application变量名
callable = app
# flask程序的启动文件,通常在本地是通过运行
wsgi-file = run_server.py
# 项目目录
chdir = /root/PythonWorkSpace/myproject
# 启动程序时所使用的ip和端口号
socket = 127.0.0.1:8001
# uwsgi日志路径
logto = /tmp/boom.log
chmod-socket = 660
vacuum = true
# 获取uwsgi统计信息的服务地址
stats = 127.0.0.1:9191
# uwsgi进程的pid,用于以后的stop和reload
pidfile = uwsgi.pid
buffer-size = 6553600
启动
uwsgi --ini myproject.ini
待验证
部署flask+uwsgi_nginx
原flask启动文件不需要修改,是什么样就是什么样
然后启动uwsgi/usr/local/python3/bin/uwsgi --socket 0.0.0.0:8889 --workers run_server:app_server --master --processes 4 --threads 2 --stats 0.0.0.0:9191
启动后ps -ef 查看进程,会有一个主进程,4个子进程
然后启动nginx
nginx配置文件如下
#user nobody;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream backend {
server 127.0.0.1:8889;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location / {
# proxy_pass http://backend;
# root html;
# index index.html index.html;
#}
location / {
include uwsgi_params;
uwsgi_pass backend; #监听的ip和端口号
#uwsgi_pass 127.0.0.1:8889; #监听的ip和端口号
uwsgi_param UWSGI_PYHOME /usr/local/python3; #pyathon环境
uwsgi_param UWSGI_CHDIR /tool_chain; #项目地址
uwsgi_param UWSGI_SCRIPT run_server:app_server; #web应用的入口模块名称
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}