sharpds77 发表于 2018-11-9 08:32:58

CentOS7部署Flask+Gunicorn+Nginx+Supervisor

1. Git客户端
  Win10安装git for windows

1.1 设置Git全局参数
  打开Git Bash
  

$ git config --global user.name "Alice-HomePC"  
$ git config --global user.email "alice@gmail.com"
  

1.2 生成SSH Key
  打开Git Bash,可使用-C选项指定公钥的说明信息
  

$ ssh-keygen -t rsa -C "Alice-HomePC"  

  一直回车确认即可,秘钥对默认保存在C:\Users\你的Win10用户名\.ssh目录下,其中id_rsa是私钥(Private Key),要小心保管;id_rsa.pub是公钥(Public Key),待会要上传到VPS上,实现基于SSH无密码登录VPS。同理,如果你在Github或Coding上有代码仓库,也是先要将公钥上传过去,才能无密码使用Git命令操作远程仓库。

2. 配置VPS

2.1 修改主机名
  

# hostnamectl set-hostname CentOS  
或者:
  
# vi /etc/hostname
  
# hostnamectl
  

  重新登录.

2.2 修改SSH端口
  

# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak  
# vi /etc/ssh/sshd_config
  
将默认22端口改为你指定的, 例如
  
Port 12345
  

  
# systemctl restart sshd
  

2.3 禁用SSH密码认证,改为秘钥认证
  首先需要将步骤1中生成的公钥上传到服务器,可以使用xmanager套件中的xftp上传,假设上传到/root目录
  

1. 添加公钥  
# cd /root
  
# mkdir ~/.ssh && chmod 700 ~/.ssh
  
# touch ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys

  
# cat>  

  
2. 修改SSH配置文件
  
# vi /etc/ssh/sshd_config
  

  
修改几处地方,最终内容如下:
  
# 禁用root登录
  
PermitRootLogin no
  
# 启用密钥验证
  
RSAAuthentication yes
  
PubkeyAuthentication yes
  
# 指定公钥数据库文件
  
AuthorizedKeysFile .ssh/authorized_keys
  
# 禁用密码验证
  
PasswordAuthentication no
  

  
3. SSH重新加载配置文件

  
# systemctl>  

  此时,Win10可以通过xshell,无密码登录VPS了,且只能使用私钥认证通过。

3. 安装Python3
  CentOS-7.3默认安装的是Python-2.7, 我的Flask程序是基于Python3写的,所以要再安装Python3
  

1. 准备编译环境  
# yum -y install gcc make readline-devel sqlite-devel openssl openssl-devel zlib*
  

  
2. 编译安装
  
# wget -P /root http://python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz
  
# tar xf Python-3.6.4.tar.xz
  
# cd Python-3.6.4/
  
# ./configure --prefix=/usr/local/python-3.6
  
# make && make install
  
# ln -s /usr/local/python-3.6/bin/python3.6 /usr/bin/python3
  
# ln -s /usr/local/python-3.6/bin/pip3.6 /usr/bin/pip3
  

  更改pip安装源为国内的源,比如aliyun

  

# mkdir ~/.pip  
# vi ~/.pip/pip.conf
  

  
添加内容如下:
  

  
index-url = http://mirrors.aliyun.com/pypi/simple/
  

  

  
trusted-host=mirrors.aliyun.com
  

  (可选)安装IPython



[*]Home Page
[*]GitHub Project
[*]Installing Jupyter Notebook
  

1. pip方式安装(推荐), 该方式会有语法高亮等特性  
# pip3 --version
  
# pip3 install --upgrade pip
  
# pip3 install ipython
  
# ln -s /usr/local/python-3.6/bin/ipython3 /usr/bin/ipython3
  

  
2. 编译安装
  
# tar xf ipython-0.13.1.tar.gz
  
# cd ipython-0.13.1/
  
# python3 setup.py install
  
# ln -s /usr/local/python-3.6/bin/ipython3 /usr/bin/ipython3
  

4. 安装MongoDB
  官方文档
  

1. 配置repo源  
# vi /etc/yum.repos.d/mongodb-org-3.6.repo
  

  
内容如下:
  

  
name=MongoDB Repository
  
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.6/x86_64/
  
gpgcheck=1
  
enabled=1
  
gpgkey=https://www.mongodb.org/static/pgp/server-3.6.asc
  

  
2. 安装并启动服务
  
# yum install -y mongodb-org
  
# systemctl start mongod.service
  
# systemctl enable mongod.service
  

5. Git服务端
  

1. 安装  
# yum install -y git
  

  
2. 创建裸仓库
  
# mkdir /home/git && cd /home/git
  
# git init --bare flask_project.git
  

  我在Win10上已经开发好了Flask程序,待会上传到此git仓库中,应用程序代码准备部署到/home/www/flask_project,并通过git的hooks当客户端每次提交代码后,自动同步仓库中的代码到应用部署的位置 Simple automated GIT Deployment using GIT Hooks
http://blog.51cto.com/admin/medias/uploaded/3-341e8656.jpg
  

