linux+python虚拟环境下flask使用gunicorn+nginx高并发部署

欢迎关注原创视频教程

Python微信订餐小程序课程视频

https://edu.csdn.net/course/detail/36074

Python实战量化交易理财系统

https://edu.csdn.net/course/detail/35475## 背景

总有人问我博客怎么部署,这里简单写下过程

1、下载源码

$ git clone https://gitee.com/pojoin/h3blog.git
$ cd h3blog
$ python -m venv venv  #创建python虚拟环境
$ source venv/bin/activate
$ 
$ pip install -r requirements.txt # 安装项目依赖,可能不全,根据提示自行安装即可

2、修改配置文件

$ vim .env

FLASK_CONFIG=production
DATABASE_URL=mysql+pymysql://root:123456@127.0.0.1/h3blog?charset=utf8

H3BLOG_UPLOAD_TYPE=qiniu
QINIU_CDN_URL=http://cdn.h3blog.com/
QINIU_BUCKET_NAME=h3blog
QINIU_ACCESS_KEY=Kio7HR9o7mUpSFdLjLwJvKxAaeTAgnXpJ4hT-h_J
QINIU_SECRET_KEY=eEHXUpudwApbuTRWQl0k1mbJZwO1Pv-iWx77Mltd

H3BLOG_DOMAIN=https://www.h3blog.com
BAIDU_PUSH_TOKEN=TagL7I7By8xm4Nm5

SITEMAP_URL_SCHEME=https

3、安装gunicorn

pip install gunicorn

4、配置gunicoren开机自启

# vim /etc/systemd/system/gunicorn_h3blog.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/data/h3blog-flask
ExecStart=/data/h3blog-flask/venv/bin/gunicorn --access-logfile - --workers 2 --bind unix:/data/h3blog-flask/h3blog.sock wsgi:app

[Install]
WantedBy=multi-user.target

开机自启

# systemctl enable gunicorn_h3blog.service

启动gunicorn

# systemctl start gunicorn_h3blog.service

5、安装nginx

apt install nginx

6、配置nginx

# vim /etc/nginx/sites-available/h3blog.com
server {
    listen 80;
    server_name www.h3blog.com h3blog.com;

    gzip on;
    gzip_buffers 32 4K;
    gzip_comp_level 6;
    gzip_min_length 100;
    gzip_types application/javascript text/css text/xml;
    gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
    gzip_vary on;

    client_max_body_size 10M;

    location /root.txt {
                alias /data/taobaoke/root.txt;
        }
    location /js/ {
                alias /data/h3blog-flask/app/main/themes/tend/static/js/;
                autoindex on;
    }
    location /css/ {
                alias /data/h3blog-flask/app/main/themes/tend/static/css/;
                autoindex on;
        }
     location /img/ {
                alias /data/h3blog-flask/app/main/themes/tend/static/img/;
                autoindex on;
        }

    location / {
        include proxy_params;
        proxy_pass http://unix:/data/h3blog-flask/h3blog.sock;
    }

    #return 301 https://$server_name$request_uri;
}

server {
    listen 443;
    server_name www.h3blog.com h3blog.com;
    ssl on;
    ssl_certificate   cert/h3blog.com/3852674_h3blog.com.pem;
    ssl_certificate_key  cert/h3blog.com/3852674_h3blog.com.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    gzip on;
    gzip_buffers 32 4K;
    gzip_comp_level 6;
    gzip_min_length 100;
    gzip_types application/javascript text/css text/xml;
    gzip_disable "MSIE [1-6]\."; #配置禁用gzip条件,支持正则。此处表示ie6及以下不启用gzip(因为ie低版本不支持)
    gzip_vary on;

    client_max_body_size 10M;

    location /js/ {
                alias /data/h3blog-flask/app/main/themes/tend/static/js/;
                autoindex on;
    }
        location /css/ {
                alias /data/h3blog-flask/app/main/themes/tend/static/css/;
                autoindex on;
        }
        location /img/ {
                alias /data/h3blog-flask/app/main/themes/tend/static/img/;
                autoindex on;
        }

    location / {
        include proxy_params;
        proxy_pass http://unix:/data/h3blog-flask/h3blog.sock;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

虚坏叔叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值