Flask+Gunicorn+Nginx 校园众筹项目部署全流程(生产环境)

Flask+Gunicorn+Nginx 校园众筹项目部署全流程(生产环境)

一、项目背景

本项目为 Flask 工厂函数模式开发的校园众筹平台,需部署在 CentOS 系统中,实现 稳定运行 + 开机自启 + 多进程并发 的生产环境能力。

二、部署环境

  • 操作系统:CentOS 7.x
  • Python 版本:3.8+
  • 核心组件:Gunicorn 23.0.0 + Nginx 1.20+
  • 项目结构:Flask 工厂函数模式(create_app 启动)

三、部署核心步骤

1. 项目代码上传与准备

bash

运行

复制代码
# 1. 创建项目目录
mkdir -p /var/www/html/campus_crowdfunding
# 2. 上传并解压项目代码到该目录
unzip campus_crowdfunding.zip -d /var/www/html/campus_crowdfunding
# 3. 进入项目目录
cd /var/www/html/campus_crowdfunding
# 4. 安装项目依赖
pip3 install -r requirements.txt

2. Gunicorn 启动适配(工厂函数模式)

bash

运行

复制代码
# 测试启动(前台运行,验证逻辑)
/root/.local/bin/gunicorn --bind 127.0.0.1:8000 "app:create_app('development')"
# 验证:终端无报错,可通过 curl 访问
curl http://127.0.0.1:8000

注意:工厂函数模式必须用 "app:create_app('development')" 作为启动参数,直接用 app:app 会报实例找不到错误。

3. Gunicorn 系统服务配置(实现开机自启)

3.1 创建服务文件

bash

运行

复制代码
vi /etc/systemd/system/campus_crowdfunding.service

写入以下配置(含并发数 -w 4):

ini

复制代码
[Unit]
Description=Campus Crowdfunding Gunicorn Service
After=network.target

[Service]
User=root
WorkingDirectory=/var/www/html/campus_crowdfunding
ExecStart=/root/.local/bin/gunicorn --bind 127.0.0.1:8000 -w 4 "app:create_app('development')"
Restart=on-failure
Environment="PATH=/usr/local/bin:/usr/bin:/root/.local/bin"
StandardOutput=append:/var/log/campus_crowdfunding.log
StandardError=append:/var/log/campus_crowdfunding.log

[Install]
WantedBy=multi-user.target
3.2 启动并设置自启

bash

运行

复制代码
# 重载 systemd 配置
systemctl daemon-reload
# 启动服务
systemctl start campus_crowdfunding
# 设置开机自启
systemctl enable campus_crowdfunding
# 验证服务状态
systemctl status campus_crowdfunding

4. Nginx 反向代理配置

4.1 编辑 Nginx 站点配置

bash

运行

复制代码
vi /etc/nginx/conf.d/default.conf

写入反向代理配置:

nginx

复制代码
server {
    listen 80;
    server_name 192.168.226.128;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    # 静态文件映射(可选)
    location /static/ {
        root /var/www/html/campus_crowdfunding;
        expires 30d;
    }
}
4.2 验证并重启 Nginx

bash

运行

复制代码
# 验证配置语法
nginx -t
# 重启 Nginx
systemctl restart nginx
# 验证 Nginx 自启状态
systemctl is-enabled nginx

四、常见问题排查

1. Gunicorn 启动失败:Address already in use

bash

运行

复制代码
# 查找占用 8000 端口的进程
ss -tulnp | grep 8000
# 强制释放端口
fuser -k 8000/tcp
# 重启服务
systemctl restart campus_crowdfunding

2. 服务文件无法保存:E212: 无法打开并写入文件

bash

运行

复制代码
# 切换到 root 用户或用 sudo 编辑
su root
# 或
sudo vi /etc/systemd/system/campus_crowdfunding.service

3. Gunicorn 服务启动后无进程

bash

运行

复制代码
# 查看服务日志
journalctl -u campus_crowdfunding -l
# 或前台运行启动命令查错
/root/.local/bin/gunicorn --bind 127.0.0.1:8000 -w 4 "app:create_app('development')"

五、最终验证

  1. 进程验证ps -ef | grep gunicorn 显示 1 个 master + 4 个 worker 进程
  2. 访问验证curl http://127.0.0.1 返回 Redirecting... 页面
  3. 自启验证 :重启系统后,执行 systemctl status campus_crowdfunding 确认服务自动启动

六、部署价值

本方案通过 systemd 托管 Gunicorn,实现了 开机自启、崩溃自愈、多进程并发 的生产级能力,解决了手动启动的临时性和不稳定性问题,同时适配 Flask 工厂函数模式的启动逻辑,保障了项目的长期稳定运行。

相关推荐
打工仔折腾 AI7 小时前
FastAPI 从本机到生产服务器:Nginx+Gunicorn+Uvicorn 完整部署实录
人工智能·后端·python·nginx·fastapi·gunicorn
Linux运维技术栈11 小时前
如何避免绕过CDN的攻击?基于 CDN 回源请求头鉴权的防御实操与安全性剖析
nginx·安全·cdn
Lsetea15 小时前
OpenSSL快速生成域名证书:含SAN的自签命令、验证与排错
nginx·https·ssl证书·openssl·自签证书
lpfasd12315 小时前
配置文件格式对比 与 AI Coding 的选择逻辑
python·flask·numpy
troy1281 天前
Python 基础语法(九):Django/Flask/FastAPI 三大 Web 框架详细对比解析
python·jupyter·django·flask·github·fastapi
小小的木头人2 天前
CentOS 7 下 Nginx 访问自定义目录报 403 的完整排查:从端口权限到 SELinux
nginx·centos
小白学大数据2 天前
Python 项目实战:用 Flask 构建生产级 MySQL 增删改查 REST API
开发语言·人工智能·python·mysql·flask
新时代农民工~2 天前
【双机高可用部署方案-前后端部署】
java·nginx·springboot
运维全栈笔记2 天前
Nginx 模块化多业务站点通用配置模板
linux·运维·nginx
典典分享指南2 天前
用飞书 Wiki 搭内容作战室:从零搭建团队内容协作中枢
django·flask·numpy·pyqt