python flask环境搭建

 

记录一下python测试环境搭建过程

先 安装 git 下载代码,此处省略 /var/www/fm/order

 1. python安装

yum -y groupinstall "Development tools"

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel  gdbm-devel db4-devel libpcap-devel xz-devel expat-devel libffi-devel

# 下载 python 3.6
wget -c https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tar.xz

tar -Jxvf Python-3.6.6.tar.xz

# 切到源码文件夹下,我一般在这个位置
cd /usr/local/python3/Python-3.6.6

#配置编译安装选项(这里指定了安装路径为/usr/local/python3)
./configure --prefix=/usr/local/python3 

#编译
make

#安装
make install

# 查看版本
python -v

 2. 因数据库用的sqlserver,安装odbc。并配置FreeTDS文件

# 安装环境依赖 gcc-c++
yum install gcc-c++

# 安装环境依赖 unixODBC
yum install unixODBC-devel

#安装pyodbc
pip3 install pyodbc


#修改配置文件 /etc/odbcinst.ini
#odbc FreeTdS配置
[FreeTDS]
Description     = FreeTDS unixODBC Driver
Driver          = /usr/lib64/libtdsodbc.so.0
Setup           = /usr/lib64/libtdsodbc.so.0
UsageCount      = 1

3. 安装 virtualenv

# 创建工程代码目录都放在fm下, order为其中一个项目, log为日志
mkdir /var/www/fm  /var/www/fm/order /var/www/fm/log

# 创建 virtualenv 环境目录
mkdir /home/dev/.pyenv /home/dev/.pyenv/order


# 安装virtualenv
pip3 install virtualenv

# 为工程创建venv虚拟环境, -p为指定要使用的python解释器,.pyenv 及其下 order 为虚拟环境存放目录
virtualenv -p python3 /home/dev/.pyenv

virtualenv -p python3 /home/dev/.pyenv/order


# 进入order项目
cd /home/dev/.pyenv/order

# 进入order项目的 虚拟环境
source venv/bin/activate 


#先安装 gunicorn
pip3 install gunicorn

# 安装项目依赖包
pip3 install -r /var/www/fm/order/requirements.txt

# 退出虚拟环境
deactivate

此时可以测试一下,项目是否能成功启动

#在项目根目录下
cd /var/www/fm/order

# 启动
/home/dev/.pyenv/flask-api/bin/gunicorn --worker-class=gevent --max-requests 5000 -w 4 -b 127.0.0.1:8200 fm_poster:app

 

 4. 安装supervsior ,管理服务


# 使用pip安装supervisor,系统默认的python2
pip install supervisor
 
# 新建supervisor配置目录
mkdir /etc/supervisor /etc/supervisor/conf.d

# 生成supervsior配置文件
echo_supervisord_conf > /etc/supervisor/supervisord.conf

# 修改supervisord.conf配置

# 启动supervsior
supervisord -c /etc/supervisor/supervisord.conf
 
# 进入supervsior管理终端, 通过 gunicorn 启动项目
supervisorctl start flask-api

#创建supervisor.sock
sudo touch /var/run/supervisor.sock
sudo chmod 777 /var/run/supervisor.sock

supervisord.conf 配置

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = /etc/supervisor/conf.d/*.conf

flask-api.conf 项目配置(flask-api 为 order 项目supervisor服务名称)


[program:flask-api]
directory=/var/www/fm/order 
command=/home/dev/.pyenv/flask-api/bin/gunicorn --worker-class=gevent --max-requests 5000 -w 4 -b 127.0.0.1:8200 fm_poster:app
loglevel=info
autostart=true
autorestart=true

redirect_stderr=true
stdout_logfile=/var/www/feima/log/flask-api.out.log
user=root
stopsignal=TERM

stdout_logfile_maxbytes=0
stdout_logfile_backups=0

 

你可能感兴趣的:(python flask环境搭建)