2024-12-31 10:03:07 +08:00
|
|
|
1.进入虚拟环境
|
|
|
|
source .venv/bin/activate
|
2025-01-17 14:08:48 +08:00
|
|
|
|
|
|
|
安装fastapi
|
|
|
|
|
2024-12-31 10:03:07 +08:00
|
|
|
2.运行fastapi
|
|
|
|
uvicorn main:app --reload --host 0.0.0.0 --port 8000
|
|
|
|
|
|
|
|
后台运行
|
|
|
|
nohup uvicorn main:app --host 0.0.0.0 --port 8000 --log-level info > ../log/app.log 2>&1 &
|
|
|
|
|
|
|
|
|
|
|
|
//待定(还有问题)
|
|
|
|
3.服务脚本
|
|
|
|
sudo nano /etc/systemd/system/fastapi.service
|
|
|
|
'''
|
|
|
|
[Unit]
|
|
|
|
Description=FastAPI Application
|
|
|
|
After=network.target
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
User=huahua # 设置为运行应用的用户
|
|
|
|
WorkingDirectory=/home/huahua/intr/HostMain/code
|
|
|
|
ExecStart=/home/huahua/intr/HostMain/.venv/bin/uvicorn main:app --host 0.0.0.0 --port 8000 --log-level info
|
|
|
|
Restart=always
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
4.运行服务
|
|
|
|
sudo systemctl daemon-reload
|
|
|
|
sudo systemctl start fastapi.service
|
|
|
|
sudo systemctl enable fastapi.service # 设置为开机自启
|
|
|
|
|
|
|
|
sudo systemctl stop fastapi.service
|
|
|
|
sudo journalctl -u fastapi.service -f # 实时查看日志
|
|
|
|
|
|
|
|
sudo systemctl disable fastapi.service # 设置为开机自启
|