阿里云CentOS 使用Flask建站(nginx + gunicorn + supervisor + flask)

一、建站的基本思路和大致流程(先了解nginx + gunicorn + supervisor + flask)

    基本思路如下图:

阿里云CentOS 使用Flask建站(nginx + gunicorn + supervisor + flask)_第1张图片

    详细介绍请看这篇文章:点我查看建站的基本思路和大致流程(详细版)

二、准备动手之前的准备工作

准备动手之前,你肯定在网上搜索了一大堆的资料,这里要明确几个点,

操作系统:CentOS  (如果你看到文章里教你用apt-get,那么你就得慎重了,那篇文章应该是基于ubantu的,CentOS里对应的是yum

Python版本:如果你的Python版本是3.*以上版本,建议用python3,如果你的电脑有多个版本,python 有可能指向的是python2

Pip:Pip是Python上安装各种插件的工具,同理Python3,此处建议pip3

如果以上工具还没有准备,请自行准备,最新的阿里云CentOS 8.*以上版本好像都自带了

三、安装 python web 框架 — flask

flask 是一个 python web 轻型框架,简洁高效。flask 依赖两个库 werkzeug 和 jinjia2。可采用 pip 方式安装: 

pip3 install flask

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

vim run.py #我是备注: 此处是用linux上自动的文本编辑工具,不熟悉的朋友可以先自行学习下linux操作指令,

 

from flask import Flask

app = Flask(__name__)

@app.route('/')

def index():
    return 'hello world!'

if __name__ == '__main__':
    app.run()

此处需要记住app和文件名,后续会用上

启动 flask

python3 run.py

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

四、使用 gunicorn 部署 python web

现在我们使用 flask 自带的服务器,完成了 web 服务的启动。生产环境下,flask 自带的 服务器,无法满足性能要求。我们这里采用 gunicorn 做 wsgi容器,用来部署 python,用pip直接安装。

pip3 install gunicorn

pip 是python 用来管理包的一个重要工具。每次安装新库后写入一个 requirement 文件里面,既能知道自己安装了什么库,也方便别人部署时,安装相应的库。

pip3 freeze > requirements.txt

以后每次 pip 安装了新的库的时候,都需freeze 一次。完整保存好requirement文本,重新安装库则只需要执行如下操作:

pip3 install -r requirements.txt

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

gunicorn -w 4 -b 0.0.0.0:5000 run:app #-w worker数量  -b bind地址 run:就是上面的文件名,app就是实例名称

此时,我们可以用 5000 的端口进行访问。

想要结束 gunicorn 只需执行 pkill gunicorn,有时候还需要用 ps 找到 pid 进程号才能 kill。

 

五、安装nginx

 

安装nginx应该是坑最多的地方,经常报错,其实都是因为版本不对,因为网上的资料都比较老,nginx经常更新,如果nginx的版本和OpenSSL对不上,就是各种错误。

这里个人建议先到nginx的网站看看最新的版本是什么,点我查看nginx的更新日志,这里以1.19.6为例,更新日期为2020-12-15,对应的OpenSSL1.1.1g.

1. gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++

2. PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

3. zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。 

yum install -y zlib zlib-devel

4. OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

 安装完成记得查看下版本,版本不对很坑,而且网上还有建议你退版本的,个人不建议。

5. 使用wget下载nginx:

wget https://nginx.org/download/nginx-1.19.6.tar.gz
#download之前先用pwd确定自己所在位置,
tar -zxvf nginx-1.19.6.tar.gz
#解压
cd nginx-1.19.6

./configure --with-http_ssl_module  # 具体参数视需求而定

make
 
make install  #编译安装

 点我查看nginx的configure详情

然后配置Nginx,刚才安装了Nginx之后,我们需要修改config,Nginx下的conf文件很多,具体是哪个呢?

可以用/usr/local/nginx/sbin/nginx -t 进行config的检查 

/usr/local/nginx/sbin/nginx -t 

此处可知,配置文件的路径为 /usr/local/nginx/conf/nginx.conf  用如下指令打开文件

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

 

server {
    listen       80;
    server_name  localhost;
    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_redirect off;
        proxy_set_header Host $host:80;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}

其中server_name就是你的域名,配置好之后可以检查下端口有没有被占用(前面的调试步骤可能占用端口),然后绑定配置文件

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf #指定配置文件并启动nginx

阿里云CentOS 使用Flask建站(nginx + gunicorn + supervisor + flask)_第2张图片

nginx启动指令 

[root@server ~]# service nginx start
[root@server ~]# nginx -s reload

六、为了方便管理使用supervisor

 

1.安装 supervisor

pip install supervisor
echo_supervisord_conf > supervisor.conf   # 生成 supervisor 默认配置文件
vim supervisor.conf                       # 修改 supervisor 配置文件,添加 gunicorn和nginx

2.添加gunicorn到supervisor中,添加到配置最下面。此处因为使用了python虚拟环境,可先在虚拟环境找到gunicorn地址,使用whereis 如:/usr/local/bin/gunicorn,记住此位置加入到supervisor的command中,如下:

【program:myflask】
command = gunicorn -w4 -b0.0.0.0:5000 run:app # 此处就是启动gunicorn的指令
directory = root  #位置 run.py文件所在的位置
autostart = true  #自动启动
startsecs = 5
autorestart = true #自动重启
startretries = 3  #启动失败时的最多重试次数
redirect_stderr = true #重定向stderr到stdout
stdout_logfile = /var/log/flask_supervisor.log

3、添加nginx进程到supervisor

[program:up_nginx]        
command = /usr/local/nginx/sbin/nginx   #启动nginx的指令,需要配置成nginx所在的位置
autostart = true             #随着supervisord的启动而启动
autorestart = true          #自动重启
startretries = 10             #启动失败时的最多重试次数
exitcodes = 0                 #正常退出代码
stopsignal = KILL            #用来杀死进程的信号
stopwaitsecs = 10            #发送SIGKILL前的等待时间
redirect_stderr = true        #重定向stderr到stdout
stdout_logfile = /home/myflask/log/nginx.log
stdout_logfile = /home/myflask/log/nginx.err

4.启用supervisor管理工具

# 执行此条指令,先要切到supervisor.conf所在的文件夹
supervisord -c supervisor.conf
#sudo unlink /tmp/supervisor.sock # 如上面启动失败,提示 Unlinking stale socket /tmp/supervisor.sock 先使用此命令之后在使用上面命令
supervisorctl start all
# 如果一直提示http://localhost:9001 refused connection
# 可以尝试 supervisorctl -c supervisor.conf
supervisord -c supervisor.conf          #通过配置文件启动supervisor
supervisorctl status                    #察看supervisor的状态
supervisorctl reload                    #重新载入 配置文件
supervisorctl start [all]|[appname]     #启动指定/所有 supervisor管理的程序进程
supervisorctl stop [all]|[appname]      #关闭指定/所有 supervisor管理的程序进程

 阿里云CentOS 使用Flask建站(nginx + gunicorn + supervisor + flask)_第3张图片

新版本的Supervisorctl需要指定conf的路径 使用此指令之前确保当前目录下有指定的.conf文件

阿里云CentOS 使用Flask建站(nginx + gunicorn + supervisor + flask)_第4张图片

七、从本地Copy文件到服务器

到此为止,应该服务器是run起来了,但是程序猿不可能在阿里云上写代码的,还是得本机写代码,本机写代码调试OK后,再远程复制到服务器。

scp -r /Users/kid/WeChatProjects/IAFood/Server [email protected]:/root/

scp就是用来上传代码的,-r表示多个文件(比如文件夹),中间的参数为路径(程序员所在电脑的路径),最后是账户@服务器地址 目标位置 

同理,如果你想从阿里云端把代码拿下来,只需要修改下相应路径和参数即可。

阿里云CentOS 使用Flask建站(nginx + gunicorn + supervisor + flask)_第5张图片

 

写在最后:

 https://www.cnblogs.com/lethon/p/8808368.html 这里要谢谢此篇文章的作者,本人也是参考此篇文章进行配置的,之间遇到了很多问题,所以在原作者基础上进行了一些修改,希望可以帮到大家!

你可能感兴趣的:(Python,阿里云,nginx,centos,python,阿里云,Flask)