简述:基于Ubuntu24编译发布rust开发的html+web后端服务的方法

生成运行包后启动web后端服务方法:
bash
控制台运行,关闭控制台停止运行
exec ./cw_elyb
后端运行,关闭控制台不影响运行
exec ./run.sh
已有window上的rust开发的html+web后端服务的源代码
利用AI生成在Ubuntu24操作系统上打包编译的脚本
执行编译打包脚本
bash
cd cbdyb_web
bash scripts/package-ubuntu.sh
具体方法:
在 Ubuntu 24 上使用
bash
# 把项目拷到 Ubuntu 后
cd cbdyb_web
bash scripts/package-ubuntu.sh
# 得到 dist/cw_elyb-0.1.0-ubuntu24.tar.gz
# 部署
sudo tar -xzf dist/cw_elyb-0.1.0-ubuntu24.tar.gz -C /opt
sudo cp /opt/cw_elyb-0.1.0-ubuntu24/cw-elyb.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now cw-elyb
# 浏览器打开 http://<服务器IP>:8080/
package-ubuntu.sh文件内容:
bash
#!/usr/bin/env bash
# Ubuntu 24 打包脚本:在 Ubuntu 上构建 release 并生成可部署的 tar.gz
# 用法:在项目根目录执行 bash scripts/package-ubuntu.sh
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$ROOT"
NAME="cw_elyb"
VER="$(grep -m1 '^version' Cargo.toml | sed 's/.*"\(.*\)"/\1/')"
OUT="dist/${NAME}-${VER}-ubuntu24"
TARBALL="dist/${NAME}-${VER}-ubuntu24.tar.gz"
CARGO_BIN="$HOME/.cargo/bin"
echo "==> 检查依赖"
need() { command -v "$1" >/dev/null 2>&1 || { echo "缺少 $1,正在安装..."; sudo apt-get update -qq && sudo apt-get install -y -qq "$2"; }; }
need curl curl
need build-essential build-essential
need pkg-config pkg-config
export PATH="$CARGO_BIN:$PATH"
echo "==> 准备 Rust 工具链"
# 1) 没有 rustup 就装
if ! command -v rustup >/dev/null 2>&1 && [ ! -x "$CARGO_BIN/rustup" ]; then
echo " 未找到 rustup,安装中..."
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable
fi
export PATH="$CARGO_BIN:$PATH"
# 2) 探测 rustc;失败则强制重装 stable
RUSTUP="$CARGO_BIN/rustup"
[ -x "$RUSTUP" ] || RUSTUP="$(command -v rustup || true)"
if ! rustc -vV >/dev/null 2>&1; then
echo " rustc 不可用(工具链损坏),强制重装 stable..."
if [ -n "$RUSTUP" ]; then
"$RUSTUP" toolchain uninstall stable 2>/dev/null || true
"$RUSTUP" toolchain install stable --profile minimal
"$RUSTUP" default stable
else
echo "错误:找不到 rustup,请先执行 curl https://sh.rustup.rs -sSf | sh -s -- -y"
exit 1
fi
else
echo " 工具链正常,可选更新..."
[ -n "$RUSTUP" ] && "$RUSTUP" update stable >/dev/null 2>&1 || true
fi
export PATH="$CARGO_BIN:$PATH"
echo "==> rustc: $(rustc -vV | head -1)"
echo "==> cargo: $(cargo -V)"
echo "==> cargo build --release"
cargo build --release
echo "==> 组装发布目录 $OUT"
rm -rf "$OUT"
mkdir -p "$OUT"
cp target/release/${NAME} "$OUT/"
cp -r static "$OUT/static"
cp config.json "$OUT/"
cp README.md "$OUT/"
cp scripts/run.sh "$OUT/"
cp scripts/cw-elyb.service "$OUT/"
chmod +x "$OUT/${NAME}" "$OUT/run.sh"
echo "==> 打包 $TARBALL"
tar -czf "$TARBALL" -C dist "$(basename "$OUT")"
echo
echo "完成:"
echo " 目录 $ROOT/$OUT"
echo " 压缩包 $ROOT/$TARBALL"
echo
echo "部署到 Ubuntu 24:"
echo " sudo tar -xzf $TARBALL -C /opt"
echo " # 方式 A:systemd 常驻(推荐)"
echo " sudo cp /opt/$(basename "$OUT")/cw-elyb.service /etc/systemd/system/"
echo " sudo systemctl daemon-reload && sudo systemctl enable --now cw-elyb"
echo " # 方式 B:脚本后台启动"
echo " cd /opt/$(basename "$OUT") && ./run.sh # 后台启动"
echo " ./run.sh status / stop / restart"
echo " # 浏览器打开 http://<服务器IP>:8080/"
在生成dict中 解压tar包
把在ubuntu操作系统上exec ./cw_elyb 改为在后台执行的命令
run.sh文件内容:
bash
#!/usr/bin/env bash
# CW GIS 二轮延包管理系统 启动/停止脚本(后台运行)
# 用法(在解压后的目录内执行):
# ./run.sh # 后台启动(默认)
# ./run.sh start # 后台启动
# ./run.sh stop # 停止
# ./run.sh restart # 重启
# ./run.sh status # 查看状态
# ./run.sh fg # 前台运行(调试用)
set -euo pipefail
DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$DIR"
BIN="./cw_elyb"
PID_FILE="./cw_elyb.pid"
LOG_FILE="./cw_elyb.log"
export RUST_LOG="${RUST_LOG:-info}"
is_running() {
[ -f "$PID_FILE" ] && kill -0 "$(cat "$PID_FILE")" 2>/dev/null
}
do_start() {
if is_running; then
echo "已在运行 (pid $(cat "$PID_FILE"))"
return 0
fi
echo "启动中... 日志:$DIR/cw_elyb.log"
nohup "$BIN" >>"$LOG_FILE" 2>&1 &
echo $! >"$PID_FILE"
sleep 0.5
if is_running; then
echo "已后台启动 (pid $(cat "$PID_FILE"))"
else
echo "启动失败,请查看 $LOG_FILE"
rm -f "$PID_FILE"
return 1
fi
}
do_stop() {
if ! is_running; then
echo "未在运行"
rm -f "$PID_FILE"
return 0
fi
local pid
pid="$(cat "$PID_FILE")"
echo "停止 pid $pid ..."
kill "$pid" 2>/dev/null || true
for _ in 1 2 3 4 5 6 7 8 9 10; do
kill -0 "$pid" 2>/dev/null || break
sleep 0.3
done
kill -9 "$pid" 2>/dev/null || true
rm -f "$PID_FILE"
echo "已停止"
}
do_status() {
if is_running; then
echo "运行中 (pid $(cat "$PID_FILE"))"
else
echo "未运行"
fi
}
case "${1:-start}" in
start) do_start ;;
stop) do_stop ;;
restart) do_stop; do_start ;;
status) do_status ;;
fg) exec "$BIN" ;;
*) echo "用法: $0 {start|stop|restart|status|fg}"; exit 1 ;;
esac
如果在Ubuntu上编译报错时修复环境安装包:
先在 Ubuntu 上手动修工具链(可立即执行)
bash
export PATH="$HOME/.cargo/bin:$PATH"
rustup toolchain uninstall stable 2>/dev/null || true
rustup toolchain install stable --profile minimal
rustup default stable
rustc -vV # 必须能打印 version,否则不要往下走
再跑打包
bash
cd /home/cbdyb_web
# 确认是新脚本:应能看到 "==> 准备 Rust 工具链"
head -30 scripts/package-ubuntu.sh
bash scripts/package-ubuntu.sh
最后生成tar包
本blog地址:https://blog.csdn.net/hsg77