Flask部署

python flask
创建于:2019年07月05日 更新于:2019年09月02日

Linux+Nginx+Uwsgi+Supervisor部署Flask

部署前需要关闭防火墙:

iptables -F

安装python3

环境centos7.2

下载、解压(编译安装,把下载好的python3安装包解压):

Linux下默认系统自带python2.7的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装python3和python2共存

  首先去python官网下载python3的源码包,网址:https://www.python.org/

  进去之后点击导航栏的Downloads,也可以鼠标放到Downloads上弹出菜单选择Source code,就是源码包的意思,这里选择最新版本36.2,当然下面也有很多其他历史版本,点进去之后页面下方可以看到下载链接,包括源码包、Mac OSX安装包、Windows的安装包

tar -xvf Python-3.6.2.tgz


进入目录:

cd Python-3.6.2/

添加配置:

./configure --with-ssl --prefix=/usr/python

这里配置自己的安装目录,接下来编译源码、安装:

make && make install

如果出现错误提示缺少zlib…啥的则需要安装zlib相关依赖包

yum -y install zlib*

注:如果安装后出现Ignoring ensurepip failure: pip 9.0.1 requires SSL/TLS

yum install -y openssl-devel

2、进入 Python安装包,修改Module路径的setup文件:

vim Modules/Setup

找到下面一行代码,去掉注释:

#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
去掉注释
zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz

系统中原来的python在/usr/bin/python,通过ls -l可以看到,python是一个软链接,链接到本目录下的python2.7

我们可以把这个删除,也可以新建一个python3的软链接,只不过执行时python要改成python3,或者python脚本头部声明要改为#!/usr/bin/python3

ln -s /usr/python/bin/python3 /usr/bin/python3

安装pip

wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
python3 get-pip.py

如果pip安装模块时提示

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

则需要安装openssl

yum install -y openssl-devel

然后重新编译安装即可

安装virtualenv

pip install virtualenv
mkdir myproject
cd myproject
virtualenv --no-site-packages venv (指定python版本:virtualenv -p /usr/bin/python3 venv)
linux:source venv/bin/activate windows:venv\Scripts\activate
deactivate 退出

注:如果出现 bash: pip: command not found

首先查找pip安装目录 find / -name pip

然后建立软链 ln -s /usr/python/bin/pip /usr/bin/pip

安装uwsgi

pip install uwsgi

配置uwsgi

在网站根目录下创建config.ini文件,写入以下内容

[uwsgi]
# uwsgi 启动时所使用的地址与端口
socket = 127.0.0.1:8001
# 指向网站目录
chdir = /home/project/flask/
# python 启动程序文件
wsgi-file = manage.py
# python 程序内用以启动的 application 变量名
callable = app
# 处理器数
processes = 4
# 线程数
threads = 2
#状态检测地址
stats = 127.0.0.1:9191

安装nginx

yum -y install nginx

配置nginx

vim /etc/nginx/conf.d/default.conf
server{
    listen 80;
    server_name www.chenyy.xin;
    root /home/project/flask;
    location / {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8001; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
        uwsgi_param UWSGI_PYHOME /home/project/flask/flask_env361; # 指向虚拟环境目录
        uwsgi_param UWSGI_CHDIR /home/project/flask; # 指向网站根目录
        uwsgi_param UWSGI_SCRIPT manage:app; # 指定启动程序
    }
}


#开启
/bin/systemctl start nginx.service

#重启
/bin/systemctl restart nginx.service

安装supervisor

yum install python-setuptools
pip install supervisor

测试是否安装成功

echo_supervisord_conf

创建配置文件

mkdir -m 755 -p /etc/supervisor/
cd /etc
echo_supervisord_conf > /etc/supervisor/supervisord.conf
cd supervisor

创建项目配置文件目录

mkdir -m 755 conf.d
cd /etc/supervisor/conf.d
vim flask.conf

[program:flask]
command =/home/project/flask/flask_env361/bin/uwsgi /home/project/flask/config.ini
directory =/home/project/flask
user =root
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true
stderr_logfile=/home/project/log/error.log#要确保该目录存在

在主配文档中引入flask.conf

vim supervisord.conf

找到include

;[include]
;files = relative/directory/*.ini
//改为
[include]
files = ./conf.d/*.conf

启动supervisor

supervisord -c supervisord.conf

如果出现以下错误

Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
For help, use /root/.pyenv/versions/2.7.6/bin/supervisord -h

找到 supervisor.sock

unlink supervisor.sock

重新启动supervisor

用supervisorctl查看已经被监控的program

(flask_env361) [root@iZ2zefw4wlcm0he1pjkawoZ supervisor]# supervisorctl
flask RUNNING pid 1764, uptime 0:59:45

如果supervisorctl没效果,换成

supervisorctl -c /etc/supervisor/supervisord.conf

重启监控服务

supervisorctl reload

异常解决

[root@iZ2zefw4wlcm0he1pjkawoZ supervisor]# supervisorctl
unix:///tmp/supervisor.sock no such file
supervisor> reload
Really restart the remote supervisord process y/N? y
error: <class  'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224
supervisor> status
unix:///tmp/supervisor.sock no such file
supervisor> exit
[root@iZ2zefw4wlcm0he1pjkawoZ supervisor]# supervisord
/usr/lib/python2.7/site-packages/supervisor/options.py:298: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
'Supervisord is running as root and it is searching '
[root@iZ2zefw4wlcm0he1pjkawoZ supervisor]# supervisorctl
flask RUNNING pid 2 2995, uptime 0:00:15

安装mysql

1.下载MySQL源

wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm

2.安装MySQL源

sudo rpm -ivh mysql57-community-release-el7-7.noarch.rpm

3.安装mysql-community-server

sudo yum install mysql-community-server -y

4.重启mysql

systemctl restart mysqld
注:自动启动服务用 systemctl enable mysqld

5.查看临时密码

grep 'A temporary password' /var/log/mysqld.log

6.数据库修改密码

mysql -u root -p
set global validate_password_policy=0;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx';

yum -y install mysql-devel

查看日志

tail -f /var/log/mysqld.log