python web 部署

python web 部署

web开发中,各类语言争奇斗艳,web的部署方面,却没有太多的方式。简单而已,大概都是 nginx 作前端代理,中间 webservice 调用程序脚本。大概方式:nginx + webservice + script前端

nginx 不用多说,一个高性能的web服务器。一般用来在前端作反向代理服务器。所谓正向与反向(reverse),只是英文说法翻译。代理服务,简而言之,一个请求通过代理服务器从局域网发出,而后到达互联网上服务器,这个过程的代理为正向代理。若是一个请求,从互联网过来,先进入代理服务器,再由代理服务器转发给局域网的目标服务器,这个时候,代理服务器为反向代理(相对正向而言)。python

正向代理:{ 客户端 —》 代理服务器 } —》 服务器linux

反向代理:客户端 —》 { 代理服务器 —》 服务器 }nginx

{} 表示局域网git

nginx既能够作正向,也能够作反向。
webservice 的方式一样也有不少方式。常见的有FastCGIWSGI等。咱们采用gunicorn为 wsgi容器。python为服务器script,采用flask框架。同时采用supervisor管理服务器进程。也就是最终的部署方式为:
nginx + gunicorn + flask ++ supervisorweb

建立一个项目

Vim编程

1flask

mkdir myprojectvim

建立 python 虚拟环境

virtualenv 能够说是 python 的一个大杀器。用来在一个系统中建立不一样的 python 隔离环境。相互之间还不会影响,使用简单到使人发指。(个人工做路径是 /home/rsj217/rsj217浏览器

1

2

3

mkdir myproject

cd myproject

virtualenv venv

建立了 venv 环境以后,激活就能够了

1

source venv/bin/activate

安装 python web 框架 —flask

flask 是一个 python web micro framework。简洁高效,使用也很简单。flask 依赖两个库 werkzeug 和 jinjia2。采用 pip 方式安装便可。

1

pip install flask

测试咱们的 flask 安装是否成功,并使用 flask 写一个简单的 web 服务。
vim myapp.py

1

2

3

4

5

6

7

8

from flask import Flask

app = Flask(__name__)

@app.route('/')

def index():

    return 'hello world'

if __name__ == '__main__':

    app.debug = True

    app.run()

启动 flask

1

python myapp.py

此时,用浏览器访问 http://127.0.0.1:5000 就能看到网页显示 hello world

使用 gunicorn 部署 python web

如今咱们使用 flask 自带的服务器,完成了 web 服务的启动。生产环境下,flask 自带的 服务器,没法知足性能要求。咱们这里采用 gunicorn 作 wsgi容器,用来部署 python。

安装 gunicorn

1

pip install gunicorn

pip 是一个重要的工具,python 用来管理包。还有一个最佳生产就是每次使用 pip 安装的库,都写入一个 requirement 文件里面,既能知道本身安装了什么库,也方便别人部署时,安装相应的库。

1

pip freeze > requirements.txt

之后每次 pip 安装了新的库的时候,都需freeze 一次。

当咱们安装好 gunicorn 以后,须要用 gunicorn 启动 flask,注意 flask 里面的name里面的代码启动了 app.run(),这个含义是用 flask 自带的服务器启动 app。这里咱们使用了 gunicorn,myapp.py 就等同于一个库文件,被 gunicorn 调用。

1

gunicron -w4 -b0.0.0.0:8000 myapp:app

此时,咱们须要用 8000 的端口进行访问,原先的5000并无启用。其中 gunicorn 的部署中,,-w 表示开启多少个 worker,-b 表示 gunicorn 开发的访问地址。

想要结束 gunicorn 只需执行 pkill gunicorn,有时候还的 ps 找到 pid 进程号才能 kill。但是这对于一个开发来讲,太过于繁琐,所以出现了另一个神器—supervisor,一个专门用来管理进程的工具,还能够管理系统的工具进程。

安装 supervisor

1

2

3

pip install supervisor

echo_supervisord_conf > supervisor.conf   # 生成 supervisor 默认配置文件

vim supervisor.conf                       # 修改 supervisor 配置文件,添加 gunicorn 进程管理

在myapp supervisor.conf 配置文件底部添加 (注意个人工做路径是/home/rsj217/rsj217/)

1

2

3

4

5

6

7

8

9

[program:myapp]

command=/home/rsj217/rsj217/myproject/venv/bin/gunicorn -w4 -b0.0.0.0:8000 myapp:app    ; supervisor启动命令

directory=/home/rsj217/rsj217/myproject                                                 ; 项目的文件夹路径

startsecs=0                                                                             ; 启动时间

stopwaitsecs=0                                                                          ; 终止等待时间

autostart=false                                                                         ; 是否自动启动

autorestart=false                                                                       ; 是否自动重启

stdout_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.log                           ; log 日志

stderr_logfile=/home/rsj217/rsj217/myproject/log/gunicorn.err                           ; 错误日志

supervisor的基本使用命令

1

2

3

4

5

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

supervisorctl -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管理的程序进程

supervisor 还有一个web的管理界面,能够激活。更改下配置

1

2

3

4

5

6

7

8

9

10

11

12

[inet_http_server]         ; inet (TCP) server disabled by default

port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)

username=user              ; (default is no username (open server))

password=123               ; (default is no password (open server))

 

[supervisorctl]

serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket

serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket

username=user              ; should be same as http_username if set

password=123                ; should be same as http_password if set

;prompt=mysupervisor         ; cmd line prompt (default "supervisor")

;history_file=~/.sc_history  ; use readline history if available

如今可使用 supervsior 启动 gunicorn啦。运行命令 supervisord -c supervisor.conf

访问 http://127.0.0.1:9001 能够获得 supervisor的web管理界面,访问 http://127.0.0.1:2170 能够看见gunciron 启动的返回的 hello world

安装配置 nginx

采用 apt-get方式安装最简单。运行 sudo apt-get install nginx。安装好的nginx的二进制文件放在 /usr/sbin/文件夹下面。而nginx的配置文件放在 /etc/nginx下面。

使用 supervisor 来管理 nginx。这里须要注意一个问题,linux的权限问题。nginx是sudo的方式安装,启动的适合也是 root用户,那么咱们如今也须要用 root用户启动supervisor。增长下面的配置文件

1

2

3

4

5

6

7

8

[program:nginx]

command=/usr/sbin/nginx

startsecs=0

stopwaitsecs=0

autostart=false

autorestart=false

stdout_logfile=/home/rsj217/rsj217/myproject/log/nginx.log

stderr_logfile=/home/rsj217/rsj217/myproject/log/nginx.err

到此为止,进步的 web 部属已经完成。固然,最终咱们须要把项目代码部属到服务器上.批量的自动化部属须要另一个神器 fabric.具体使用,就再也不这篇笔记阐述。项目源码中包含了fabric文件。下载fabric,更改里面的用户名和秘密,就能够部属在本身或者远程的服务器上了。

项目源码: https://coding.net/u/rsj217/p/myproject/git

问啊-定制化IT教育平台,牛人一对一服务,有问必答,开发编程社交头条 官方网站:www.wenaaa.com


QQ群290551701 汇集不少互联网精英,技术总监,架构师,项目经理!开源技术研究,欢迎业内人士,大牛及新手有志于从事IT行业人员进入!

相关文章
相关标签/搜索