flask+uwsgi+nginx+cerbot配置

配置步骤

安装flask和uwsgi

bash 复制代码
pip install Flask uwsgi

创建一个简单的flask应用(app.py)或者是自己的flask项目

python 复制代码
from flask import Flask
app = Flask(__name__)
 
@app.route('/')
def hello_world():
    return 'Hello, World!'

配置uwsgi,这里我给出自己的配置

bash 复制代码
[uwsgi]
socket = :5000
wsgi-file = /root/blog/start.py
master = true
callable = app
chdir = /root/blog/
module = start:app
harakiri = 30
vacuum = true
die-on-term = true
limit-as = 512
buffer-size=65535
  • socket应该是和nginx通信的socket,注意不是http,因为uwsgi和nginx通信使用的是uwsgi协议
  • wsgi-file是程序启动文件,也就是python xxx.py的那个文件,注意写绝对路径
  • calllable是程序启动文件中应用的名字,一般就是app(xxx.run()那个xxx)
  • chdir是项目的根目录,建议写绝对路径
  • module的start是程序启动文件不带.py,app就是上面callable那个app
  • 其余参数不用修改

启动uwsgi

bash 复制代码
uwsgi --ini uwsgi.ini

配置Nginx (/etc/nginx/sites-available/default)

bash 复制代码
server {
    listen 80;
    server_name your_domain.com;
 
    location / {
        include uwsgi_params;
        uwsgi_pass the_uwsgi_socket:port;
    }
}

需要修改的是your_domain.com是域名,the_uwsgi_socket就写0.0.0.0,port和uwsgi配置中socket端口保持一致,我这里就是5000

启动nginx

bash 复制代码
sudo systemctl start nginx

安装Cerbot

bash 复制代码
sudo apt-get install certbot python3-certbot-nginx

使用certbot为nginx配置https

python 复制代码
sudo certbot --nginx

问题及解决

  • The -s/--socket option is missing and stdin is not a socket.

https://www.cnblogs.com/qiaoer1993/p/16282109.html

  • unable to load configuration from uwsgi

https://stackoverflow.com/questions/34615743/unable-to-load-configuration-from-uwsgi

参考

How To Serve Flask Applications with uWSGI and Nginx on Ubuntu 22.04 | DigitalOcean

我的博客

不负长风

相关推荐
狐凄12 分钟前
Python实例题:Python计算线性代数
开发语言·python·线性代数
西猫雷婶13 分钟前
pytorch基本运算-导数和f-string
人工智能·pytorch·python
述雾学java16 分钟前
深入理解 transforms.Normalize():PyTorch 图像预处理中的关键一步
人工智能·pytorch·python
要努力啊啊啊19 分钟前
使用 Python + SQLAlchemy 创建知识库数据库(SQLite)—— 构建本地知识库系统的基础《一》
数据库·人工智能·python·深度学习·自然语言处理·sqlite
2301_7930698232 分钟前
Azure 虚拟机端口资源:专用 IP 和公共 IP Azure Machine Learning 计算实例BUG
tcp/ip·flask·azure
Andrew_Xzw1 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
凤头百灵鸟2 小时前
Python语法基础篇(包含类型转换、拷贝、可变对象/不可变对象,函数,拆包,异常,模块,闭包,装饰器)
python
多多*3 小时前
LUA+Reids实现库存秒杀预扣减 记录流水 以及自己的思考
linux·开发语言·redis·python·bootstrap·lua
何双新4 小时前
第21讲、Odoo 18 配置机制详解
linux·python·开源
Wish3D4 小时前
阿里云OSS 上传文件 Python版本
开发语言·python·阿里云