利用systemd启动部署在服务器上的web应用

0.背景

系统环境: Ubuntu 22.04
web应用情况: 前后端分类,前端采用react,后端采用fastapi

1.具体配置

1.1 前端配置

开发态运行(启动命令是npm run dev),创建systemd服务文件

sudo nano /etc/systemd/system/frontend.service

内容如下:

[Unit]
Description=React Frontend Dev Server
After=network.target

[Service]
User=youruser
WorkingDirectory=/home/yourusr/yourweb/frontend
ExecStart=/usr/bin/npm run dev
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

如果你使用了nvm来管理Node.js,那么你应该做如下修改

[Unit]
Description=React Frontend Dev Server
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/home/yourusr/yourweb/frontend
Environment="NVM_DIR=/root/.nvm"
ExecStart=/bin/bash -c 'source $NVM_DIR/nvm.sh && nvm use 22.15.0 && npm run dev'
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

1.2后端配置

编写fastapi启动脚本,在/home/youruser/backend/start_backend.sh写入:

#!/bin/bash
cd /home/yourweb/backend
source venv/bin/activate
exec uvicorn server:app --host 0.0.0.0 --port 8000 --workers 1

如果使用conda管理环境,脚本文件的内容写入如下:

#!/bin/bash

cd /home/yourweb/backend
# 加载 conda 环境(确保 conda 可用)
source /home/youruser/miniconda3/etc/profile.d/conda.sh

# 激活 conda 虚拟环境
conda activate myenv

# 启动 Uvicorn
exec uvicorn server:app --host 0.0.0.0 --port 8000 --workers 1

Tip: 把 /home/youruser/miniconda3 替换为你实际的 conda 安装路径(可用 which conda 看一下)

然后授予可执行权限

chmod +x /home/yourweb/backend/start_backend.sh

创建systemd服务文件

sudo nano /etc/systemd/system/backend.service

内容如下:

[Unit]
Description=FastAPI Backend Service
After=network.target

[Service]
Type=simple
User=youruser
ExecStart=/home/youruser/yourweb/backend/start_backend.sh
WorkingDirectory=/home/youruser/yourweb/backend
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

2.启停管理

2.1 前端服务启停

命令如下:

sudo systemctl daemon-reload
sudo systemctl start frontend
sudo systemctl enable frontend
sudo systemctl status frontend #查看运行状态

2.2 后端服务启停

命令如下:

sudo systemctl daemon-reload
sudo systemctl start backend
sudo systemctl enable backend
sudo systemctl status backend # 查看运行状态
基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统,个人经导师指导并认可通过的高分设计项目,评审分98分,项目中的源码都是经过本地编译过可运行的,都经过严格调试,确保可以运行!主要针对计算机相关专业的正在大作业、毕业设计的学生和需要项目实战练习的学习者,资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经网络的果蔬识别系统基于python tensorflow2.3的果蔬识别系统源码+模型-基于卷积神经
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不是吧这都有重名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值