python后端部署

    目录

安装环境conda

 安装nginx

 Flask连通Nginx

安装使用uWSGI服务器

 总结


          网上关于部署python web服务的方法繁多,这里介绍一种个人认为比较简单的方案提供学习、参考。采用uwsgi+nginx的主流方式,以flask项目为例。

安装环境conda

新建文件夹

mkdir usr/local/conda
cd usr/local/conda
ls

无证书下载清华镜像网站上的conda

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda2-4.7.12.1-Linux-x86_64.sh --no-check-certificate

[root@VM-12-2-centos conda]# ls
Miniconda2-4.7.12.1-Linux-x86_64.sh
[root@VM-12-2-centos conda]# bash Miniconda2-4.7.12.1-Linux-x86_64.sh

Welcome to Miniconda2 4.7.12

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>

按照提示安装完成



==> For changes to take effect, close and re-open your current shell. <==

If you'd prefer that conda's base environment not be activated on startup,
   set the auto_activate_base parameter to false:

conda config --set auto_activate_base false

Thank you for installing Miniconda2!
[root@VM-12-2-centos conda]#

conda config --set auto_activate_base false是指进入服务器后不自动进入conda的基础环境,没人会用base环境干活,建议设置,也可不用管。

需关ssh接窗口后重新打开conda才能生效,重启没试过.

conda create -n test python=3.9   创建一个演示环境

conda deactivate   退出base

conda activate test   进入演示环境

设置pip 的地址未国内地址

pip config set global.index-url https://pypi.doubanio.com/simple

装一个flask演示

pip install flask
Successfully installed Jinja2-2.11.3 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 flask-1.1.4 itsdangerous-1.1.0

 创建一个欢迎页用来检查最终部署成功与否。


(python=3.9) [root@VM-12-2-centos local]# mkdir /usr/local/testflask
(python=3.9) [root@VM-12-2-centos local]# cd /usr/local/testflask
(python=3.9) [root@VM-12-2-centos testflask]# ls
(python=3.9) [root@VM-12-2-centos testflask]#

vim testflask.py 写个测试代码。 

#encoding=utf-8
from flask import Flask

app = Flask(__name__)


@app.route('/')
def hello_world():
    return "加班吧码农"


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

-- INSERT --                                                          13,16         All

(python=3.9) [root@VM-12-2-centos testflask]# vim test.py
(python=3.9) [root@VM-12-2-centos testflask]# ls
test.py
(python=3.9) [root@VM-12-2-centos testflask]#

注意如果是在公网上的服务器,部分厂商配置host为localhost或者127.0.0.1会无法在公网中访问到,直接0.0.0.0表示在所有可用连接上监听端口,粗暴管用。

可见测试成功。本人学习服务器绑定了域名,若虚拟机上或没有域名,ifconfig查到ip也是一样的,公网直接复制公网ip。云服务器记得开放5000端口或者直接配置到80端口启动,虚拟机检查防火墙端口并配置。

 安装nginx

创建文件夹

mkdir /usr/local/nginx
cd /usr/local/nginx/

下载nginx相关依赖

yum -y install gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

如果你是装的新版镜像,以上命令基本都是返回Nothing to do。

下载nginx

 wget http://nginx.org/download/nginx-1.9.9.tar.gz

解压并进入解压后的目录

tar -zxvf  nginx-1.9.9.tar.gz
(base) [root@VM-12-2-centos nginx-1.9.9]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

依次执行

./configure

make

make install

(base) [root@VM-12-2-centos nginx-1.9.9]# cd ../
(base) [root@VM-12-2-centos nginx]# ls
conf  html  logs  nginx-1.9.9  nginx-1.9.9.tar.gz  sbin

退回上级目录,可见多了几个目录,这里是把安装目录和安装包放在一个目录下,本人不喜分几个目录来管理一个应用。

./sbin/nginx    直接启动,默认监听80端口。


(base) [root@VM-12-2-centos nginx]# ./sbin/nginx
(base) [root@VM-12-2-centos nginx]# ps -aux|grep nginx
root     17647  0.0  0.0  24896   764 ?        Ss   13:14   0:00 nginx: master process ./sbin/nginx
nobody   17648  0.0  0.0  27392  1516 ?        S    13:14   0:00 nginx: worker process
root     17793  0.0  0.0 112812   980 pts/1    S+   13:15   0:00 grep --color=auto nginx
(base) [root@VM-12-2-centos nginx]# ^C
(base) [root@VM-12-2-centos nginx]# netstat -anp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17647/nginx: master
udp6       0      0 fe80::5054:ff:fe07::123 :::*                                647/ntpd
unix  2      [ ]         DGRAM                    26800    2928/sh              @yunjing_guard.sock

