290112011 发表于 2017-12-16 12:34:56

腾讯云Unubtu 16.04 (gunicorn+supervisor+ngnix+mongodb)部署Flask应用

1、申请腾讯云服务
  我申请了免费使用的云服务器 ,选择安装的Linux版本是ubuntu16.04.1 LTSx86_64。我个人PC安装使用的也是这个版本,比较熟悉些。
  详细参考帮助文档。

2、登录云主机
  使用ssh公钥方式登录云主机,ssh原理参考:SSH原理与运用(一):远程登录。
  在云主机页面点击SSH密钥,创建密钥-->选择已有的公钥,输入本机的ssh公钥-->将此公钥绑定到云主机上。如下图我创建本机的公钥命名为thinkpads5。
  本机的公钥位于$HOME/.ssh/目录下的id_rsa.pub文件内。如果没有现成的,可以直接用ssh-keygen生成一个。
  

  
eliefly@thinkpad-s5:~/.ssh$ ls
  


  
id_rsa >  

  

  完成公钥的绑定后(即将本机主机的公钥传送到云主机上),云主机的$HOME/.ssh/authorized_keys文件内就会保存本地主机的公钥信息。
  成功绑定后,可成功使用ssh公钥登录:
  

  
eliefly@thinkpad-s5:~$ ssh ubuntu@139.199.191.60
  

  
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-53-generic x86_64)
  

  

  

  * Documentation:https://help.ubuntu.com
  

  * Management:   https://landscape.canonical.com
  

  * Support:      https://ubuntu.com/advantage
  

  

  

  
Last login: Mon Jan9 23:03:07 2017 from 58.251.190.158
  

  
ubuntu@VM-93-44-ubuntu:~$ ls /
  

  
bin   dev   initrd.imglost+foundopt   run   srvusr
  

  
bootetc   lib         media       procsbinsysvar
  

  
datahomelib64       mnt         rootsnaptmpvmlinuz
  

  
ubuntu@VM-93-44-ubuntu:~$ logout
  

  
Connection to 139.199.191.60 closed.
  

  
eliefly@thinkpad-s5:~$
  

  

3、部署一个简单的WEB服务
  自己的应用折腾半天没成,就先从简单的应用部署下,理清脉络。
  主要参考:python web 部署:nginx + gunicorn + supervisor + flask 部署笔记
  在路径/srv/helloworld下创建hello_tecent.py文件。
  

  
from flask import Flask
  

  
from flask_script import Manager
  

  
app = Flask(__name__)
  

  
manager = Manager(app)
  

  
@app.route("/")
  

  
def hello():
  

  return "hello tecent cloud!"
  

  
if __name__ == "__main__":
  

  manager.run()
  

  

  同时$ pyvenv venv-helloworld创建虚拟环境venv-helloworld,激活虚拟环境,安装必要的扩展包Flask Flask-Script。
  

  
/srv/helloworld# ls
  

  
hello_tecent.pyvenv-helloworld
  

  

  验证Flask程序能否正常启动,如下项目启动成功,virtualenv和flask项目部署ok。
  

  
(venv-helloworld) root@VM-93-44-ubuntu:/srv/helloworld# python hello_tecent.py runserver
  

  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
  

  

4、使用 gunicorn 部署 flask web
  上面我们使用 flask 自带的服务器,完成了 web 服务的启动。生产环境下,flask 自带的 服务器,无法满足性能要求。我们这里采用 gunicorn 做 wsgi容器,用来部署 python。
  webservice 的方式同样也有很多方式。常见的有FastCGI,WSGI等。我们采用gunicorn为 wsgi容器。
  安装gunicorn,注意此处的gunicorn同样是需要安装在虚拟环境下。在激活虚拟环境的情况下,使用sudo pip install gunicorn安装,会把gunicorn云主机实体环境中。为安装正确,先获取root权限再安装。
  获取云主机root权限:$ sudo /bin/su - root。
  

  

  
ubuntu@VM-93-44-ubuntu:/srv/helloworld$ sudo /bin/su - root
  

  
root@VM-93-44-ubuntu:~# cd /srv/helloworld/
  

  
root@VM-93-44-ubuntu:/srv/helloworld# source ./venv-helloworld/bin/activate
  

  
(venv-helloworld) root@VM-93-44-ubuntu:/srv/helloworld# pip install gunicorn
  

  

  之前使用 Flask-Script扩展来启动Flask服务器,执行# python hello_tecent.py runserver,服务器由manager.run()。
  gunicorn安装好 之后,需要用gunicorn启动 flask。执行$ ./venv-helloworld/bin/gunicorn hello_tecent:app -w 4 -b 0.0.0.0:8000启动服务器。
  此处,hello_tecent.py就等同于一个库文件,被 gunicorn 调用。app为Flask 程序实例名,源码中app = Flask(__name__)。使用 8000 的端口进行访问,原先的5000并没有启用。-w 表示开启多少个 worker,-b 表示 gunicorn 开发的访问地址。
  

  
