Centos7 环境下部署flask网站步骤

自己之前做了一个flask项目,完成后部署在腾讯云,过程由踩一点坑,这里记录下过程。在部署之前,需要先配置centos7环境,具体链接为:
centos7 环境配置

1、安装依赖包
在虚拟环境中安装好所有包,例如:

pip install -r requements.txt

2、关闭调试模式 app.debug = False,并且安装flask-script插件来进行命令行启动项目,如下:

pip install flask-script
from flask_script import Manager

manage = Manager(app)

if __name__ == "__main__":
    manage.run()

3、在init文件中修改mysql数据库连接(ip地址等),然后在navicat中远程连接到服务器的mysql,进行数据传输,这样是最省时的。
4、配置nginx 反向代理
nginx配置文件:

worker_processes 4;
events {
    worker_connections 262140;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;
    limit_conn_zone $binary_remote_addr zone=addr:5m;
    upstream movie {
        server 127.0.0.1:5000;
        server 127.0.0.1:5001;
        server 127.0.0.1:5002;
        server 127.0.0.1:5003;
        server 127.0.0.1:5004;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
            root html;
            index index.html index.htm;
            proxy_pass http://movie;
        }
        location ~ \.flv$ {
            flv;
            limit_conn addr 4;
            limit_rate 1024k;
            rewrite ^/static/uploads/movies/(.+?).flv$ /flask_movie/app/static/uploads/movies/$1.flv permanent;
        }
        location ~ \.mp4$ {
            mp4;
            limit_conn addr 4;
            limit_rate 1024k;
            rewrite ^/static/uploads/movies/(.+?).mp4$ /flask_movie/app/static/uploads/movies/$1.mp4 permanent;
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
    }
}

然后将配置文件拷贝至nginx目录中,覆盖原有配置文件:

cp nginx.conf /usr/local/nginx/conf/
ubuntu: cp nginx.conf /etc/nginx/conf.d

将项目拷贝到nginx的html目录:

cp -r movie_project /usr/local/nginx/html

然后重启nginx:

nginx -s stop
nginx
nginx -s reload

启动项目:

cd /usr/local/nginx/html/movie_project

python manage.py runserver

5、开启多个端口后台运行

nohup python manage.py runserver -h 127.0.0.1 -p 5001 &

然后 ctr + c

nohup python manage.py runserver -h 127.0.0.1 -p 5002 &

然后 ctr + c

nohup python manage.py runserver -h 127.0.0.1 -p 5003 &

然后 ctr + c

nohup python manage.py runserver -h 127.0.0.1 -p 5004 &

这样就开启了四个端口,输入如下命令可以查看:

netstat -anptu | grep 500*

6、流媒体访问限制
限制单个ip能发起的连接: limit_conn addr 1;
限制视频速率: limit_rate 1024k;
刷新nginx nginx -s reload

Last modification:November 25th, 2019 at 09:21 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment