From 531320697678b90e791e14e508e8189b15f4a05c Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 4 Jun 2026 21:59:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=20FastAPI=20HTTPS?= =?UTF-8?q?=20Hello=20World=20=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 包含 FastAPI 应用、Nginx 反向代理配置、systemd 服务及腾讯云 pip 源配置。 Co-authored-by: Cursor --- .gitignore | 4 ++++ app.py | 13 +++++++++++++ hair.service | 14 ++++++++++++++ nginx/hair.conf | 12 ++++++++++++ pip.conf | 3 +++ requirements.txt | 2 ++ start.sh | 3 +++ 7 files changed, 51 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 hair.service create mode 100644 nginx/hair.conf create mode 100644 pip.conf create mode 100644 requirements.txt create mode 100755 start.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b91c72 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv/ +__pycache__/ +*.pyc +.env diff --git a/app.py b/app.py new file mode 100644 index 0000000..6102c83 --- /dev/null +++ b/app.py @@ -0,0 +1,13 @@ +from fastapi import FastAPI + +app = FastAPI(title="hair") + + +@app.get("/") +async def hello(): + return {"message": "Hello, World!"} + + +@app.get("/health") +async def health(): + return {"status": "ok"} diff --git a/hair.service b/hair.service new file mode 100644 index 0000000..28a328c --- /dev/null +++ b/hair.service @@ -0,0 +1,14 @@ +[Unit] +Description=Hair FastAPI Application +After=network.target + +[Service] +Type=simple +User=ubuntu +WorkingDirectory=/home/ubuntu/hair +ExecStart=/home/ubuntu/hair/venv/bin/uvicorn app:app --host 127.0.0.1 --port 8000 +Restart=always +RestartSec=3 + +[Install] +WantedBy=multi-user.target diff --git a/nginx/hair.conf b/nginx/hair.conf new file mode 100644 index 0000000..0b8250f --- /dev/null +++ b/nginx/hair.conf @@ -0,0 +1,12 @@ +server { + listen 80; + server_name hair.xiangsilian.com; + + 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; + proxy_set_header X-Forwarded-Proto $scheme; + } +} diff --git a/pip.conf b/pip.conf new file mode 100644 index 0000000..6f2a15f --- /dev/null +++ b/pip.conf @@ -0,0 +1,3 @@ +[global] +index-url = https://mirrors.cloud.tencent.com/pypi/simple/ +trusted-host = mirrors.cloud.tencent.com diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..336a1d9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi==0.115.12 +uvicorn[standard]==0.34.2 diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..77c7e57 --- /dev/null +++ b/start.sh @@ -0,0 +1,3 @@ +#!/bin/bash +cd "$(dirname "$0")" +exec ./venv/bin/uvicorn app:app --host 127.0.0.1 --port 8000