Skip to content

Deploying an Actix App

Actix is a powerful, pragmatic, and extremely fast web framework for Rust. It is built on the Actix actor framework and is known for its high performance, flexibility, and strong type safety, making it ideal for building robust web services and APIs in Rust.

This guide explains how to deploy an Actix application to Klutch.sh, both with and without a Dockerfile. It also covers installation and provides sample code to get started.

Prerequisites

  • Rust (latest stable)
  • Cargo installed
  • Git and GitHub account
  • Klutch.sh account

Getting Started: Install Actix

  1. Create a new Actix app:
    Terminal window
    cargo new my-actix-app
    cd my-actix-app
  2. Add Actix-web to your Cargo.toml:
    [dependencies]
    actix-web = "4"
  3. Create a basic Actix app (src/main.rs):
    use actix_web::{get, App, HttpServer, Responder};
    use std::env;
    #[get("/")]
    async fn index() -> impl Responder {
    "Hello from Actix on Klutch.sh!"
    }
    #[actix_web::main]
    async fn main() -> std::io::Result<()> {
    let port = env::var("PORT").unwrap_or("8000".to_string());
    HttpServer::new(|| App::new().service(index))
    .bind(("0.0.0.0", port.parse().unwrap()))?
    .run()
    .await
    }
  4. Test locally:
    Terminal window
    cargo run
    Visit http://localhost:8000 to see your app running.

Deploying Without a Dockerfile

  1. Push your Actix app to a GitHub repository.
  2. Log in to Klutch.sh.
  3. Create a new project and give it a name.
  4. Create a new app:
    • Select your Actix GitHub repository and branch
    • Set the port to route traffic (usually 8000 for Actix)
    • Choose region, compute, number of instances, and add any environment variables
  5. Add a start command in your app settings:
    Terminal window
    cargo run --release
  6. Click “Create” to deploy. Klutch.sh will build and deploy your app automatically.

Deploying With a Dockerfile

  1. Add a Dockerfile to your project root. Example:
    # Use official Rust image for build
    FROM rust:latest AS builder
    WORKDIR /app
    COPY . .
    RUN cargo build --release
    # Use minimal image for running
    FROM debian:bullseye-slim
    WORKDIR /app
    COPY --from=builder /app/target/release/my-actix-app .
    # Expose port (match your Actix app)
    EXPOSE 8000
    # Start the app
    CMD ["./my-actix-app"]
  2. Push your code (with Dockerfile) to GitHub.
  3. In Klutch.sh, follow the same steps to create a project and app, but select the Dockerfile option when prompted.
  4. Set the service details and environment variables as needed.
  5. Click “Create” to deploy. Klutch.sh will build your Docker image and deploy your app.

Note: Your Actix app should always listen on the PORT environment variable as shown above.


Resources


Deploying to Klutch.sh is simple and flexible. Choose the method that best fits your workflow and project requirements.