Introduction: Why Rust is ideal for production
Rust is not just a trendy programming language. Its main advantages are memory safety, high performance, and predictable behavior. When you write in Rust, you get a binary file that can be run on any Linux server without installing a virtual machine or interpreter. This makes deploying Rust applications on VPS incredibly simple and efficient.
In this article, we will break down the entire process: from building the project to setting up a production environment with Nginx and systemd. You will learn how to properly compile the application, configure auto-start, and ensure security.
Preparing the VPS and environment
Choosing and setting up the server
Any VPS with Linux (Ubuntu 22.04 LTS is the optimal choice) is suitable for deploying a Rust application. Minimum requirements: 1 vCPU, 1 GB RAM, 20 GB SSD. This is enough for most web services in Rust.
First, connect to the server via SSH:
ssh root@your-ip-address
Update packages and install necessary tools:
apt update && apt upgrade -y
apt install curl git build-essential pkg-config libssl-dev -y
Installing Rust on the server
Although we will build the binary locally, it is sometimes convenient to compile directly on the server. Install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
Verify the installation:
rustc --version
cargo --version
Building the Rust application for production
Optimizing the build
For production, you need to build the application with the --release flag. This enables optimizations that reduce the binary size and speed up its operation. Add the following settings to Cargo.toml:
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
strip = true
- opt-level = 3 — maximum speed optimization
- lto = true — Link Time Optimization, reduces size and speeds up
- codegen-units = 1 — increases build time but improves optimization
- strip = true — removes debug information, reducing the binary by 30-50%
Building the binary
Run the command on your local machine (or CI server):
cargo build --release
The finished binary is located in target/release/project-name. Its size should be small — from 2 to 10 MB for a typical web application.
Copying to the server
Use scp or rsync to transfer the file:
scp target/release/your-application root@your-ip-address:/opt/app/
Create the /opt/app directory on the server in advance:
mkdir -p /opt/app
mkdir -p /opt/app/static # if there are static files
Setting up systemd for auto-start
Creating the service
Systemd is the standard service manager in Linux. It will automatically start your application after a server reboot and restart it in case of a crash.
Create the service file:
nano /etc/systemd/system/rust-app.service
Add the configuration:
[Unit]
Description=Rust Web Application
After=network.target
[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=/opt/app
ExecStart=/opt/app/your-application
Restart=always
RestartSec=5
Environment=RUST_LOG=info
Environment=DATABASE_URL=postgres://user:pass@localhost/dbname
[Install]
WantedBy=multi-user.target
Important parameters:
- User — run as an unprivileged user
www-data - Restart=always — automatic restart