安装uWSGI

https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html

apt-get install build-essential python3-dev

pip3 install uwsgi

使用uWSGI

运行

运行uwsgi,假设有run.py并且使用python3 run.py可以运行项目

uwsgi --socket 0.0.0.0:5000 --protocol=http -w run:app

配置文件

创建uwsgi.ini

[uwsgi]
module = run:app

master = true
processes = 5

http = 0.0.0.0:5000

使用配置文件运行

uwsgi --ini uwsgi.ini

如果要使用nginx,应该使用socket

[uwsgi]
module = run:app

master = true
processes = 5

socket = /tmp/flask.sock
chmod-socket = 666

chdir = {path}
logto = {path}

添加一个服务

使用systemd

systemd 文件的路径在 /etc/systemd/system

创建一个文件uwsgiproject.service

[Unit]
Description=uWSGI instance to serve myproject
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory={path}//工作目录
Environment=FLASK_SETTINGS={path}//环境变量
ExecStart=/usr/local/bin/uwsgi --ini {path to uwsgi.ini}

[Install]
WantedBy=multi-user.target
sudo systemctl start uwsgiproject
sudo systemctl stop uwsgiproject
sudo systemctl restart uwsgiproject

nginx

新建配置文件

server {
    listen 80;
    server_name {domain or ip};

        location / {
                include uwsgi_params;
                uwsgi_pass unix:/tmp/flask.sock;#和配置文件中的socket对应
        }

}

增加软链接到sites-enabled中

sudo ln -s /etc/nginx/sites-available/{filename} /etc/nginx/sites-enabled

测试nginx

nginx -t 

使配置生效

service nginx reload

如果权限设置成600,会出现502错误,查看日志是没有权限的错误

less /var/log/nginx/error.log

*81 connect() to unix:/tmp/flask.sock failed (13: Permission denied) while connecting to upstream