1. 创建代码部署目录  
# mkdir -pv /home/www/flask_project
  

  
2. 创建hooks
  
# vi /home/git/flask_project.git/hooks/post-receive
  

  
内容如下:
  
#!/bin/bash
  
TRAGET="/home/www/flask_project"
  
GIT_DIR="/home/git/flask_project.git"
  
BRANCH="master"
  

  
while read oldrev newrev ref
  
do
  # only checking out the master (or whatever branch you would like to deploy)
  if [[ $ref = refs/heads/$BRANCH ]];
  then
  echo "Ref $ref received. Deploying ${BRANCH} branch to production…"
  git --work-tree=$TRAGET --git-dir=$GIT_DIR checkout -f
  else
  echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
  fi
  
done
  

  
3. 赋权
  
# chmod +x /home/git/flask_project.git/hooks/post-receive
  

6. 上传代码
  打开Git Bash,准备把服务器上的flask_project.git仓库(目前为空)克隆下来。Git默认使用SSH协议且端口22,由于我们刚修改了服务器的SSH端口,所以克隆时要指定修改后的端口号

6.1 克隆远程仓库
  方法1:
  

$ git clone ssh://root@VPS的IP或域名:VPS的SSH端口号//home/git/flask_project.git  

  方法2: 在Win10保存SSH秘钥对的目录下创建配置文件 C:\Users\你的Win10用户名\.ssh\config
  

host VPS的IP或域名  
port VPS的SSH端口
  

  然后执行克隆命令:
  

$ git clone root@VPS的IP或域名:/home/git/flask_project.git  

6.2 提交代码
  克隆后会在当前目录下生成 flask_project 目录,把开发好的flask代码拷贝到这里面,并指定哪些文件不提交到git仓库,在git bash中运行:
  

$ cd flask_project  
$ vi .gitignore
  
比如我的规则:
  
.idea/
  
__pycache__/
  
uploads/
  
venv3/
  

  提交代码:
  

$ git add .  
$ git commit -m "initial"
  
$ git push
  

  你会发现/home/www/flask_project目录下会自动拷贝git仓库中master分支的最新代码过来。

7. 调试程序

7.1 准备virtualenv环境
  

# pip3 install virtualenv  
# ln -s /usr/local/python-3.6/bin/virtualenv /usr/bin/virtualenv
  
# cd /home/www/flask_project
  
# virtualenv --no-site-packages --python=/usr/bin/python3 venv3
  

7.2 安装相关python包
  

# source venv3/bin/active  
(venv3)# pip install flask
  
依次安装完所有的依赖包后,
  
(venv3)# pip freeze > requirements.txt
  

7.3 测试用Flask自带的服务器能否运行
  

(venv3)# python manage.py runserver -h 0.0.0.0 -p 80  

  如果你能通过VPS的IP正常访问Flask应用,那么就可以进行下一步,使用Gunicorn替代Flask自带的开发服务器

8. Gunicorn


[*]官网
[*]文档
8.1 安装
  

(venv3)# pip install gunicorn  

8.2 创建包含应用入口app的模块文件
  一般我们开发时,都是使用manage.py,里面有flask-script方便调试,生产环境要再创建一个模块,比如:
  

# vi wsgi.py  

  
内容如下:
  
import os
  

  
from app import create_app
  

  
###
  
# 调用工厂函数,初始化Flask程序实例,默认使用生产环境配置
  
###
  
app = create_app(os.getenv('FLASK_CONFIG') or 'production')
  

  
if __name__ == "__main__":
  app.run(host='0.0.0.0')
  

  那么使用gunicorn命令行来启动Flask非常简单:
  

(venv3)# gunicorn -w 3 wsgi:app -b 0.0.0.0:80  
说明:
  
-w 3 是启用3个进程,建议是CPU核数*2 + 1
  
wsgi:app 其中wsgi代表当前目录下的wsgi.py模块,后面的app代表wsgi.py模块里面的Flask应用app
  

  如果你能通过VPS的IP正常访问Flask应用,那么通过指定gunicorn配置文件来启动Flask,比如:
  

# mkdir deploy  
# vi deploy/gunicorn.conf.py
  

  
内容如下:
  
import multiprocessing
  

  
# bind = '127.0.0.1:8001'
  
bind = 'unix:/run/gunicorn.sock'
  
workers = multiprocessing.cpu_count() * 2 + 1
  
# daemon = True
  
pidfile = '/run/gunicorn.pid'
  
loglevel = 'info'
  
errorlog = '/tmp/gunicorn-error.log'
  
accesslog = '/tmp/gunicorn-access.log'
  
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
  

  更详细的配置可以参考Gunicorn官方示例
  那么,此时启动Flask变成:
  

(venv3)# gunicorn wsgi:app -c deploy/gunicorn.conf.py  

  想查看完整的内容,请访问我的个人博客: http://www.madmalls.com/blog/post/deploy-flask-gunicorn-nginx-supervisor-on-centos7/?q=gunicorn


页: [1]
查看完整版本: CentOS7部署Flask+Gunicorn+Nginx+Supervisor