(venv-helloworld) root@VM-93-44-ubuntu:/srv/helloworld# ./venv-helloworld/bin/gunicorn hello_tecent:app -w 4 -b 0.0.0.0:8000
  

  
Starting gunicorn 19.6.0
  

  
Listening at: http://0.0.0.0:8000 (21387)
  

  
Using worker: sync
  

  
Booting worker with pid: 21390
  

  
Booting worker with pid: 21391
  

  
Booting worker with pid: 21393
  

  
Booting worker with pid: 21394
  

  

  WEB服务启动成功后,就可以在本机浏览器访问云主机的WEB服务。浏览器地址栏输入:<ip address>:8000,就可以看到亲切的 hello tecent cloud!

5.安装 supervisor
  想要结束 gunicorn 只需执行 pkill gunicorn,有时候还的 ps 找到 pid 进程号才能 kill。可是这对于一个开发来说,太过于繁琐,因此出现了另外一个神器---supervisor,一个专门用来管理进程的工具,还可以管理系统的工具进程。
  supervisor python3中不支持,我虚拟环境下使用的是python3.5,无法安装 supervisor。
  

  
(venv-helloworld) root@VM-93-44-ubuntu:/srv/helloworld# pip install supervisor
  

  
Collecting supervisor
  

  Downloading supervisor-3.3.1.tar.gz (415kB)
  

  100% |################################| 419kB 716kB/s
  

  Complete output from command python setup.py egg_info:
  

  Supervisor requires Python 2.4 or later but does not work on any version of Python 3.You are using version 3.5.2 (default, Nov 17 2016, 17:05:23)
  

.Please install using a supported version.
  

  

  ----------------------------------------
  

  
Command &quot;python setup.py egg_info&quot; failed with error code 1 in /tmp/pip-build-0bi4mj2b/supervisor/
  

  

  把supervisor安装在云主机实体环境python2下(ubuntu16.04自带python2和python3)。
  

  
# pip install supervisor
  

  

  将其原始配置文件重定向到程序根目录(需要root权限,sudo执行也失败):
  

  
# echo_supervisord_conf>supervisor.conf
  

  

  我的工程目录是/srv/helloworld,新建log目录,在/srv/helloworld/supervisor.conf配置文件底部添加,hello_tecent。
  

  

  

  
command=/srv/helloworld/venv-helloworld/bin/gunicorn -w4 -b 0.0.0.0:8000 hello_tecent:app    ; supervisor启动命令
  

  
directory=/srv/helloworld                                                 ; 项目的文件夹路径
  

  
startsecs=0                                                                           ; 启动时间
  

  
stopwaitsecs=0                                                                        ; 终止等待时间
  

  
autostart=false                                                                         ; 是否自动启动
  

  
autorestart=false                                                                     ; 是否自动重启
  

  
stdout_logfile=/srv/helloworld/log/gunicorn.log                           ; log 日志
  

  
stderr_logfile=/srv/helloworld/log/gunicorn.err   
  

  

  supervisor的基本使用命令:
  

  
supervisord -c supervisor.conf                           通过配置文件启动supervisor
  

  
supervisorctl -c supervisor.conf status                  察看supervisor的状态
  


  
supervisorctl -c supervisor.conf>  

  
supervisorctl -c supervisor.conf start |   启动指定/所有 supervisor管理的程序进程
  

  
supervisorctl -c supervisor.conf stop |      关闭指定/所有 supervisor管理的程序进程
  

  

  运行成功:
  

  
root@VM-93-44-ubuntu:/srv/helloworld# supervisord -c ./supervisor.conf
  

  
root@VM-93-44-ubuntu:/srv/helloworld# supervisorctl -c ./supervisor.conf start hello_tecent
  

  
hello_tecent: started
  

  
root@VM-93-44-ubuntu:/srv/helloworld#supervisorctl -c ./supervisor.conf status         
  

  
hello_tecent                     RUNNING   pid 2411, uptime 0:01:08
  

  
root@VM-93-44-ubuntu:/srv/helloworld# ps -ef | grep supervisor
  

  
root      2409   10 00:15 ?      00:00:00 /usr/bin/python /usr/bin/supervisord -c ./supervisor.conf
  

  
root      254918430 00:17 pts/0    00:00:00 grep --color=auto supervisor
  

  

  此时本机浏览器打开外网<ip address>:8000便可看到hello tecent cloud!。
  可能出现的错误:
  1)Another program is already listening on a port that one of our HTTP servers is configured to use.
  

  
$ supervisord -c supervisor.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 /usr/bin/supervisord -h
  

  

  解决:执行ps -ef | grep supervisor找到supervisord进程kill掉。
  2)Unlinking stale socket /tmp/supervisor.sock
  解决:sudo unlink /tmp/supervisor.sock
  上面直接把hello_tecent程序的supervisor配置追加到supervisor.conf文件的末尾。其实,hello_tecent程序的supervisor配置文件也可放置在其他目录下,如在目录/srv/helloworld/conf.d/下新建文件 hello_tecnet.ini,相应的supervisor.conf文件的末尾修改为如下,包含关系。这样有多个监控程序时方便管理。
  

  

  

  
