安装Rust工具链
使用rustup安装(推荐):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env # 激活环境变量
或通过包管理器安装(如Ubuntu):
sudo apt install cargo
构建Rust项目
在项目目录执行:
cargo build --release # 生成优化后的可执行文件,位于target/release/
部署到服务器
使用scp/rsync上传可执行文件到目标服务器:
scp target/release/your_app user@server:/path/to/deploy
设置运行环境
chmod +x /path/to/deploy/your_app
运行应用
/path/to/deploy/your_app
nohup /path/to/deploy/your_app &
使用systemd管理(可选)
创建服务文件/etc/systemd/system/your_app.service:
[Unit]
Description=Your Rust App
After=network.target
[Service]
ExecStart=/path/to/deploy/your_app
Restart=always
User=your_user
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl daemon-reload
sudo systemctl start your_app
sudo systemctl enable your_app
验证与维护
journalctl -u your_app(若使用systemd)。