Linux自启动fastapi服务
步骤一
在/etc/systemd/system/文件夹下创建pyod.service(其中/path/to/conda/bin/activate要改为activate实际存放位置,例如miniconda的实际存放位置为/root/miniconda3/bin/activate)
[Unit]
Description=PyOD Uvicorn Service
After=network.target
[Service]
Type=simple
User=root # 使用root用户运行服务(可根据需要修改为其他用户)
Group=root # 使用root用户组运行服务(可根据需要修改为其他用户组)
ExecStart=/bin/bash -c 'source /path/to/conda/bin/activate pyod && cd /data/pyod && uvicorn main:app --host 0.0.0.0 --port 8000 --reload'
Restart=always
RestartSec=5s
[Install]
WantedBy=multi-user.target
步骤二
保存文件后,重新加载systemd守护进程:
sudo systemctl daemon-reload
开机自启服务:
sudo systemctl enable pyod.service
运行服务:
sudo systemctl start pyod.service
检查服务状态:
sudo systemctl status pyod.service
步骤三
fastapi服务访问成功
curl http://127.0.0.1:8000