files = ./conf.d/hello_tecent.ini
  

  

6、安装使用nginx代理
  nginx 不用多说,一个高性能的web服务器。通常用来在前端做反向代理服务器。所谓正向与反向(reverse),只是英文说法翻译。代理服务,简而言之,一个请求经过代理服务器从局域网发出,然后到达互联网上服务器,这个过程的代理为正向代理。如果一个请求,从互联网过来,先进入代理服务器,再由代理服务器转发给局域网的目标服务器,这个时候,代理服务器为反向代理(相对正向而言)。
  简言之,正向代理:客户端和代理服务器在一起,反向代理器:服务器和代理在一起。
  

  
正向代理:{ 客户端 --->代理服务器 } ---> 服务器
  

  
反向代理:客户端 --->{ 代理服务器 ---> 服务器 }
  

  
{} 表示局域网
  

  

  ngnix的配置放在/etc/nginx/sites-available 139.199.191.60是所申请的云主机公网IP,sudo vim hello-tecent:
  

  

  

  

  然后在/etc/nginx/sites-enabled/目录下创建软链接:
  

  
$ sudo ln -s /etc/nginx/sites-available/hello-tecent /etc/nginx/sites-enabled/
  

  

  让Nginx重新加载配置文件并重启nginx服务:
  

  
ubuntu@VM-93-44-ubuntu:/$ sudo /etc/init.d/nginx restart
  

  
[ ok ] Restarting nginx (via systemctl): nginx.service.
  


  
ubuntu@VM-93-44-ubuntu:/$ sudo /etc/init.d/nginx>  


  
[ ok ]>  

  

  在本机浏览器输入http://139.199.191.60/,注意此时由于ngixn反向代理作用,可以省去端口号进行访问到hello tecent cloud!了。
  

  
$ sudo supervisorctl -c supervisor.conf status
  

  
hello_tecent                     RUNNING   pid 1036, uptime 0:00:44
  

  

7、下面部署自己的Flask应用
  有Flaks+Mongodb构建的博客网站,Git源码位置:NovBlog。

7.1 部署代码至云主机
  我们使用Fabric部署代码至云主机/srv/NovBlog目录,也可使用Git Clone或者FTP传递等方式。
  Fabric参考:Day 15 - 部署Web App

7.2 安装Mongodb数据库。
  Ubuntu 16.04安装Mongodb参考How to Install and Configure MongoDB on Ubuntu 16.04。

7.3 按照之前的方式安装配置gunicorn,supervisor 和 ngnix。
  supervisor配置文件:
  

  

  

  
command=/srv/NovBlog/venv-novblog/bin/gunicorn -w4 -b 0.0.0.0:8000 manage:app    ; supervisor启动命令
  

  
directory=/srv/NovBlog/www                                                 ; 项目的文件夹路径,manage.py所在
  

  
startsecs=0                                                                           ; 启动时间
  

  
stopwaitsecs=0                                                                        ; 终止等待时间
  

  
autostart=false                                                                         ; 是否自动启动
  

  
autorestart=false                                                                     ; 是否自动重启
  

  
stdout_logfile=/srv/NovBlog/log/gunicorn.log                           ; log 日志
  

  
stderr_logfile=/srv/NovBlog/log/gunicorn.err   
  

  

  nginx配置:
  配置屏蔽部分,如果不屏蔽会有问题,会导致static路径出错,源码中引用静态资源是使用如<link href=&quot;{{ url_for('static', filename='css/bootstrap.min.css') }}&quot;>,需要再研究下配置中static如何与href中匹配。
  

  
server {
  

  listen      80;
  

  server_name 139.199.191.60; # 这是HOST机器的外部域名,用地址也行
  

  

  

  root       /srv/NovBlog/www/;
  

  access_log /srv/NovBlog/log/access_log;
  

  error_log/srv/NovBlog/log/error_log;
  

  

  

  # location /favicon.ico {
  

  #    root /srv/NovBlog/www/app/static/img;
  

  # }
  

  

  

  # location ~ ^\/static\/.*$ {
  

  #    root /srv/NovBlog/www/app/static;
  

  # }
  

  

  

  location / {
  

  proxy_pass       http://127.0.0.1:8000;
  

  proxy_set_header Host $host;
  

  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  

  }
  

  
}
  

  

7.4 绑定域名
  域名没买,参考操作:
  域名解析添加A型记录,值为云服务器外网ip即可。
  
nginx配置相应的一行代码亦需改一下:
  

  
$ server_name <域名>;
  

  

  此时,在网址栏输入域名即可进入网站。
  参考:
  1、在腾讯云上部署flask应用
  2、python web 部署:nginx + gunicorn + supervisor + flask 部署笔记
页: [1]
查看完整版本: 腾讯云Unubtu 16.04 (gunicorn+supervisor+ngnix+mongodb)部署Flask应用