Introduction
Rust is a systems programming language known for its performance and memory safety. However, writing an application is only half the battle. To make your project accessible to users on the internet, it needs to be deployed on a server. In this article, we will walk through the step-by-step process of deploying a Rust application on a VPS (Virtual Private Server).
We will use a typical web server based on the actix-web framework, set up the environment, compile a release build, run the application as a systemd service, and configure an Nginx reverse proxy with an SSL certificate. By following this guide, you will be able to deploy any Rust application on a remote server running Linux.
1. Server and Environment Preparation
1.1. Connecting to the Server
First, connect to your VPS via SSH:
ssh root@your_server_ipIt is recommended to create a separate user with limited privileges (sudo) for security, but for simplicity, we will work as root in this guide.
1.2. Installing Rust on the Server
Although we could compile the application locally and simply copy the binary, it is more convenient to install Rust on the server for possible recompilation for the target architecture. Use the official script:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shsource $HOME/.cargo/envVerify the installation:
rustc --versioncargo --version1.3. Installing System Dependencies
Building some crates (e.g., openssl) may require system libraries. Install them:
apt updateapt install build-essential pkg-config libssl-dev git -y2. Building and Preparing the Application
2.1. Example of a Simple Web Server with actix-web
Let's create a minimal application. If you already have a project, skip this step.
Cargo.toml:
[package]name = "my_rust_app"version = "0.1.0"edition = "2021"
[dependencies]actix-web = "4"serde = { version = "1", features = ["derive"] }src/main.rs:
use actix_web::{get, web, App, HttpServer, Responder};
#[get("/")]async fn index() -> impl Responder { "Hello from Rust on VPS!"}
#[actix_web::main]async fn main() -> std::io::Result<()> { println!("Starting server at 0.0.0.0:8080"); HttpServer::new(|| { App::new() .service(index) }) .bind("0.0.0.0:8080")? .run() .await}2.2. Compiling in Release Mode
Build the application on your local machine or directly on the server. For maximum performance, use the --release flag:
cargo build --releaseThe resulting binary will be located at target/release/my_rust_app (the name matches the package name).
2.3. Copying the Binary to the Server (if built locally)
If you built the application on your local machine, copy it via SCP:
scp target/release/my_rust_app root@your_server_ip:/opt/my_rust_app/Create the /opt/my_rust_app directory on the server in advance:
mkdir -p /opt/my_rust_app3. Configuring Startup via systemd
To ensure the application starts automatically on system boot and restarts in case of failure, we will create a systemd service.
3.1. Creating the Unit File
Create the file /etc/systemd/system/my_rust_app.service:
[Unit]Description=My Rust Web ApplicationAfter=network.target
[Service]Type=simpleUser=rootWorkingDirectory=/opt/my_rust_appExecStart=/opt/my_rust_app/my_rust_appRestart=on-failureRestartSec=10
[Install]WantedBy=multi-user.target