Compare commits
1 Commits
1c316594f5
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 41bbdabf5b |
468
README.md
Normal file
468
README.md
Normal file
@@ -0,0 +1,468 @@
|
||||
## 安装 `python3.11`
|
||||
|
||||
### 一、Linux 安装 Python 3.11
|
||||
|
||||
#### 1. 更新系统和安装依赖
|
||||
|
||||
```bash
|
||||
sudo apt update
|
||||
sudo apt install -y software-properties-common
|
||||
```
|
||||
|
||||
#### 2. 添加 Python 3.11 PPA 源(Ubuntu)
|
||||
|
||||
```bash
|
||||
sudo add-apt-repository ppa:deadsnakes/ppa
|
||||
sudo apt update
|
||||
```
|
||||
|
||||
#### 3. 安装 Python 3.11
|
||||
|
||||
```bash
|
||||
sudo apt install -y python3.11 python3.11-venv python3.11-dev
|
||||
```
|
||||
|
||||
#### 4. 确认安装版本
|
||||
|
||||
```bash
|
||||
python3.11 --version
|
||||
```
|
||||
|
||||
#### 5. 设置 Python 3.11 为默认版本(可选)
|
||||
|
||||
> 如果需要将 Python 3.11 设置为系统默认 Python3 版本:
|
||||
|
||||
```bash
|
||||
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
|
||||
```
|
||||
|
||||
### 二、Windows 安装 Python 3.11
|
||||
|
||||
#### 1. 下载 Python 3.11
|
||||
|
||||
- 前往 [Python 官方下载页面](https://www.python.org/downloads/release/python-3110/)。
|
||||
- 选择 Windows 安装程序(Installer),并下载适合的版本(32位或64位)。
|
||||
|
||||
#### 2. 运行安装程序
|
||||
|
||||
- 双击下载的 `.exe` 文件,启动安装程序。
|
||||
- 勾选 **“Add Python 3.11 to PATH”**,以便系统能够识别 Python 命令。
|
||||
- 选择 **“Customize installation”** 以进行自定义安装。
|
||||
- 点击 **“Install”** 开始安装。
|
||||
|
||||
#### 3. 确认安装
|
||||
|
||||
- 在命令提示符(Command Prompt)或 PowerShell 中输入以下命令检查安装是否成功:
|
||||
|
||||
```cmd
|
||||
python --version
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 配置 Python 虚拟环境
|
||||
|
||||
### 1. Linux 环境
|
||||
|
||||
- **创建虚拟环境**
|
||||
```shell
|
||||
python3 -m venv 虚拟环境名称
|
||||
```
|
||||
|
||||
- **启动虚拟环境**
|
||||
```shell
|
||||
source 虚拟环境名称/bin/activate
|
||||
```
|
||||
|
||||
- **安装 Python 依赖**
|
||||
```shell
|
||||
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple -r ./requirements.txt
|
||||
```
|
||||
|
||||
### 2. Windows 环境
|
||||
|
||||
- **创建虚拟环境**
|
||||
```shell
|
||||
python -m venv 虚拟环境名称
|
||||
```
|
||||
|
||||
- **启动虚拟环境**
|
||||
```shell
|
||||
.\虚拟环境名称\Scripts\activate
|
||||
```
|
||||
|
||||
- **安装 Python 依赖**
|
||||
```shell
|
||||
pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple -r ./requirements.txt
|
||||
```
|
||||
|
||||
## MySQL 的安装和配置说明。
|
||||
|
||||
### 1. 在 Linux 上安装 MySQL
|
||||
|
||||
- **步骤 1**: 更新包列表并安装 MySQL 服务器
|
||||
```shell
|
||||
sudo apt update
|
||||
sudo apt install mysql-server -y
|
||||
```
|
||||
|
||||
- **步骤 2**: 启动 MySQL 服务并设置为开机启动
|
||||
```shell
|
||||
sudo systemctl start mysql
|
||||
sudo systemctl enable mysql
|
||||
```
|
||||
|
||||
- **步骤 3**: 安全配置 MySQL
|
||||
```shell
|
||||
sudo mysql_secure_installation
|
||||
```
|
||||
按照提示配置 root 密码,移除匿名用户,禁止 root 远程登录,删除测试数据库等。
|
||||
|
||||
- **步骤 4**: 登录 MySQL 并创建数据库和用户
|
||||
```shell
|
||||
sudo mysql -u root -p
|
||||
```
|
||||
使用以下命令创建数据库和用户并赋予权限:
|
||||
```sql
|
||||
CREATE DATABASE 数据库名称 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
CREATE USER '用户名'@'%' IDENTIFIED BY '用户密码';
|
||||
GRANT ALL PRIVILEGES ON 数据库名称.* TO '用户名'@'%';
|
||||
FLUSH PRIVILEGES;
|
||||
```
|
||||
|
||||
- **步骤 5**: 配置 MySQL 远程访问(如有需要)
|
||||
修改 MySQL 配置文件 `/etc/mysql/mysql.conf.d/mysqld.cnf`,将 `bind-address` 设置为 `0.0.0.0`:
|
||||
```ini
|
||||
bind-address = 0.0.0.0
|
||||
```
|
||||
保存并重启 MySQL 服务:
|
||||
```shell
|
||||
sudo systemctl restart mysql
|
||||
```
|
||||
|
||||
### 2. 在 Windows 上安装 MySQL
|
||||
|
||||
- **步骤 1**: 下载并安装 MySQL
|
||||
- 前往 [MySQL 官网下载](https://dev.mysql.com/downloads/installer/),选择适合的版本进行下载。
|
||||
- 安装时可选择默认安装路径,并勾选 MySQL Server 和 MySQL Workbench 等工具。
|
||||
|
||||
- **步骤 2**: 配置 MySQL
|
||||
- 在安装向导中,设置 root 用户的密码,并选择 MySQL 配置(建议选择开发者配置)。
|
||||
- 设置端口(默认 3306)和字符集为 `utf8mb4`。
|
||||
|
||||
- **步骤 3**: 配置环境变量(可选)
|
||||
- 打开 Windows 环境变量设置,将 MySQL 安装目录的 `bin` 文件夹路径(如 `C:\Program Files\MySQL\MySQL Server 8.0\bin`)添加到 `Path` 中,以便在命令行直接使用 `mysql` 命令。
|
||||
|
||||
- **步骤 4**: 登录 MySQL 并创建数据库和用户
|
||||
使用命令行工具登录 MySQL,并按照以下命令创建数据库和用户:
|
||||
```shell
|
||||
mysql -u root -p
|
||||
```
|
||||
```sql
|
||||
CREATE DATABASE 数据库名称 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
CREATE USER '用户名'@'localhost' IDENTIFIED BY '用户密码';
|
||||
GRANT ALL PRIVILEGES ON 数据库名称.* TO '用户名'@'localhost';
|
||||
FLUSH PRIVILEGES;
|
||||
```
|
||||
```
|
||||
|
||||
### 4. 验证数据库连接
|
||||
|
||||
- **配置完成后,可以在 FastAPI 项目中测试 MySQL 数据库连接,确保连接配置正确。**
|
||||
|
||||
|
||||
---
|
||||
|
||||
## 导入 `fastapi.sql` 数据文件。
|
||||
|
||||
- 在 MySQL 配置完成后,可以将项目的初始数据通过 `fastapi.sql` 文件导入到数据库中。
|
||||
|
||||
### 1. 在 Linux 上导入 `fastapi.sql` 数据
|
||||
|
||||
- **步骤 1**: 登录到 MySQL
|
||||
```shell
|
||||
mysql -u 用户名 -p
|
||||
```
|
||||
系统会提示输入密码,输入后进入 MySQL 命令行。
|
||||
|
||||
- **步骤 2**: 使用 `source` 命令导入数据
|
||||
在 MySQL 命令行中,选择目标数据库并导入 `fastapi.sql` 文件:
|
||||
```sql
|
||||
USE 数据库名称;
|
||||
SOURCE /path/to/fastapi.sql;
|
||||
```
|
||||
确保 `fastapi.sql` 文件路径正确,数据将被导入指定的数据库。
|
||||
|
||||
- **步骤 3**: 验证导入
|
||||
导入完成后,可以执行一些简单查询来验证数据是否导入成功:
|
||||
```sql
|
||||
SELECT * FROM 表名称 LIMIT 5;
|
||||
```
|
||||
|
||||
### 2. 在 Windows 上导入 `fastapi.sql` 数据
|
||||
|
||||
- **步骤 1**: 打开命令提示符并登录到 MySQL
|
||||
打开命令提示符(CMD)并运行以下命令:
|
||||
```shell
|
||||
mysql -u 用户名 -p
|
||||
```
|
||||
输入密码后进入 MySQL 命令行。
|
||||
|
||||
- **步骤 2**: 使用 `source` 命令导入数据
|
||||
在 MySQL 命令行中,先选择数据库,然后导入 `fastapi.sql` 文件:
|
||||
```sql
|
||||
USE 数据库名称;
|
||||
SOURCE C:\\path\\to\\fastapi.sql;
|
||||
```
|
||||
注意:Windows 系统中路径使用双反斜杠 `\\`。
|
||||
|
||||
- **步骤 3**: 验证导入
|
||||
导入完成后,可以使用简单的查询语句检查数据是否导入成功:
|
||||
```sql
|
||||
SELECT * FROM 表名称 LIMIT 5;
|
||||
```
|
||||
|
||||
### 3. 使用命令直接导入 `fastapi.sql` 文件
|
||||
|
||||
- 对于一些简单情况,可以直接在命令行中使用以下方式导入,无需进入 MySQL 命令行。
|
||||
|
||||
- **通用方法**:
|
||||
```shell
|
||||
mysql -u 用户名 -p 数据库名称 < /path/to/fastapi.sql
|
||||
```
|
||||
这种方法适用于 Linux 和 Windows 系统,路径需根据操作系统设置正确的格式。
|
||||
|
||||
### 注意事项
|
||||
|
||||
- 确保 `fastapi.sql` 文件中的表结构与目标数据库一致,以避免导入错误。
|
||||
- 如果数据库中已有相同数据,请谨慎操作,避免数据冲突或重复。
|
||||
|
||||
---
|
||||
|
||||
## Nginx配置
|
||||
|
||||
### Linux 版本
|
||||
|
||||
1. **安装 Nginx**
|
||||
|
||||
在 Linux 上,使用以下命令安装 Nginx:
|
||||
|
||||
```shell
|
||||
sudo apt update
|
||||
sudo apt install nginx
|
||||
```
|
||||
|
||||
2. **创建 FastAPI 反向代理配置**
|
||||
|
||||
打开或创建 Nginx 配置文件,例如 `/etc/nginx/sites-available/fastapi.conf`:
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name example.com; # 替换为您的域名或服务器 IP
|
||||
|
||||
# 日志路径
|
||||
access_log /var/log/nginx/fastapi_access.log;
|
||||
error_log /var/log/nginx/fastapi_error.log;
|
||||
|
||||
# 文件上传大小限制为 1024MB
|
||||
client_max_body_size 1024M;
|
||||
|
||||
location / {
|
||||
root /var/www/html/; # 替换为您的项目根目录
|
||||
index index.html index.htm;
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
# 如果后端在本地比如127.0.0.1或者localhost请解开下面的rewrite注释即可
|
||||
rewrite ^.+api/?(.*)$ /$1 break;
|
||||
# 这里填写后端地址(后面一定不要忘记添加 / )
|
||||
proxy_pass http://127.0.0.1:9090/;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header Cookie $http_cookie;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_redirect default;
|
||||
add_header Access-Control-Allow-Origin *;
|
||||
add_header Access-Control-Allow-Headers *;
|
||||
add_header Access-Control-Allow-Methods *;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3. **启用配置**
|
||||
|
||||
- 将配置文件链接到 `sites-enabled`:
|
||||
|
||||
```shell
|
||||
sudo ln -s /etc/nginx/sites-available/fastapi.conf /etc/nginx/sites-enabled/
|
||||
```
|
||||
|
||||
- 检查配置文件是否有错误:
|
||||
|
||||
```shell
|
||||
sudo nginx -t
|
||||
```
|
||||
|
||||
- 重启 Nginx 以应用配置:
|
||||
|
||||
```shell
|
||||
sudo systemctl reload nginx
|
||||
```
|
||||
|
||||
4. **验证配置**
|
||||
|
||||
在浏览器中访问 `http://example.com`,确保 FastAPI 应用能够正常访问并支持 1GB 文件上传。
|
||||
|
||||
### Windows 版本
|
||||
|
||||
1. **安装 Nginx**
|
||||
|
||||
- 下载 Nginx Windows 版本:[Nginx 官方网站](https://nginx.org/en/download.html)。
|
||||
- 解压后,将 Nginx 文件夹放置在您选择的目录(例如 `C:\nginx`)。
|
||||
|
||||
2. **创建 FastAPI 配置**
|
||||
|
||||
打开 `C:\nginx\conf\nginx.conf` 文件,在 `http` 块中添加以下配置:
|
||||
|
||||
```nginx
|
||||
http {
|
||||
include mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
# 日志路径
|
||||
access_log logs/fastapi_access.log;
|
||||
error_log logs/fastapi_error.log;
|
||||
|
||||
# 客户端最大文件上传大小限制为 1024MB
|
||||
client_max_body_size 1024M;
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
server_name localhost; # 或替换为您的服务器 IP
|
||||
|
||||
location / {
|
||||
root /var/www/html/; # 替换为您的项目根目录
|
||||
index index.html index.htm;
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
# 如果后端在本地比如127.0.0.1或者localhost请解开下面的rewrite注释即可
|
||||
rewrite ^.+api/?(.*)$ /$1 break;
|
||||
# 这里填写后端地址(后面一定不要忘记添加 / )
|
||||
proxy_pass http://127.0.0.1:9090/;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header Cookie $http_cookie;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_redirect default;
|
||||
add_header Access-Control-Allow-Origin *;
|
||||
add_header Access-Control-Allow-Headers *;
|
||||
add_header Access-Control-Allow-Methods *;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
3. **启动 Nginx**
|
||||
|
||||
- 打开命令提示符,进入 Nginx 目录并启动服务:
|
||||
|
||||
```shell
|
||||
cd C:\nginx
|
||||
nginx.exe
|
||||
```
|
||||
|
||||
4. **验证配置**
|
||||
|
||||
在浏览器中访问 `http://localhost`,检查 FastAPI 应用是否可用并支持 1GB 文件上传。
|
||||
|
||||
### 可选的 SSL 配置
|
||||
|
||||
- 若需要在 Linux 系统上启用 HTTPS 和 SSL 证书,请参考 [Let’s Encrypt](https://letsencrypt.org/) 或其他 SSL 提供商的文档,使用类似以下的配置:
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name example.com;
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 443 ssl;
|
||||
server_name example.com;
|
||||
|
||||
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
|
||||
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
|
||||
|
||||
client_max_body_size 1024M;
|
||||
|
||||
location / {
|
||||
root /var/www/html/; # 替换为您的项目根目录
|
||||
index index.html index.htm;
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
location /api/ {
|
||||
# 如果后端在本地比如127.0.0.1或者localhost请解开下面的rewrite注释即可
|
||||
rewrite ^.+api/?(.*)$ /$1 break;
|
||||
# 这里填写后端地址(后面一定不要忘记添加 / )
|
||||
proxy_pass http://127.0.0.1:9090/;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header Cookie $http_cookie;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_redirect default;
|
||||
add_header Access-Control-Allow-Origin *;
|
||||
add_header Access-Control-Allow-Headers *;
|
||||
add_header Access-Control-Allow-Methods *;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
---
|
||||
|
||||
## 启动运行
|
||||
|
||||
### 1. 进入项目目录并激活虚拟环境
|
||||
|
||||
#### Linux
|
||||
|
||||
```shell
|
||||
# 进入项目目录
|
||||
cd /path/to/your/project
|
||||
|
||||
# 激活虚拟环境
|
||||
source venv/bin/activate
|
||||
```
|
||||
|
||||
#### Windows
|
||||
|
||||
```shell
|
||||
# 进入项目目录
|
||||
cd \path\to\your\project
|
||||
|
||||
# 激活虚拟环境
|
||||
.\venv\Scripts\activate
|
||||
```
|
||||
|
||||
### 2. 启动 FastAPI 项目
|
||||
|
||||
```shell
|
||||
python app.py
|
||||
```
|
||||
|
||||
### 3. 停止项目
|
||||
|
||||
在命令行中按下 `Ctrl + C` 以停止运行中的 FastAPI 项目。
|
||||
|
||||
### 备注
|
||||
|
||||
- 运行项目时,请确保虚拟环境已正确激活,以避免 Python 库路径错误。
|
||||
- 如果 Nginx 已配置为反向代理,则可以直接访问 Nginx 设置的域名或 IP。
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user