nginx+uwsgi+flask
平时我们都是使用这种启动方式
nohup python3 app.py >/dev/null 2>&1 &
但是用uwsgi会更好。
例子:先创建虚拟环境
#直接创建
virtualenv -p /usr/bin/python3 venv
virtualenv venv
#激活进入虚拟环境
source venv/bin/activate
#如果想退出
deactivate
#-----------------
如何使用uwsgi启动flask呢?
1.先安装uwsgi
pip install uwsgi
2.写一个flask文件 app.py
3.写启动配置文件config.ini
[uwsgi]
http-socket = :5000
chdir = . #应该是项目目录
virtualenv = /home/venv
wsgi-file = app.py #启动文件
callable = app #文件里面的app变量
master = true
3.1直接复制这个,配置文件里面不要井号,不要写井号
[uwsgi]
http-socket = :5000
chdir = /demouwsgi/mumuByFlask
virtualenv = /demouwsgi/venv
wsgi-file = app.py
callable = app
master = true
4.启动命令
uwsgi --ini config.ini
5.测试是否成功
curl 127.0.0.1:5000
--------------------------
# 启动uWSGI服务器
uwsgi --ini uwsgi.ini
# 重启uWSGI服务器
sudo service uwsgi restart
# 查看所有uWSGI进程
ps aux | grep uwsgi
# 停止所有uWSGI进程
sudo pkill -f uwsgi -9
对nginx的操作---------------------
启动
/usr/local/nginx/sbin/nginx
#去修改配置信息
--------------------------------------
nginx与uwsgi交互
1.更改uwsgi的配置文件*.ini
[uwsgi]
...
#使用nginx连接时, 监控地址
socket=127.0.0.1:8080
#直接做web服务器时, 所监控地址
#http=127.0.0.1:8080
2.修改nginx的配置文件
server {
listen 80;
server_name localhost;
location / {
include uwsgi_params;
uwsgi_pass localhost:8080;
}
2.2,替换nginx配置文件
/usr/local/nginx/conf/nginx.conf
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass localhost:5000; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
3.重启nginx和uwsgi