使用waitress与nginx在腾讯云上进行flask项目部署

2023-12-02 15:20

本文主要是介绍使用waitress与nginx在腾讯云上进行flask项目部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.首先介绍一下waitress与nginx是啥,

  1. Waitress:

    • 类别: Waitress 是一个用于 Python Web 应用程序的 WSGI(Web Server Gateway Interface)服务器。WSGI 是 Python Web 应用程序和 Web 服务器之间的标准接口,允许开发人员使用不同的框架编写 Web 应用程序,而不用担心与特定的服务器兼容性问题。
    • 用途: Waitress 主要用于运行 Python Web 应用程序,它可以作为一个独立的服务器来提供 Web 服务。
  2. Nginx:

    • 类别: Nginx 是一个高性能的开源 Web 服务器,也可以用作反向代理服务器、负载均衡器和 HTTP 缓存。
    • 用途: Nginx 主要用于托管静态内容、处理反向代理以及负载均衡。它是一个轻量级、高度可定制的服务器,被广泛用于提高 Web 应用程序的性能和可用性。

简而言之,Waitress 是一个专注于运行 Python Web 应用程序的服务器,而 Nginx 是一个通用的高性能 Web 服务器,适用于托管各种类型的 Web 内容和服务。在一些场景中,你可能会将 Waitress 与 Nginx 结合使用,其中 Nginx 充当反向代理服务器,而 Waitress 处理 Python Web 应用程序的请求。这样的组合可以充分利用 Nginx 的高性能和 Waitress 的专业性。

2.为什么使用Waitress而不是其他的服务器,例如uWSGI,gunicorn

这与我的开发环境与生产环境有关,我使用的环境皆是windows,而当我试图在windows上安装uWSGI,gunicorn时出现了错误,修改到最后还是放弃了这个方案

uWSGI的错误出现在安装它的时候

C:\Users\Administrator>pip install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/si
mple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting uwsgiUsing cached https://pypi.tuna.tsinghua.edu.cn/packages/79/73/b5def500729e134d
1cb8dfc334e27fa2e9cfd4e639c1f60c6532d40edaed/uwsgi-2.0.23.tar.gz (810 kB)Installing build dependencies ... doneGetting requirements to build wheel ... errorerror: subprocess-exited-with-error× Getting requirements to build wheel did not run successfully.│ exit code: 1╰─> [19 lines of output]Traceback (most recent call last):File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\
site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353,in <module>main()File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\
site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335,in mainjson_out['return_val'] = hook(**hook_input['kwargs'])File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\
site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 118,in get_requires_for_build_wheelreturn hook(config_settings)File "C:\Users\Administrator\AppData\Local\Temp\2\pip-build-env-m26e9hf4
\overlay\Lib\site-packages\setuptools\build_meta.py", line 325, in get_requires_
for_build_wheelreturn self._get_build_requires(config_settings, requirements=['wheel'
])File "C:\Users\Administrator\AppData\Local\Temp\2\pip-build-env-m26e9hf4
\overlay\Lib\site-packages\setuptools\build_meta.py", line 295, in _get_build_re
quiresself.run_setup()File "C:\Users\Administrator\AppData\Local\Temp\2\pip-build-env-m26e9hf4
\overlay\Lib\site-packages\setuptools\build_meta.py", line 480, in run_setupsuper(_BuildMetaLegacyBackend, self).run_setup(setup_script=setup_scri
pt)File "C:\Users\Administrator\AppData\Local\Temp\2\pip-build-env-m26e9hf4
\overlay\Lib\site-packages\setuptools\build_meta.py", line 311, in run_setupexec(code, locals())File "<string>", line 3, in <module>File "C:\Users\Administrator\AppData\Local\Temp\2\pip-install-pg6_kfal\u
wsgi_05f18753100a4e25a475b36504fbabe5\uwsgiconfig.py", line 8, in <module>uwsgi_os = os.uname()[0]AttributeError: module 'os' has no attribute 'uname'[end of output]note: This error originates from a subprocess, and is likely not a problem wit
h pip.
error: subprocess-exited-with-error× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.note: This error originates from a subprocess, and is likely not a problem with
pip.

 gunicorn的错误出现在我试图使用它运行flask项目的时候

User
C:\Users\Administrator\Desktop\******>gunicorn -w 4 -t 30 -b 0.0.0.0
:5000 app:index
Traceback (most recent call last):File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\runpy.
py", line 197, in _run_module_as_mainreturn _run_code(code, main_globals, None,File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\runpy.
py", line 87, in _run_codeexec(code, run_globals)File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\Scripts\gu
nicorn.exe\__main__.py", line 4, in <module>File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-p
ackages\gunicorn\app\wsgiapp.py", line 9, in <module>from gunicorn.app.base import ApplicationFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-p
ackages\gunicorn\app\base.py", line 11, in <module>from gunicorn import utilFile "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-p
ackages\gunicorn\util.py", line 8, in <module>import fcntl
ModuleNotFoundError: No module named 'fcntl'

它们的运行或安装无一例外都是需要UNIX/Linux,都是默认在 UNIX/Linux 上运行,因此选择 Waitress作为windows上的服务器是最好的选择

3.nginx命令

先来了解一点nginx命令方便下面操作

nginx.exe 启动nginx
nginx.exe -t 检查conf配置有没有问题
nginx.exe -s stop 停止nginx
nginx.exe -s reload 更新nginx

4. Waitress命令

waitress-serve --host=0.0.0.0 --port=5000 myapp:application

修改成与flask项目配置有关的端口号与地址

如果您的 Flask 应用程序在名为 的文件中定义myapp.py,并且 WSGI 应用程序对象名为application

5.开始!

1.准备好你的flask项目

2.启动它

3.使用 Waitress与它绑定并监视来自nginx的请求

waitress-serve --host=0.0.0.0 --port=5000 myapp:application

4.修改nginx配置文件,使其适用于你的项目

server {listen       8080;  # 修改为您的所需端口server_name  localhost;location / {# 允许所有请求方法,包括 POSTallow all;proxy_pass ****;  # 更改为您的Flask应用程序的主机和端口}location /static/ {root ******;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}
}

5.启动nginx

nginx.exe 启动nginx
nginx.exe -t 检查conf配置有没有问题
nginx.exe -s reload 更新nginx
nginx.exe -s stop 停止nginx
nginx.exe 启动nginx

6.通过域名访问,大工告成!

这篇关于使用waitress与nginx在腾讯云上进行flask项目部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/445839

相关文章

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热