一、安装python3.6

1. 安装依赖环境

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 sqlite-devel sqlite3-devel

2. 下载Python3

wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz

解压:tar -zxvf Python-3.6.4.tgz

3.进入解压后的目录,编译安装

cd Python-3.6.4
./configure --prefix=/usr/local/python3.6
make && make altinstall 

4.建立python3的软链

ln -s /usr/local/python3/bin/python3.6 /usr/bin/python3

ln -s /usr/local/python3/bin/pip3.6 /usr/bin/pip3

5. 并将/usr/local/python3/bin加入PATH

 vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
#生效
source ~/.bash_profile

 6.检查Python3及pip3是否正常可用:

pip3 -V
pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)

返回如上信息表示可以正常使用

二、安装virtualenv

pip3 install virtualenv
检查:
virtualenv --version
没有可以建立软连接
ln -s /usr/local/python3.6/bin/virtualenv ./virtualenv
创建虚拟环境路径:
cd your_project
cd ..
virtualenv your_project
激活虚拟环境:
source your_project/bin/activate
停用虚拟环境:
deactivate

三、部署项目到虚拟环境

1.将项目包放到放入虚拟环境目录

2.安装项目所需第三方包

pip3 freeze > requirements.txt
pip3 install -r requirements.txt
pip3 install gunicorn

3.启动服务

gunicorn -w 4 -b 0.0.0.0:5000 manage:app
解释:-b服务映射端口 manage(项目启动脚本名称)

4.用supervisor管理服务

pip3 install supervisor
echo_supervisord_conf > supervisor.conf
vi supervisor.conf

在supervisor.conf文件的底部添加(将flask_path改成您的项目路径)
[program:myapp]
command=flask_path/venv/bin/gunicorn -w4 -b0.0.0.0:5000 myapp:app    
directory=flask_path                                              
startsecs=0   
stopwaitsecs=0    
autostart=false                                                                        
autorestart=false                                                             
stdout_logfile=flask_path/log/gunicorn.log                          
stderr_logfile=flask_path/log/gunicorn.err

supervisor基本的命令有:
supervisord -c supervisor.conf                             通过配置文件启动supervisor
ps aux | grep supervisord                                  查看启动进程
supervisorctl                                              进入客户端命令行

supervisorctl status                                       业务状态

stop program_name
start program_name
restart program_name
stop all
# 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
reload
# 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启
update

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Logo

更多推荐