flask项目结构如下: -------------------------------------------------------------------------------------------- blog/ │ ├── app.py # 主程序 ├── static/ # 可选的静态资源文件夹 │ ├── style.css ├── templates/ │ ├── index.html # 首页模板 │ └── content.html # 显示内容页面模板 └── posts/ # 存放 .txt 文件的目录 ├── hello.txt ├── about.txt └── example.txt -------------------------------------------------------------------------------------------- 前端代码 ----------------------------------------------index.html-------------------------------------------------------- <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>林毓斌的博客</title> <link rel="stylesheet" href="/static/style.css"> </head> <body> <header class="header"> <h1>林毓斌的博客</h1> </header> <main class="container"> <div class="card-list"> {% for file in files %} <div class="card"> <a href="/post/{{ file }}" class="card-link"> {{ file }} </a> </div> {% endfor %} </div> </main> <footer class="footer"> <a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备2024227658号</a> </footer> </body> </html> ------------------------------------------------------------------------------------------------------------------- ----------------------------------------------content.html-------------------------------------------------------- <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>{{ title }}</title> <link rel="stylesheet" href="/static/style.css"> </head> <body> <header class="header"> <h1>{{ title }}</h1> <nav class="nav"> <a href="/">返回首页</a> </nav> </header> <main class="container content"> <pre>{{ content }}</pre> </main> <footer class="footer"> <a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备2024227658号</a> </footer> </body> </html> --------------------------------------------------------------------------------------------------------------------- ----------------------------------------------style.css-------------------------------------------------------- /* 全局样式 */ * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: "Segoe UI", sans-serif; background-color: #f9f9f9; color: #333; line-height: 1.6; padding: 1rem; } /* 头部 */ .header { text-align: center; padding: 1rem 0; border-bottom: 1px solid #ddd; } .nav { margin-top: 0.5rem; } .nav a { text-decoration: none; color: #007BFF; margin: 0 10px; font-weight: bold; } /* 容器 */ .container { max-width: 900px; margin: 2rem auto; padding: 0 1rem; } /* 首页文章卡片 */ .card-list { display: flex; flex-wrap: wrap; gap: 1rem; justify-content: center; } .card { background-color: white; border: 1px solid #ddd; border-radius: 8px; padding: 1rem; width: 250px; transition: transform 0.2s ease; box-shadow: 0 2px 5px rgba(0,0,0,0.05); } .card:hover { transform: translateY(-5px); } .card-link { text-decoration: none; color: #0056b3; font-size: 1.1rem; font-weight: 500; } /* 内容页文本 */ .content pre { white-space: pre-wrap; background-color: #fff; padding: 1rem; border: 1px solid #ddd; border-radius: 6px; overflow-x: auto; } /* 底部 */ .footer { position: fixed; bottom: 0; left: 0; width: 100%; background-color: #f1f1f1; text-align: center; padding: 0.5rem 0; border-top: 1px solid #ddd; z-index: 1000; font-size: 0.9rem; color: #666; } ---------------------------------------------------------------------------------------------------------------- 后端代码 ----------------------------------------------app.py-------------------------------------------------------- from flask import Flask, render_template, abort import os app = Flask(__name__) POSTS_DIR = 'posts' @app.route('/') def index(): files = [f for f in os.listdir(POSTS_DIR) if f.endswith('.txt')] file_names = [os.path.splitext(f)[0] for f in files] return render_template('index.html', files=file_names) @app.route('/post/<filename>') def view_post(filename): file_path = os.path.join(POSTS_DIR, filename + '.txt') if not os.path.exists(file_path): abort(404) with open(file_path, 'r', encoding='utf-8') as f: content = f.read() return render_template('content.html', title=filename, content=content) if __name__ == '__main__': app.run(debug=False) -------------------------------------------------------------------------------------------------------------- 本地测试 ------------------------------------------------------- python app.py ------------------------------------------------------- 测试没问题后,上传到服务器,安装nginx ---------------------------------------------------- yum install nginx ---------------------------------------------------- 配置nginx,443转发5000,http重定向https ---------------------------查找配置文件位置----------------------- nginx -V ----------------------------------------------------------------- --------------------------443部分---------------------------------- server_name linyubin.top www.linyubin.top; location / { proxy_pass http://localhost:5000; } ssl_certificate "linyubin.top_bundle.crt"; ssl_certificate_key "linyubin.top.key"; ----------------------------------------------------------------------- --------------------------80部分------------------------------------ server_name linyubin.top www.linyubin.top; return 301 https://linyubin.top; ----------------------------------------------------------------------- 配置好后,检查结果successful后就可以运行nginx ----------------------------------------------------------------- nginx -t ----------------------------------------------------------------- ----------------------------------------------------------------- nginx ----------------------------------------------------------------- 切换到项目目录,不间断运行flask项目 ------------------------------------------- pip3 install flask ------------------------------------------- ------------------------------------------------------------------------------------------ nohup python3 app.py --host=0.0.0.0 --port=5000 > app.log 2>&1 & ------------------------------------------------------------------------------------------ 取消5000的防火墙,系统设置和服务器设置,缺一不可 ----------------------------------------------------------------------------------- iptables -A INPUT -p tcp --dport 5000 -j ACCEPT ----------------------------------------------------------------------------------- 后期项目改动后要重启flask应用,先通过pid结束进程,再启动 ----------------------------------------------------------------- lsof -i :5000 ----------------------------------------------------------------- ----------------------------------------------------------------- kill <pid> ----------------------------------------------------------------- 如果是nginx改动,则重启nginx ----------------------------------------------------------------- nginx -s reload -----------------------------------------------------------------