项目根目录:/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
# wsgi-file = /home/lora/test/test1/test1.py
callable = app
processes = 4
threads = 2
daemonize = /home/lora/test/test1/server.log
各参数介绍:
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
部署websocket项目
因为要带websocket所以我们需要在uwsgi启动的配置文件中写上合适的配置项,比如像下面这个一样:
[uwsgi]
project = /root/ICManage
pythonpath = /root/ICManage
wsgi-file = /root/ICManage/manage.py
chdir = %(project)
module = manage
callable = app
master = true
processes = 1
#threads = 2
socket = 127.0.0.1:5050
chmod-socket = 664
#buffer-size = 32768
http-websockets = 1
gevent = 1000
async = 30
daemonize = /home/hips/ICManage/uwsgi/logs/server.log
project指出了项目目录。
%(project)是对已配置项project进行一个取值。
master设置是首先开启一个uwsgi的管理进程,然后由它开启若干个worker子进程,当子进程挂掉的时候还会自动重启。
备注:这些其实是对上面一般性配置描述的一个补充,并不是决定websocket特性的。
决定websocket特性的则是http-websockets,gevent,async这些配置项,他们指出了通过这个配置文件启动的uwsgi进程是支持websocket的(uwsgi版本在2.0之后才开始支持websocket)。另外还有一个很重要的改动:
1.processes改成了1,并且注释去掉了threads配置。如果不去掉threads,这会和gevent冲突,导致的现象就是通过nginx访问uwsgi程序时总会返回502 bad gateway。
2.如果processes设置大于1,那么导致的现象就是socket通信总是迟缓且没有规律。这主要是因为websocket的通信是要基于一个sessionid的,而每个进程接受请求时给出的sessionid都不同。uwsgi在做均衡的时候可能把发向某一个进程的请求发给了另一个进程,而那个进程显然没有处理这个请求的上下文,导致返回400 bad request,所以在socket通信时总是会涌现出大量的400和502错误。
把processes改成1显然不是一个万全之策,如此,性能上就出现了问题,这个要如何解决还有待研究。
然后贴出改造成兼容websocket之后的nginx配置,至少我是这么配置启动之后可以正常运行:
server {
listen 80;
server_name 192.168.1.101;
#charset koi8-r;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:5050;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}