腾讯云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$ lsid_rsa id_rsa.pub known_hosts
完成公钥的绑定后(即将本机主机的公钥传送到云主机上),云主机的$HOME/.ssh/authorized_keys
文件内就会保存本地主机的公钥信息。
成功绑定后,可成功使用ssh公钥登录:
eliefly@thinkpad-s5:~$ ssh ubuntu@139.199.191.60Welcome 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/advantageLast login: Mon Jan 9 23:03:07 2017 from 58.251.190.158ubuntu@VM-93-44-ubuntu:~$ ls /bin dev initrd.img lost+found opt run srv usrboot etc lib media proc sbin sys vardata home lib64 mnt root snap tmp vmlinuzubuntu@VM-93-44-ubuntu:~$ logoutConnection 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 Flaskfrom flask_script import Managerapp = 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# lshello_tecent.py venv-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 - rootroot@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[2017-01-15 22:47:55 +0800] [21387] [INFO] Starting gunicorn 19.6.0[2017-01-15 22:47:55 +0800] [21387] [INFO] Listening at: http://0.0.0.0:8000 (21387)[2017-01-15 22:47:55 +0800] [21387] [INFO] Using worker: sync[2017-01-15 22:47:55 +0800] [21390] [INFO] Booting worker with pid: 21390[2017-01-15 22:47:55 +0800] [21391] [INFO] Booting worker with pid: 21391[2017-01-15 22:47:55 +0800] [21393] [INFO] Booting worker with pid: 21393[2017-01-15 22:47:55 +0800] [21394] [INFO] 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 supervisorCollecting supervisorDownloading 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)[GCC 5.4.0 20160609]. Please install using a supported version.----------------------------------------Command "python setup.py egg_info" 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。
[program: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 通过配置文件启动supervisorsupervisorctl -c supervisor.conf status 察看supervisor的状态supervisorctl -c supervisor.conf reload 重新载入 配置文件supervisorctl -c supervisor.conf start [all]|[appname] 启动指定/所有 supervisor管理的程序进程supervisorctl -c supervisor.conf stop [all]|[appname] 关闭指定/所有 supervisor管理的程序进程
运行成功:
root@VM-93-44-ubuntu:/srv/helloworld# supervisord -c ./supervisor.confroot@VM-93-44-ubuntu:/srv/helloworld# supervisorctl -c ./supervisor.conf start hello_tecenthello_tecent: startedroot@VM-93-44-ubuntu:/srv/helloworld# supervisorctl -c ./supervisor.conf status hello_tecent RUNNING pid 2411, uptime 0:01:08root@VM-93-44-ubuntu:/srv/helloworld# ps -ef | grep supervisorroot 2409 1 0 00:15 ? 00:00:00 /usr/bin/python /usr/bin/supervisord -c ./supervisor.confroot 2549 1843 0 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.confError: 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文件的末尾修改为如下,包含关系。这样有多个监控程序时方便管理。
[include]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 reload[ ok ] Reloading nginx configuration (via systemctl): nginx.service.
在本机浏览器输入http://139.199.191.60/
,注意此时由于ngixn反向代理作用,可以省去端口号进行访问到hello tecent cloud!了。
$ sudo supervisorctl -c supervisor.conf statushello_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配置文件:
[program:novblog]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="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
,需要再研究下配置中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 部署笔记
转载于:https://www.cnblogs.com/elie/p/6341680.html
腾讯云Unubtu 16.04 (gunicorn+supervisor+ngnix+mongodb)部署Flask应用相关推荐
- 本地Windows远程桌面连接阿里云Ubuntu 16.04服务器:
本地Windows远程桌面连接阿里云Ubuntu 16.04服务器: 1.目的:希望通过本地的Windows远程桌面连接到阿里云的Ubuntu服务器,通过远程桌面图形界面的方式操作服务器. 2.条件: ...
- 腾讯云Ubuntu 18.04.1开启root用户远程登录并解决SSH服务器拒绝了密码的问题
文章目录 1. 设置root密码 2. 设置允许密码通过SSH登录 2. 设置允许root用户使用密码通过SSH登录 默认情况下:root用户默认是没有密码的,且不许登录. 实际测试镜像:Ubuntu ...
- Ubuntu Server 16.04 LTS上使用Docker部署Tomcat修改为80端口
场景 Ubuntu Server 16.04 LTS上使用Docker部署Tomcat: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/detai ...
- 基于Ubuntu Server 16.04 LTS版本安装和部署Django之(二):Apache安装和配置
基于Ubuntu Server 16.04 LTS版本安装和部署Django之(一):安装Python3-pip和Django 基于Ubuntu Server 16.04 LTS版本安装和部署Djan ...
- 基于Ubuntu Server 16.04 LTS版本安装和部署Django之(一):安装Python3-pip和Django
近期开始学习基于Linux平台的Django开发,想配置一台可以发布的服务器,经过近一个月的努力,终于掌握了基于Apache和mod-wsgi插件的部署模式,自己也写了一个教程,一是让自己有个记录,二 ...
- 一起飞系列之:腾讯云配置Ubuntu16.04, Nginx, PHP 7, MySql, PhpMyAdmin, 域名
前言 公司业务扩展需要大力提高微信用户的关注度,但是主服务器都在境外,国内客户访问速度成为了最大的障碍.先后试验过CloudFlare跟腾讯DNS加速,但是都无法逾越我们的万里长城万里长.所以决定将服 ...
- 【腾讯云】Ubuntu16.04下搭建Java开发环境一站式服务(WinSCP、PuTTY、JDK、MySQL、Tomcat)
购买腾讯云服务器 感觉这并没有什么好说的,直接链接进去,点击支付,简单粗暴. 链接地址:云+校园扶持计划(学生10元每月,1核2G的云服务器,还是很便宜的). 一般情况下:选择Ubuntu16.04的 ...
- 腾讯云Ubuntu 22.04安装ERPnext V14 安装教程
一.安装基础环境 腾讯云服务器Ubuntu系统root账户设置密码 step 1 : sudo passwd root 使用sudo命令为root用户添加新密码 step 2 : sudo vi /e ...
- Ubuntu Server 16.04 LTS上使用Docker部署Tomcat
场景 Ubuntu Server 16.04 LTS上安装Docker(使用脚本的方式): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...
最新文章
- Windows Server 2012
- 2012/08/27 夜
- 皮一皮:有的人自以为有小聪明...
- 笔记本8G+256G固态免费送,吃鸡不吃力,包邮!
- Fiddler 抓包https配置 提示creation of the root certificate was not successful 证书安装不成功
- LINQ系列:LINQ to XML操作
- python爬虫记录时间_Python新手写爬虫全过程记录分析
- 2020年Java程序员应该学习的10大技术
- Spark常用API(五)
- NetBeans 成为 Apache 软件基金会顶级项目
- Unity 快速实现镜子效果
- Multisim小信号调谐放大器电路仿真
- 三款好用的记账软件推荐
- Amaze UI后台管理模板 v2.7.2
- python笛卡尔坐标系_THREE笛卡尔右手坐标系详解
- 国际化进程加速,上海树图区块链研究院联合创始人伍鸣博士受邀访问香港科技园...
- server数据库标记为可疑 sql_SQL Server 2008数据库被标记为可疑的解决方法
- 禁止灵格斯2.7弹出“新奇英语 互动锐词”广告窗口的解决方法
- 激励名言 摘抄其他编程人员的话
- Spark-Excel算子