flask + nginx + uWSGI部署

Posted Python程序设计教程

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask + nginx + uWSGI部署相关的知识,希望对你有一定的参考价值。


01

openssl


  1. 安装openssl1.1.1.f

wget ftp://ftp.fi.muni.cz/pub/openssl/source/openssl-1.1.1f.tar.gz

  $ zxvf openssl-1.1.1f.tar.gz
$ cd openssl-1.1.1f/
$ ./config --prefix=/usr/local/openssl-1.1.1f

$ make && make install

2.检查结果

  $ cd /usr/local/openssl-1.1.1f/
$ ldd bin/openssl
linux-vdso.so.1 => (0x00007ffc30ef0000)
libssl.so.1.1 => not found
libcrypto.so.1.1 => not found
libdl.so.2 => /lib64/libdl.so.2 (0x00007f3d29a58000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f3d2983c000)
libc.so.6 => /lib64/libc.so.6 (0x00007f3d2946f000)

/lib64/ld-linux-x86-64.so.2 (0x00007f3d29c5c000)

这个时候是不能运行的

$ ./bin/openssl version
./bin/openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

修复错误

$ echo "/usr/local/openssl-1.1.1f/lib/" >> /etc/ld.so.conf

$ ldconfig -v

  $ mv /usr/bin/openssl /usr/bin/openssl.old
$ ln -s /usr/local/openssl-1.1.1f/bin/openssl /usr/bin/openssl
$ openssl version


./bin/openssl version

OpenSSL 1.1.1f  31 Mar 2020


*可以看看ldconfig命令的用途

02


uwsgi配置

编写uwsgi.ini文件:

[uwsgi]

master = true

http=127.0.0.1:5051

#socket=127.0.0.1:5000

pythonpath=/opt/myweb

chdir = /opt/myweb

wsgi-file=/opt/myweb/run.py

callable=app

processes=4

threads=2

buffer-size = 65536

vacuum=true

pidfile =/root/myweb/uwsgi.pid

其中/opt/myweb为你的程序路径

运行uwsgi:

uwsgi uwsgi.ini

测试一下服务端口:

flask + nginx + uWSGI部署

看一下服务是否正常:

写一个flask代码:run.py

from flask import Flask

app = Flask(__name__)


@app.route('/')

def hello_world():

    return 'Hello, World!'

写一个测试脚本:test.py

import urllib.request

res=urllib.request.urlopen('http://127.0.0.1:5051/')

print(res.read())


运行后能打出helloworld:

flask + nginx + uWSGI部署

表示运行可以正常。

03


nginx配置

flask + nginx + uWSGI部署

04


测试

显示internal error

一 .查看报错信息?

nginx 默认报错日志输出位置/var/log/nginx/error.log warn;

/04/20 13:59:35 [crit] 2409#2409: *7 connect() to 127.0.0.1:5000 failed (13: Permission denied) while connecting to upstream, client: 192.168.13.22, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "10.22.32.63"

2020/04/20 13:59:35 [crit] 2409#2409: *7 connect() to 127.0.0.1:5000 failed (13: Permission denied) while connecting to upstream, client: 192.168.13.22, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:5000/favicon.ico", host: "10.22.32.63"

2020/04/20 13:59:39 [crit] 2409#2409: *7 connect() to 127.0.0.1:5000 failed (13: Permission denied) while connecting to upstream, client: 192.168.13.22, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "10.22.32.63"

2020/04/20 14:00:48 [crit] 2409#2409: *11 connect() to 127.0.0.1:5000 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:5000/", host: "localhost"

二 .处理思路

Linux命令行输入:# setsebool -P httpd_can_network_connect 1,执行成功后就对了

05

设置权限

  • setsebool -P httpd_can_network_connect 1


flask + nginx + uWSGI部署

=======================================================

本号旨在打造一个小白Python学习社区。学习社区主要聚焦于Python编程、科学数据、可视化、机器学习、金融大数据量化等内容。学习内容一篇文章控制在15-20分钟左右。欢迎大家留言交流。


======================================================







以上是关于flask + nginx + uWSGI部署的主要内容,如果未能解决你的问题,请参考以下文章

Flask+uWSGI+Nginx项目部署

centos部署flask+nginx+uwsgi之踩坑指南

使用Nginx和uwsgi部署Flask项目

Ubuntu18部署uwsgi+flask应用

用uWSGI和Nginx部署Flask项目

使用 Nginx + uWSGI 部署 Flask 应用