阿里云服务器部署flask项目gunicorn + nginx + 支持https

2023-05-2613:07:32服务器及运维Comments1,219 views字数 5945阅读模式

做了一个微信小程序,使用 flask 实现了对应的后台,上线需要部署到服务器上,之前只是了解并没有全链路试过,靠着网上的资料最终完成部署上线,但中间遇到了较多的一些问题,网上的资料也比较零碎,所以整理了这篇文章,一方面是作为记录方便后续查阅,另一方面也希望能够让跟我一样的新手少走弯路。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

准备

1、首先要有一个服务器,我这里使用的是阿里云的,系统使用的 CenterOS 7.9
2、第二需要有一个自己的域名(微信小程序必须要求是域名访问而不能是IP访问,并且域名还要是支持 https的);
3、其他环境配置:python:3.10.6;pip:22.2.1;nginx: 1.20.2 openssl: 1.1.1s文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

服务器端口开发

由于防火墙的原因,我们需要现在服务器开放一些需要用到的端口,要不然后面在公网场景会出现访问不到的情况,增加的方式也很简单,在阿里云的Esc 服务器里面,点击安全组,创建安全组,然后添加下面几个端口配置即可。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

阿里云服务器部署flask项目gunicorn + nginx + 支持https
duankou.jpg

升级python,安装python3

