使用flask开发api——部署fla

1,013次阅读
没有评论
使用flask开发api——部署fla

使用flask开发api——部署flask,使用gunicorn+gevent模式的http server

用flask开发了服务端的api,记录部署上服务器的过程,以供后续使用。

安装python3.5

本身服务器已经安装了python3,并且版本为3.5,这里不重新安装,如果服务器中没有安装python3,则进行安装

$ sudo apt-get install python3<span class="hljs-number">.5</span>

安装pip3

我们需要用pip来使用第3方模块,先安装pip

<span class="hljs-comment"># 下载安装脚本</span>
$ curl -O https://bootstrap.pypa.io/get-pip.py

<span class="hljs-comment"># 安装</span>
sudo python3 get-pip.py 

安装成功后,查看安装版本,确定与你目前的python3版本一致

$ pip3 -V

//显示的版本信息
pip <span class="hljs-number">9.0</span><span class="hljs-number">.1</span> <span class="hljs-keyword">from</span> /usr/local/lib/python3<span class="hljs-number">.5</span>/dist-packages (python <span class="hljs-number">3.5</span>)

安装virtualenv

每个项目可能需要用到的环境不一样,为了避免影响整个服务器的环境,我们给每个项目一个独立的运行环境,需要用到virtualenv

$ sudo pip3 install virtualenv 

//如果安装成功,会显示
Successfully installed virtualenv<span class="hljs-number">-15.1</span><span class="hljs-number">.0</span>

创建虚拟环境

$ cd /var/www/your_project_name

<span class="hljs-comment"># 创建虚拟环境,其中py35env为你自定义的环境名称</span>
$ virtualenv -p python3 py35env

<span class="hljs-comment"># 创建成功会有如下说明</span>
Already using interpreter /usr/bin/python3
Using base prefix <span class="hljs-string">'/usr'</span>
New python executable <span class="hljs-keyword">in</span> /var/www/your_project_name/py35env/bin/python3
Also creating executable <span class="hljs-keyword">in</span> /var/www/your_project_name/py35env/bin/python
Installing setuptools, pip, wheel...done.

<span class="hljs-comment"># 激活虚拟环境</span>
$ source py35env/bin/activate

<span class="hljs-comment"># 这时命令行路径前面会出现你的虚拟环境名,例如:</span>
(py35env) ubuntu@ip<span class="hljs-number">-10</span><span class="hljs-number">-0</span><span class="hljs-number">-0</span><span class="hljs-number">-119</span>:/var/www/your_project_name$

<span class="hljs-comment"># 查看你目前的插件,有些可能之前系统python环境已经安装了,你可以不用重复安装</span>
$ sudo pip3 list

安装项目中需要的python 模块

1.安装Flask

$ pip3 install Flask 

<span class="hljs-comment"># 安装成功会提示如下:</span>
Successfully installed Flask<span class="hljs-number">-0.12</span><span class="hljs-number">.2</span> Werkzeug<span class="hljs-number">-0.12</span><span class="hljs-number">.2</span> click<span class="hljs-number">-6.7</span> itsdangerous<span class="hljs-number">-0.24</span>
<span class="hljs-comment"># 也可以用sudo pip3 list来查看</span>

2. 安装Flask-RESTful

$ pip3 install Flask-RESTful

<span class="hljs-comment"># 安装成功会提示如下:</span>
Successfully installed Flask-RESTful<span class="hljs-number">-0.3</span><span class="hljs-number">.6</span> aniso8601<span class="hljs-number">-1.3</span><span class="hljs-number">.0</span> python-dateutil<span class="hljs-number">-2.6</span><span class="hljs-number">.1</span> pytz<span class="hljs-number">-2017.2</span>
<span class="hljs-comment"># 也可以用sudo pip3 list来查看</span>

3. 安装pyMySQL

$ pip3 install pyMySQL

<span class="hljs-comment"># 安装成功会提示如下:</span>
Successfully installed pyMySQL<span class="hljs-number">-0.7</span><span class="hljs-number">.11</span>
<span class="hljs-comment"># 也可以用sudo pip3 list来查看</span>

4. 安装gunicorn

$ pip3 install gunicorn

<span class="hljs-comment"># 安装成功会提示如下:</span>
Successfully installed gunicorn<span class="hljs-number">-19.7</span><span class="hljs-number">.1</span>
<span class="hljs-comment"># 也可以用sudo pip3 list来查看</span>

