使用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 
          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 "", line 3, in 
        File "C:\Users\Administrator\AppData\Local\Temp\2\pip-install-pg6_kfal\u
wsgi_05f18753100a4e25a475b36504fbabe5\uwsgiconfig.py", line 8, in 
          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 
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-p
ackages\gunicorn\app\wsgiapp.py", line 9, in 
    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 
    from gunicorn import util
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python39\lib\site-p
ackages\gunicorn\util.py", line 8, in 
    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 / {
        # 允许所有请求方法,包括 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.通过域名访问,大工告成!

你可能感兴趣的:(nginx,flask,运维)