flask网站部署实例

2024年5月9日 13:28 by wst

服务部署

问题

Flask网站开发好之后怎么部署呢?

答案

这里给出一个常规的方案:

前提:用conda安装的虚拟环境wxpay.

使用uwsgi+NGINX完成,步骤如下:

1. 配置uwsgi,内容(uwsgi_config.xml)如下:

<uwsgi>
     <home>/home/ubuntu/miniconda3/envs/wxpay</home>
     <pythonpath>/home/ubuntu/workspace/wxmppay</pythonpath> #网站根目录
     <module>main</module>                 #Flask的主入口文件,平时是直接运行这个文件启动测试服务器的
     <callable>app</callable>   #runServer.py入口文件里的程序入口
     <socket>127.0.0.1:5000</socket>       #监听端口
     <master/>
     <processes>2</processes>                #注:跑几个线程,这里用4个线程
     <memory-report/>
</uwsgi>

2. 配置NGINX文件,内容(zy.wanshitao.com.conf)如下:

server {
    listen 80;
    server_name  zy.wanshitao.com;

    root /home/ubuntu/workspace/wxmppay;

    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:5000;
    }

    location  /MP_verify_2KfyqMkJczW6S9zt.txt {
            #如果你的static目录不在root里,可以配置 alias /path/to/your/mysite/static;
            alias /home/ubuntu/workspace/wxmppay/MP_verify_2KfyqMkJczW6S9zt.txt;
        }


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/zy.wanshitao.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/zy.wanshitao.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {
    if ($host = zy.wanshitao.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name zy.wanshitao.com;
    listen 80;
    return 404; # managed by Certbot


}

3. 启动flask服务:

uwsgi -x uwsgi_config.xml -d uwsgi.log

说明

这里是采用了“微信公众号网页支付”项目中的配置文件。目录结构如下:

├── config.py
├── main.py
├── MP_verify_2KfyqMkJczW6S9zt.txt
├── nginx.conf
├── readme.md
├── requirements.txt
├── templates
│   └── unified_order.html
├── utils
├── uwsgi_config.xml
└── zy.wanshitao.com.conf

熟练的老铁应该能看明白,如有问题欢迎评论!


Comments(0) Add Your Comment

Not Comment!