<span class="hljs-comment"># 创建gunicorn存放日志文件夹</span>
$ sudo mkdir /var/log/gunicorn
<span class="hljs-comment"># 更改权限</span>
$ sudo chmod -R <span class="hljs-number">777</span> /var/log/gunicorn

5.安装gevent

$ pip3 install gevent

<span class="hljs-comment"># 安装成功会提示如下:</span>
Successfully installed gevent<span class="hljs-number">-1.2</span><span class="hljs-number">.2</span> greenlet<span class="hljs-number">-0.4</span><span class="hljs-number">.12</span>
<span class="hljs-comment"># 也可以用sudo pip3 list来查看</span>

安装项目

将项目克隆下来

$ git clone git@xxx.xxx.xxx/your_project_name.git

进入到项目下,测试gunicorn是否正确运行

$ cd your_project_name

<span class="hljs-comment"># 更新项目日志文件夹的权限</span>
$ sudo chmod -R <span class="hljs-number">777</span> logs

$ gunicorn -b <span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">5000</span> manager:app
<span class="hljs-comment"># 如果提示如下:</span>
[<span class="hljs-number">2017</span><span class="hljs-number">-10</span><span class="hljs-number">-20</span> <span class="hljs-number">09</span>:<span class="hljs-number">44</span>:<span class="hljs-number">48</span> +<span class="hljs-number">0000</span>] [<span class="hljs-number">21856</span>] [INFO] Starting gunicorn <span class="hljs-number">19.7</span><span class="hljs-number">.1</span>
[<span class="hljs-number">2017</span><span class="hljs-number">-10</span><span class="hljs-number">-20</span> <span class="hljs-number">09</span>:<span class="hljs-number">44</span>:<span class="hljs-number">48</span> +<span class="hljs-number">0000</span>] [<span class="hljs-number">21856</span>] [INFO] Listening at: http://<span class="hljs-number">0.0</span><span class="hljs-number">.0</span><span class="hljs-number">.0</span>:<span class="hljs-number">5000</span> (<span class="hljs-number">21856</span>)
[<span class="hljs-number">2017</span><span class="hljs-number">-10</span><span class="hljs-number">-20</span> <span class="hljs-number">09</span>:<span class="hljs-number">44</span>:<span class="hljs-number">48</span> +<span class="hljs-number">0000</span>] [<span class="hljs-number">21856</span>] [INFO] Using worker: sync
[<span class="hljs-number">2017</span><span class="hljs-number">-10</span><span class="hljs-number">-20</span> <span class="hljs-number">09</span>:<span class="hljs-number">44</span>:<span class="hljs-number">49</span> +<span class="hljs-number">0000</span>] [<span class="hljs-number">21859</span>] [INFO] Booting worker <span class="hljs-keyword">with</span> pid: <span class="hljs-number">21859</span>

<span class="hljs-comment"># 开启后,可以在客户端中测试访问</span>
<span class="hljs-comment"># crtl+c关闭</span>

安装supervisor做守护进程

由于gunicorn在控制面板关闭后,进程也会退出,需要使用supervisor做后台守护。

退出虚拟环境

$ deactivate

如果服务器中没有安装supervisor,先进行安装

$ sudo apt-get install supervisor

增加supervisor 配置文件

$  cd /etc/supervisor/conf.d
<span class="hljs-comment"># 创建配置文件</span>
$ sudo touch your_project_name.conf
<span class="hljs-comment"># 编辑</span>
$ sudo vi your_project_name.conf

<span class="hljs-comment"># 内容如下</span>
[program:your_project_name]
command=/var/www/your_project_name/py35env/bin/gunicorn manager:app -c /var/www/your_project_name/your_project_name/gun.py
directory=/var/www/your_project_name/your_project_name
autostart=true
autorestart=true
stdout_logfile=/var/log/gunicorn/gunicorn_supervisor.log
stderr_logfile=/var/log/gunicorn/gunicorn_supervisor_err.log

更新supervisor

$ sudo supervisorctl update
<span class="hljs-comment"># 会提示相关的进程已经被加入</span>
your_project_name: added process group

<span class="hljs-comment"># 要关闭相关的进程可以用</span>
$ sudo supervisorctl stop your_project_name
<span class="hljs-comment"># 开启可以用</span>
$ sudo supervisorctl start your_project_name
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2022-12-27发表,共计2974字。
新手QQ群:570568346,欢迎进群讨论 Python51学习