Center OS7 本来就安装了 Python2,而且不能被删除,我们用到的是Python3,所以需要进行升级。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

  • 安装依赖包
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
  • python安装包下载(https://www.python.org/downloads/)
    我这里选择安装的是 3.10.6版本,跟我本地开发的环境保持一致,先从上面的链接下载对应的 安装包之后,上传到 /user/local/python3
mkdir /usr/local/python3
cd /usr/local/python3
tar -xvf Python-3.10.6.tar.gz
  • 编译安装
cd Python-3.10.6/
./configure --prefix=/usr/local/python3
make && make install
  • 创建软链接
ln -sf /usr/local/python3/bin/python3 /usr/bin/python3
  • 将/usr/local/python3/bin加入PATH
# vim ~/.bash_profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
  • 输入:wq回车退出,修改完执行下面的命令,让上一步的修改生效
source ~/.bash_profile
  • 检查python3是否安装成功,输出版本号及代表成功
[root@iZt4n8a2eru6403z3l0adjZ Python-3.10.6]# python3 -V
Python 3.10.6

pip3安装

在运行项目的过程中,经常会有一些第三方库需要安装,我们先安装 pip ,方便后续的一些操作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

  • 先安装 pip相关的前置依赖 setuptools(https://pypi.python.org/pypi/setuptools)
    下载对应的安装包,创建 setuptools 文件夹,在阿里云服务器上传到这个文件夹,进行解压编译
mkdir setuptools
cd setuptools
tar -xvf setuptools-36.6.0.tar.gz
cd setuptools-36.6.0
python3 setup.py build
python3 setup.py install
  • 安装pip(https://pypi.python.org/packages/source/)
    同样下载安装包,上传到对应文件夹,进行解压编译
mkdir pip3
cd pip3
tar -xvf pip-22.2.1.tar.gz
cd pip-22.2.1
python setup.py install
# 验证操作是否成功
pip --version

gunicorn

  • 安装 gunicorn
pip3 install gunicorn
# 查看是否安装成功
gunicorn -h
  • 上传项目文件,我们以下面最简单的 hello_world 为例
# hello.py

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
    return '<h1>hello world</h1>'

if __name__ == '__main__':
    app.run(debug=True)
  • 启动 gunicorn
    一般的命令是 gunicorn -w worker数量 -b ip:端口号 运行文件名:flask实例名,- w 表示有3 个 工作线程;- b 指定ip 和端口;app 为全局变量 (app = Flask(_ _name _ _)),例如我们这里就是:
gunicorn -b 127.0.0.1:5000 hello:app

这样子在地址栏直接输入127.0.0.1:5000就可以看到文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

阿里云服务器部署flask项目gunicorn + nginx + 支持https
Xnip2023-05-14_15-36-21.jpg

作为守护进程在后台运行文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

gunicorn -w 4 -b 127.0.0.1:5000 -D --access-logfile log文件路径 运行文件名称:Flask程序实例名
# 例:gunicorn -w 4 -b 127.0.0.1:5000 -D --access-logfile ./logs/log =hello:app
  • 重启gunicorn
    通过 ps -ef | grep gunicorn 获取对应的进程id,使用命令 kill -HUP 进程ID 进行重启。执行上述命令后,再次执行“pstree -ap|grep gunicorn”,我们很容易发现,除了主进程,其他的Gunicorn进程都已经销毁,并新建了进程(进程ID发生了变化)。
  • 关闭gunicorn
    通过 ps -ef | grep gunicorn 获取对应的进程id,使用命令 kill -9进程ID 进行关闭

nginx

使用Nginx主要是为了实现分流、转发、负载均衡,以及分担服务器的压力。Nginx部署简单,内存消耗少,成本低。Nginx既可以做正向代理,也可以做反向代理。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

  • 安装 pcre 前置依赖
cd /usr/src
wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
tar -xvf pcre-8.37.tar.gz
cd pcre-8.37
./configure
make && make install
#安装成功查看版本
pcre-config --version
  • 安装openssl 、zlib、gcc依赖
yum -y install make zlib zlib-devel gcc-c++ libtool    openssl openssl-devel
  • 安装nginx(https://nginx.org/download/)
    通过上面的链接,选择 nginx 的版本,我这里一开始选择的是 nginx-1.12.2.tar.gz,如果没有支持 https 的需求倒是问题不大,但是后面支持 https 访问的时候,安装 openssl 的依赖一直出错,最终是升级nginx 为 1.20.2 才解决,所以可以直接安装 1.20.2 版本。同样是通过上面的链接下载安装包,然后上传到 /usr/src/目录下
tar -xvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
./configure
make && make install
# 安装成功的话,会在 usr/local/ 多一个 nginx 目录
cd /usr/local/nginx/sbin
#启动 nginx
./nginx
#查看进程 
ps -ef | grep nginx

要在样子直接在浏览器访问公网的ip能够访问到,还需要修改Nginx的配置。路径是:/usr/local/nginx/conf/nginx.conf 文件。通过阿里云的文件树功能(或者vim也行)打开对应的文件,文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

阿里云服务器部署flask项目gunicorn + nginx + 支持https
nginxc.jpg

server_name 那里可填写公网的ip地址,或者你如果已经有对应的域名,并且增加了映射的话,可以填写自己的域名,修改保存之后执行 nginx -s reload 重新加载配置,这样子直接访问对应的公网IP地址或者域名就可以看到上面的 hello world 页面啦。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

支持Https

首先需要你有对应的ssl证书,如果使用自生成的在访问的时候,一些主流的浏览器也会提示不安全。阿里云的服务器可以免费提供这个证书。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

获取SSL证书

登录阿里云 - 数字证书管理服务- SSL证书,选择免费证书,创建证书,配置证书,绑定域名,下载证书,环境选择 Nginx文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

阿里云服务器部署flask项目gunicorn + nginx + 支持https
ssl.jpg

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

下载后解压。得到证书的密钥文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

修改nginx.conf 的配置

将证书上传到阿里云服务器,这里我创建了一个文件夹 /usr/src/cert,然后修改 nginx.conf 文件的配置,增加多一个 server,具体内容如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

    server {
        listen       443 ssl;
        server_name  你的域名;
        ssl_certificate      /usr/src/cert/xxx.pem;
        ssl_certificate_key  /usr/src/cert/xxx.key;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_protocols  SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2 ;
        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        #ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://127.0.0.1:5000;
            add_header Access-Control-Allow-Origin *;

        }
    }

最终文件内容如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

阿里云服务器部署flask项目gunicorn + nginx + 支持https
https.jpg

保存并执行 nginx -s reload重载配置,这个时候会报错文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf

这个是因为我们一开始在安装 nginx 的时候,没有开启 ssl 模块,cd 到我们的 nginx源码文件夹文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

cd /usr/src/nginx-1.20.2
./nginx -V
# 可以看到上面的输出,在configure arguments:后面是不带参数的
nginx 开启ssl 模块

一般这个时候,看到网上通用的做法是执行如下命令文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

这个时候就会出现如下错误:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

./configure: error: SSL modules require the OpenSSL library

这个是因为系统是centos7.9,原生openssl版本是1.0.2k,需要进行升级 openssl 版本的升级。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

  • openssl 升级为 1.1.1s(https://www.openssl.org/source/old/1.1.1/)
1.下载openssl1.1.1s版本的安装包
cd /usr/src/
wget https://www.openssl.org/source/openssl-1.1.1s.tar.gz --no-check-certificate

2.编译openssl
tar xf openssl-1.1.1s.tar.gz
cd openssl-1.1.1s
./config && make && make install

3. 更换openssl版本
echo "/usr/local/lib64/" >> /etc/ld.so.conf
ldconfig
mv /usr/bin/openssl /usr/bin/openssl.old
ln -sv /usr/local/bin/openssl /usr/bin/openssl

4.查看版本信息
# openssl version
OpenSSL 1.1.1s  1 Nov 2022
nginx 重新构建
cd cd /usr/src/nginx-1.20.2
./configure  --with-http_stub_status_module --with-pcre=/usr/src/pcre-8.37  --with-stream --with-http_ssl_module --with-openssl=/usr/src/openssl-1.1.1s
make && make install 
重启nginx
service nginx stop
service nginx start

这个时候就可以使用https 访问你的公域ip 或者域名啦。
大功告成!!!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

常用命令

nginx
  • 启动 nginx 服务:service nginx start
  • 停止 nginx 服务:service nginx stop
  • 重启 nginx 服务:service nginx restart 或者 ./nginx -s quit && ./nginx
  • 重新加载修改了的配置:nginx -s reload
pip指定源

pip install -i http://mirrors.aliyun.com/pypi/simple Pillow文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

遇到的错误

  • nginx: [error] invalid PID number “” in “/usr/local/nginx/logs/nginx.pid”
    在编辑nginx.conf 之后运行时出现nginx已经在运行,然后输入./nginx -s reload时出现
nginx: [error] invalid PID number “” in “/usr/local/nginx/logs/nginx.pid” 

解决方案:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html

cd /usr/local/nginx/sbin/      #进入/usr/local/nginx/sbin/目录
killall -9 nginx              # 杀掉所有nginx进程
./nginx -t                #检查配置文件是否有错
./nginx -c /usr/local/nginx/conf/nginx.conf     # 指定配置文件-c启动nginx
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/42567.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/42567.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定