在CentOS服务器上,首先需要安装Rust编译器及依赖工具。推荐使用rustup(Rust官方工具链管理器)进行安装:
# 下载并运行rustup安装脚本
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 按提示完成安装(选择默认配置)
# 重新加载shell环境,使rustc和cargo命令生效
source $HOME/.cargo/env
# 验证安装版本
rustc --version # 应输出Rust编译器版本(如1.75.0)
cargo --version # 应输出Cargo包管理器版本
确保安装成功后,再继续后续步骤。
进入Rust项目根目录(包含Cargo.toml文件的目录),执行以下命令构建发布版本(优化后的版本,性能更好):
# 构建release版本,生成的可执行文件位于target/release目录下
cargo build --release
若项目依赖系统库(如OpenSSL),需提前安装对应开发包(以CentOS为例):
sudo yum install openssl-devel gcc make # 安装OpenSSL开发依赖
构建完成后,可通过./target/release/your_app_name(替换为你的可执行文件名)测试运行。
若希望可执行文件在任意CentOS系统上运行(无需担心系统库版本冲突),建议进行静态编译。常见方法有两种:
glibc-static静态链接(性能更优)sudo yum install glibc-static libstdc++-static # CentOS静态库包
.cargo/config.toml文件,添加以下内容:[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "target-feature=+crt-static"]
cargo build --release
ldd ./target/release/your_app_name # 若输出“not a dynamic executable”,则说明静态编译成功
musl工具链(兼容性更强)若glibc-static无法满足需求(如部分库不支持静态链接),可使用musl(轻量级C库)进行全静态编译:
musl工具链:rustup target add x86_64-unknown-linux-musl # 添加musl目标
cargo build --release --target x86_64-unknown-linux-musl
ldd命令应显示“not a dynamic executable”。将构建好的可执行文件上传至CentOS服务器。若本地与服务器在同一网络,可使用scp命令:
# 本地(替换为你的项目路径和文件名)
scp ./target/release/your_app_name user@your_server_ip:/path/to/deploy
# 登录服务器
ssh user@your_server_ip
# 赋予执行权限
chmod +x /path/to/deploy/your_app_name
为确保Rust应用在服务器重启后自动启动,并在后台稳定运行,建议使用systemd创建系统服务:
sudo nano /etc/systemd/system/your_app.service # 替换为你的服务名
User、ExecStart路径):[Unit]
Description=Your Rust Application
After=network.target # 确保网络就绪后再启动
[Service]
User=your_username # 运行服务的用户(如ubuntu、centos)
Group=your_groupname # 运行服务的组
ExecStart=/path/to/deploy/your_app_name # 可执行文件绝对路径
Restart=always # 崩溃后自动重启
RestartSec=3 # 重启间隔3秒
Environment="ENV_VAR=value" # 可选:设置环境变量(如数据库连接串)
[Install]
WantedBy=multi-user.target # 设置为多用户模式启动
sudo systemctl daemon-reload # 重载配置
sudo systemctl start your_app # 启动服务
sudo systemctl enable your_app # 设置开机自启
sudo systemctl status your_app # 查看运行状态(若显示“active (running)”则表示成功)
sudo journalctl -u your_app -f # 实时查看应用日志(用于排查问题)
若应用需要通过网络访问(如Web服务),需配置CentOS防火墙(firewalld)开放对应端口:
# 允许HTTP端口(80)
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
# 允许HTTPS端口(443)
sudo firewall-cmd --zone=public --add-port=443/tcp --permanent
# 重新加载防火墙规则
sudo firewall-cmd --reload
若应用直接暴露在公网(如80/443端口),建议通过Nginx反向代理,隐藏真实端口并支持HTTPS:
sudo yum install nginx # CentOS 7/8均适用
sudo systemctl start nginx
sudo systemctl enable nginx
/etc/nginx/conf.d/your_app.conf中添加以下内容(替换your_domain.com为你的域名):server {
listen 80;
server_name your_domain.com; # 替换为你的域名
location / {
proxy_pass http://localhost:8080; # 转发到Rust应用的端口(需与ExecStart中的端口一致)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
sudo systemctl reload nginx
Cargo.toml的[profile.release]中添加strip = true,自动移除可执行文件中的调试信息(减小体积)。Cargo.toml中配置opt-level = "z"(体积优化)、lto = true(链接时优化),提升运行性能。upx工具压缩可执行文件(减小体积,但会增加首次启动时间):sudo yum install upx # 安装upx
upx --best /path/to/deploy/your_app_name # 压缩(--best表示最高压缩率)
通过以上步骤,即可在CentOS服务器上完成Rust项目的部署上线,并确保其稳定运行。