Deploying a Rust Application on VPS: Step-by-Step from A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction

\n\n

You've written a cool application in Rust. It's fast, safe, and doesn't crash. But how do you make it run 24/7 on a remote server and be accessible to users? Deploying a Rust application on a VPS (Virtual Private Server) is a process that includes compiling the binary, setting up the environment, running it as a system service, and often setting up a reverse proxy.

\n\n

In this article, we will go through the entire path from zero to a fully working Rust application deployed on a VPS with Ubuntu. You will learn how to prepare the server, build the project, set up auto-start, and ensure security. This guide is suitable for both web servers (Actix-web, Axum, Rocket) and any other Rust daemons.

\n\n

Let's get started.

\n\n

1. Server and Environment Preparation

\n\n

Before deploying the code, you need to prepare the "ground". We will use Ubuntu 22.04 LTS, but the instructions are also suitable for Debian.

\n\n

Connecting to the Server and Basic Setup

\n\n

Connect to your VPS via SSH:

\n\n
ssh root@your-server-ip
\n\n

Update packages and create a user (working as root is bad practice):

\n\n
apt update && apt upgrade -y\nadduser deployer\nusermod -aG sudo deployer
\n\n

Configure SSH keys for the new user so you don't have to enter a password every time.

\n\n

Installing Rust on the Server

\n\n

The most reliable way is to use the official rustup script. Switch to the deployer user and run:

\n\n
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
\n\n

After installation, reload the shell or run:

\n\n
source $HOME/.cargo/env
\n\n

Check the version:

\n\n
rustc --version
\n\n

For a production build, we will need static linking (so the binary works on any Linux without extra libraries). Install musl-tools:

\n\n
sudo apt install musl-tools -y\nrustup target add x86_64-unknown-linux-musl
\n\n

2. Building and Transferring the Binary to the Server

\n\n

It's better to build the application on your local machine (or in CI/CD) and then copy the ready binary to the server. This is faster and doesn't load the VPS.

\n\n

Building the Production Version

\n\n

On your local computer, in the project folder, run:

\n\n
cargo build --release --target x86_64-unknown-linux-musl
\n\n

This will create a statically linked binary with no dependencies on glibc. The size will be larger, but it will run on any Linux.

\n\n

Example of a simple web server using Actix-web for testing (file src/main.rs):

\n\n
use actix_web::{web, App, HttpServer, Responder};\n\nasync fn hello() -> impl Responder {\n    "Hello from Rust on VPS!"\n}\n\n#[actix_web::main]\nasync fn main() -> std::io::Result<()> {\n    HttpServer::new(|| {\n        App::new().route("/", web::get().to(hello))\n    })\n    .bind("0.0.0.0:8080")?\n    .run()\n    .await\n}
\n\n

Copying to the Server

\n\n

Use scp for secure copying:

\n\n
scp ./target/x86_64-unknown-linux-musl/release/your_project deployer@your-server-ip:/home/deployer/
\n\n

On the server, move the binary to /usr/local/bin:

\n\n
sudo mv /home/deployer/your_project /usr/local/bin/my-rust-app\nsudo chmod +x /usr/local/bin/my-rust-app
\n\n

3. Setting up systemd for Auto-start

\n\n

To make the application start on system boot, restart after a crash, and run in the background, we will create a systemd service.

\n\n

Create the service file:

\n\n
sudo nano /etc/systemd/system/rust-app.service
\n\n

Paste the following config (replace deployer with your user):

\n\n
[Unit]\nDescription=Rust Application\nAfter=network.target\n\n[Service]\nType=simple\nUse                

Blogs

Book Recommendations