Skip to content

Deploy a Rust App

How to Deploy a Rust App Anywhere with HTTPS and a custom domain

In this guide, we will deploy a simple Rust web app on a VPS (or a cloud server). It doesn't matter which cloud provider you use; the steps remain largely the same with one exception: some cloud providers, like AWS, block all ports by default, and you must enable them manually. If you just want to get your Rust app online as soon as possible, we recommend using Hetzner or Digital Ocean. This guide has been tested with both cloud providers.

Setting Up the Server

Create a new server on your cloud provider or local machine with a public IP address, SSH access from your development machine, and a fresh Ubuntu 22.04 installation. Ensure that ports 22, 80, and 443 are open. (Some cloud providers, like AWS, close ports by default; you’ll need to open them before deployment.)

If you’re looking for a new cloud provider, consider Hetzner, DigitalOcean, or OVH. These providers typically have ports open by default. OVH also offers affordable domain registration and free DNS management via API.

Deploy with One Command

If you don’t want to deploy manually or don’t have time to set up a deployment pipeline, you can deploy your Rust application to any cloud provider or private server with a single command from your local repository using TurboCloud. (No need for GitHub, Bitbucket, or even Git!) You can also deploy directly from GitHub or Bitbucket repositories.

Prepare Your Go App for Deployment

If you deploy with TurboCloud, it will check for the presence of a Dockerfile in the project's root directory. If no Dockerfile is found, TurboCloud will attempt to generate one automatically using Nixpacks. (See here for details on how Nixpacks generates Dockerfiles for Rust apps.) However, we recommend creating a Dockerfile manually and placing it in the project's root folder for better control:

  • Create a file named Dockerfile in your project’s root directory.
  • Add the following content:
FROM rust:1.67 as builder
WORKDIR /usr/src/myapp
COPY . .
RUN cargo install --path .

FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y extra-runtime-dependencies && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/cargo/bin/myapp /usr/local/bin/myapp
CMD ["myapp"]

Deploy from the Local Repository

  • On your development machine, navigate to the folder containing the Rust app code and run the deployment command (replace server_ip with the actual public IP of your server and service_port with the port number your app uses):
cd my_project
curl https://turbocloud.dev/deploy | bash -s -- -i server_ip -p service_port
  • Wait until the deployment script displays the URL of the deployed Rust application.
  • Full documentation for all available parameters and detailed instructions can be found at turbocloud.dev/docs.

Deploy from GitHub or Bitbucket

  • SSH into your server (replace server_ip with the actual public IP of your server):
ssh root@server_ip
  • Run the setup command:
curl https://turbocloud.dev/setup | bash -s
  • Once installation is complete, start the TurboCloud TUI (Terminal User Interface), which functions similarly to standard applications on macOS, Linux, and other operating systems but can operate on servers without displays. Use the TUI to set up deployments with interactive guides:
turbocloud

Step-by-Step guide

Why Use Docker, and Caddy?

  • Docker packages your application with its dependencies, ensuring consistency across environments.
  • Caddy simplifies HTTPS configuration and automatically provisions SSL certificates for your domain.

By combining these tools, you get a streamlined deployment process with a secure, reliable setup.

Prerequisites

Before you begin, ensure the following:

  • A VPS: A virtual private server with SSH access and a public IP address.
  • A Custom Domain: Registered and pointing to your VPS's IP address.
  • Docker Installed on Your Local Machine: Follow the official Docker installation guide.
  • Rust Installed on Your Local Machine: Use rustup to install Rust on your development machine.

Step 1: Create a Simple Rust Web App

We’ll start by writing a simple Rust web application using the Actix Web framework.

1.1 Create a New Rust Project

Run the following command to create a new project:

cargo new rust-app
cd rust-app

1.2 Add Dependencies

Edit Cargo.toml to include actix-web:

[dependencies]
actix-web = "4.0"

1.3 Write the Web Server Code

Replace the contents of src/main.rs with the following:

use actix_web::{web, App, HttpServer, Responder};

async fn greet() -> impl Responder {
    "Hello, world!"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(greet))
    })
    .bind("0.0.0.0:8080")?
    .run()
    .await
}

This simple application responds with Hello, world! to requests at the root path.

1.4 Build and Test Locally

Run the application locally to ensure it works:

cargo run

Visit http://localhost:8080 in your browser to see the output.


Step 2: Containerize the Application with Docker

Docker makes it easy to package your Rust application and deploy it consistently.

2.1 Create a Dockerfile

In your project directory, create a Dockerfile:

FROM rust:1.67 as builder
WORKDIR /usr/src/myapp
COPY . .
RUN cargo install --path .

FROM debian:bullseye-slim
RUN apt-get update && apt-get install -y extra-runtime-dependencies && rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/cargo/bin/myapp /usr/local/bin/myapp
CMD ["myapp"]

2.2 Build the Docker Image

Run the following command to build the image:

docker build -t rust-app .

2.3 Test the Dockerized App Locally

Run the container locally to ensure it works:

docker run -p 8080:8080 rust-app

Visit http://localhost:8080 to confirm it’s working.


Step 3: Deploy to the VPS

3.1 Transfer the Docker Image

You can transfer the Docker image to your VPS using docker save and scp:

On your local machine:

docker save rust-app | gzip > rust-app.tar.gz
scp rust-app.tar.gz user@your-vps-ip:/home/user

On your VPS:

gunzip rust-app.tar.gz
docker load < rust-app.tar

Alternatively, you can use a Docker registry like Docker Hub to push and pull images.

3.2 Run the Container on the VPS

Run the Docker container on your VPS:

docker run -d --name rust-app -p 8080:8080 rust-app

Your app should now be accessible at http://your-vps-ip:8080.


Step 4: Configure Caddy for HTTPS and a Custom Domain

Caddy simplifies HTTPS and domain configuration with minimal setup.

4.1 Install Caddy

If you haven’t installed Caddy, use Docker:

docker run -d -p 80:80 -p 443:443 --name caddy -v /path/to/Caddyfile:/etc/caddy/Caddyfile caddy

4.2 Create a Caddyfile

Create a Caddyfile to configure your domain and proxy:

yourdomain.com {
    reverse_proxy 127.0.0.1:8080
}

4.3 Start Caddy

Ensure Caddy is running and reads the Caddyfile:

docker restart caddy

Caddy will automatically generate an SSL certificate for your domain and configure HTTPS.


Step 5: Test Your Deployment

Wait a few minutes after you restart Caddy and visit your custom domain in the browser. You should see Hello, world! served securely over HTTPS.


Conclusion

In this guide, we’ve covered two options for deploying Rust applications:

  1. Using the TurboCloud deployment toolkit with a single command. This option also includes CI/CD, multiple environments, a Web Application Firewall (WAF), a Rate Limiter, and VPN functionality across servers and local machines.
  2. Deploying manually with Docker, Docker Compose, and Caddy. To prepare your server for production, we recommend adding a Web Application Firewall (WAF) and a Rate Limiter.