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

我的博客

不负长风

相关推荐
IT毕设实战小研12 分钟前
基于大数据的桂林热门旅游数据分析与可视化
大数据·python·机器学习·数据分析·django·课程设计·旅游
SamChan9016 分钟前
用PostgreSQL+pgvector构建PDF翻译记忆库:向量相似度检索+增量更新实战
数据库·python·ai·postgresql·pdf·wpf
金融小师妹17 分钟前
多因子市场推演:油价、英伟达与杰克逊霍尔如何影响资产定价的AI事件预测框架
人工智能·python·深度学习
大海变好AI23 分钟前
AI 工作流怎么搭建提升效率
大数据·人工智能·python
叫我:松哥23 分钟前
基于flask图书管理系统,技术栈flask+layui+sqlite+Echarts
后端·python·数据分析·flask·echarts·layui
落魄大学生之流水线上谋生计23 分钟前
Python Literal[] 类型提示详解
开发语言·python
nanawinona25 分钟前
2026年下半年按能力基础选量化工具
人工智能·python
赵民勇26 分钟前
Python泛型使用技巧
python
jyOverQ36 分钟前
LangGraph:子图动态路由与 Agent 工作流设计
python·langchain
晴天1640 分钟前
Gradio 与 Streamlit:Python 快速构建 Web 应用的双子星-Day28
开发语言·前端·python