使用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 uwsgi
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/79/73/b5def500729e134d
1cb8dfc334e27fa2e9cfd4e639c1f60c6532d40edaed/uwsgi-2.0.23.tar.gz (810 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: 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 main
          json_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_wheel
          return 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_wheel
          return 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
quires
          self.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_setup
          super(_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_setup
          exec(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_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\runpy.
py", line 87, in _run_code
    exec(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 Application
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-p
ackages\gunicorn\app\base.py", line 11, in <module>
    from gunicorn import util
  File "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
  1. 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 / {
        # 允许所有请求方法,包括 POST
        allow 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.通过域名访问,大工告成!

相关推荐
全栈工程师修炼指南4 分钟前
Nginx | stream 四层反向代理:SSL、PREREAD 阶段模块指令浅析与实践
运维·网络·网络协议·nginx·ssl
威迪斯特1 小时前
CentOS图形化操作界面:理论解析与实践指南
linux·运维·centos·组件·图形化·桌面·xserver
一方热衷.1 小时前
在线安装对应版本NVIDIA驱动
linux·运维·服务器
玄同7651 小时前
Python 后端三剑客:FastAPI/Flask/Django 对比与 LLM 开发选型指南
人工智能·python·机器学习·自然语言处理·django·flask·fastapi
独自归家的兔1 小时前
ubuntu系统安装dbswitch教程 - 备份本地数据到远程服务器
linux·运维·ubuntu
ONE_SIX_MIX1 小时前
ubuntu 24.04 用rdp连接,桌面黑屏问题,解决
linux·运维·ubuntu
龙飞051 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
春日见1 小时前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
DARLING Zero two♡1 小时前
告别 Docker 命令行!Portainer+cpolar 让容器管理从局域网走向公网
运维·docker·容器
消失的旧时光-19432 小时前
Linux 编辑器入门:nano 与 vim 的区别与选择指南
linux·运维·服务器