进程中发现应用,网络状态中发现端口被监听。部分朋友可能遇到访问失败之类的,先检查进程和端口,如果没问题还访问不了,去设置云服务器的安全选项开放对应的端口,并检查防火墙是否允许通过端口。

访问地址,nigx启动成功。

 Flask连通Nginx

首先通过nohup在后台启动我们之前调试好的flask测试应用。

(base) [root@VM-12-2-centos testflask]# ls
debug.log  test.py
(base) [root@VM-12-2-centos testflask]# conda activate python=3.9
(python=3.9) [root@VM-12-2-centos testflask]# nohup python test.py > ./flask.log 2>&1 &
[1] 10011
(python=3.9) [root@VM-12-2-centos testflask]# jobs
[1]+  Running                 nohup python test.py > ./flask.log 2>&1 &

去浏览器访问成功后,下一步修改niginx配置如下:

vim /usr/local/nginx/conf/nginx.conf

将server项的location字段改为如下:


        location / {
            proxy_pass  http://0.0.0.0:5000;
        }

 重启nginx

(python=3.9) [root@VM-12-2-centos sbin]# ./nginx -s stop
(python=3.9) [root@VM-12-2-centos sbin]# ps -ef|grep nginx
root     12491  9508  0 16:22 pts/0    00:00:00 vim mkdir /usr/lib/systemd/system/nginx.service
root     14134  9508  0 16:33 pts/0    00:00:00 grep --color=auto nginx
(python=3.9) [root@VM-12-2-centos sbin]# ./nginx

浏览器访问成功。

但是flask包自带的web服务本身只是用来做开发时调试用的,性能较差。下面介绍使用uwsgi作为web服务。 

安装使用uWSGI服务器

安装python3开发扩展包

yum install python3-devel

先激活运行项目的环境,把uwsgi服务器安装到环境目录里,配置软连接。


Successfully installed uwsgi-2.0.21
(test) [root@VM-12-2-centos ~]# find / -name uwsgi
/root/miniconda2/envs/test/bin
(test) [root@VM-12-2-centos ~]# ln -s /root/miniconda2/envs/test/bin /usr/bin/uwsgi

查找运行项目时的conda虚拟环境位置:

查找conda虚拟环境的路径(conda虚拟环境的解释器):先激活环境,然后which python命令获得

(test) [root@VM-12-2-centos testflask]# which python
/root/miniconda2/envs/test/bin/python

切入项目根目录,新建vim uwsgi.ini文件

内容如下

[uwsgi]
master=true
processes=1
threads=10
chdir =/usr/local/testflask
wsgi-file=/usr/local/testflask/test.py
env=/root/miniconda2/envs/test/bin
callable=app
enable-threads=True
socket = /usr/local/testflask/uwsgi.sock
socket = 0.0.0.0:5000
chmod-socket = 777
vacuum = true
daemonize=/usr/local/testflask/uwsgi.log
pidfile=/usr/local/testflask/uwsgi.pid
lazy-apps = true
disable-logging = true
max-requests = 1000
log-maxsize = 5000000

启动

修改ngixn根目录下的conf目录下的ngxin.conf配置文件。

将上面修改的server字段下的location字段内容修改如下:

        location / {
            include uwsgi_params;
            uwsgi_pass unix:///usr/local/testflask/uwsgi.sock;
        }

检查配置并重启nginx

cd ../sbin
./nginx -t
./nginx

 ps -aux|grep test.py

ps -aux|grep uwsgi

检查flask服务有没有在运行,有则kill,检查uwsgi有没有在进程中,没有cd到ini位置启动。

访问域名(或ip),http默认访问80端口,也就是nginx默认端口。

 总结

1、安装conda,隔离linux自带的python2,为项目开辟独立python环境。如果是docker则可直接将镜像自带的python2更新成需要用的python3版本。

2、安装nginx、uwsgi,uwsgi须安装在项目环境中。

3、配置uwsgi服务启动flask项目并通过本地soket通信,配置nginx与uwsgi通信。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值