flask用nginx+uwsgi进行项目部署

linux系统: ubantu 20.4

python3.9

查看当前pip安装的所有插件:pip list

生成当前项目使用的python版本的安装环境: pip freeze > requirements.txt

下载安装环境 pip install -r requirements.txt 注意:如有虚拟环境记得切换一下环境再安装

修改host地址:将启动项目时的host设为0.0.0.0,这样所有人就可以访问了

sudo apt-get install nginx

安装完成后,切换到nginx安装目录,找到配置文件目录

cd /etc/nginx/conf.d

进入到配置文件目录后,新建一个该项目的server config

touch 项目名.conf

创建完成后,编辑conf文件进行配置

vim 项目名.conf

可直接粘贴以下内容:

upstream paitech {
          server 0.0.0.0:5001;

  }
  server {
          listen 80;
          server_name localhost;
          location / {
          # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
          # 包含uwsgi的请求参数
          include uwsgi_params;
          # 转交给uwsgi
          uwsgi_pass 0.0.0.0:5000;
          proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
         }
 }

​ 上面代码可根据自己情况修改server 的地址和端口,如:项目中使用的端口是8080,则可以将5001改为8080,如有多台服务器,可以换行继续加服务器地址,多台服务器可以加权重:weight=任意数字,数字越大,权重越高,被访问到几率越高

保存退出

Esc  :wq  enter

重启nginx

service nginx restart

终端输入命令

pip install uwsgi -i https://pypi.tuna.tsinghua.edu.cn/simple

在项目的main.py同级目录下新建文件myuwsgi.ini, 参考配置

[uwsgi]

socket = 0.0.0.0:5001

chdir = /home/.../你的项目地址

module = main

master=True

processes=4

threads = 10

pidfile=%(chdir)\uwsgi.pid

daemonize = %(chdir)\log\uwsgi.log

vacuum=True

memory-report=true

启动和停止uwsgi

uwsgi --ini uwsgi.ini

uwsgi --stop uwsgi.pid

错误总结

unable to find “application” callable in file
unable to load app 0 (mountpoint=”) (callable not found or import error)

出现这种错误原因是文件里面使用了if name == ‘ main‘这个语句,uWSGI不会将您的应用程序加载为__main__,因此它永远不会找到app,解决方案

if __name__ == "__main__":
    app.run()
else:
    application = app

bind(): Address already in use

uwsgi启动次数过多,杀掉进程,重新运行即可

fuser -k 9090/tcp

查看端口占用情况

netstat -npl | grep "端口"

占用的话直接kill掉即可

kill -9 端口号

最后:使用0.0.0.0作为host时,测试需要使用本地ip地址

Original: https://blog.csdn.net/sj/article/details/124079271
Author: s
j
Title: flask用nginx+uwsgi进行项目部署

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/745306/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球