CentOS7.5+Python3.7.3+Nginx1.16.0+MySQL5.6+uWSGI部署上线Flask项目 --项目部署

1、Python3环境的安装

请参考个人其它文章:Installing stable Python3.7.3 in CentOS7.5 --项目部署python

2、Nginx环境的安装

请参考个人其它文章:Installing the stable nginx-1.16.0 in CentOS7.5(source installation) --项目部署mysql

3、MySQ环境的安装

请参考个人其它文章:Installing and configuring MySql5.6 In CentOS7.5 --项目部署
安装完成后,将开发环境中的数据库文件导出:nginx

thanlon@thanlon-Ubuntu:~$ mysqldump -uroot -p blueflag>db.sql

上传到服务器中,sql

thanlon@thanlon-Ubuntu:~$ scp db5.15.sql root@106.12.115.123:/root/db_file/

建立和use数据库后,经过source指令导入到MySQL中,数据库

source db.sql
4、上传项目源码到服务器中
  1. 在CentOS中的/root目录下建立flask文件夹用来存放项目文件
[root@instance-mtfsf05r ~]# mkdir flask
  1. 上传项目源码到服务其中(个人开发环境是Ubuntu,因此我就经过scp命令上传了)
thanlon@thanlon-Ubuntu:~/pythonWeb$ scp -r blueflag root@106.12.115.136:/root/flask
5、安装Python虚拟化环境
  1. 经过pip3命令安装virtualenv
[root@instance-mtfsf05r ~]# pip3 install virtualenv
  1. 在项目根目录中建立虚拟环境文件目录(文件名自定义,这里是blueflagvenv
[root@instance-mtfsf05r ~]# cd flask/blueflag
[root@instance-mtfsf05r blueflag]# virtualenv blueflagvenv
  1. 激活虚拟化环境
[root@instance-mtfsf05r blueflag]# source blueflagvenv/bin/activate

激活后出现虚拟化环境文件目录名子:(blueflagvenv) [root@instance-mtfsf05r blueflag]#,说明激活成功,当前正处于Python虚拟环境flask

6、安装项目须要的第三方库

根据项目的需求安装(在虚拟环境中安装),例如:个人这些库segmentfault

(blueflagvenv) [root@instance-mtfsf05r blueflag]# pip install flask
(blueflagvenv) [root@instance-mtfsf05r blueflag]# pip install wtforms
(blueflagvenv) [root@instance-mtfsf05r blueflag]# pip install flask_sqlalchemy
(blueflagvenv) [root@instance-mtfsf05r blueflag]# pip install requests
(blueflagvenv) [root@instance-mtfsf05r blueflag]# pip install pymysql
(blueflagvenv) [root@instance-mtfsf05r blueflag]# pip install flask_script

还可使用:安全

thanlon@thanlon-Ubuntu:~$ pip3 freeze>blueflag.txt

导出依赖包,而后直接经过:服务器

(blueflagvenv) [root@instance-mtfsf05r blueflag]# pip install -r blueflag.txt

安装这些依赖包。网络

7、安装和配置uWSGI
  1. 安装
(blueflagvenv) [root@instance-mtfsf05r blueflag]# pip install uwsgi
  1. 在项目根目录下建立一个配置文件uwsgiconf.ini,文件名能够自定义
(blueflagvenv) [root@instance-mtfsf05r blueflag]# touch uwsgiconf.ini
  1. 配置uwsgiconf.ini文件

在uwsgiconf.ini文件中写入下面代码块的内容:(记得把注释所有删掉)

[uwsgi]
socket = 127.0.0.1:8001#启动程序时所使用的地址和端口

chdir = /root/flask/blueflag/#项目目录

wsgi-file = manager.py#项目程序启动文件

callable = app#程序内启用的application变量名字

processes = 2#处理器的数量,个人是2个处理器

threads = 2 #线程数量

stats = 127.0.0.1:9191#获取uwsgi统计信息的服务地址
~

这样安装和配置uWSGI就完成了

8、配置Nginx

配置Nginx做为代理的目的是保证项目的安全和负载均衡。当存在网络请求时,Nginx先进行处理,而后再将请求交给uWSGI处理。

  1. 打开Nginx配置文件nginx.conf
[root@instance-mtfsf05r ~]# vi /usr/local/nginx/conf/nginx.conf
  1. 修改nginx.conf中的内容以下:(http{}中花括号中的server{})
server {
        listen       80;
        server_name  106.12.123.123;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        access_log      /root/flask/blueflag/logs/access.log;#服务器接收的请求日志
        error_log       /root/flask/blueflag/logs/error.log;#错误日志

        locatior / {
            include uwsgi_params; #这里是导入的uwsgi配置
            uwsgi_pass     127.0.0.1:8001;#须要和uwsgi的配置文件里socket项的地址
            uwsgi_param UWSGI_PYHOME /root/flask/blueflag/blueflagvenv;#python的位置(虚拟环境下)
            uwsgi_param UWSGI_CHDIR  /root/flask/blueflag;#项目根目录
            uwsgi_param UWSGI_SCRIPT manager:app;
        }
9、启动Nginx与uWSGI
  1. 启动Nginx
[root@instance-mtfsf05r ~]# nginx
  1. 到项目根目录blueflag(这是个人目录)下启动uWSGI,注意要激活Python虚拟环境中,这里在虚拟环境中安装了uwsgi
[root@instance-mtfsf05r blueflag]# cd /root/flask/blueflag/
[root@instance-mtfsf05r blueflag]# source blueflagvenv/bin/activate
(blueflagvenv) [root@instance-mtfsf05r blueflag]# uwsgi uwsgiconf.ini
  1. 经过服务器IP地址访问你的项目,正常访问,则部署上线成功
    在这里插入图片描述

    参考文章:CentOS下用Nginx和uwsgi部署flask项目

相关文章
相关标签/搜索