Deploy a Rust App
How to Deploy a Rust Application on a VPS, Cloud, or Dedicated Server with Caddy
To make deployment more efficient and secure, we will use Caddy in this guide. Caddy’s primary attraction is its simplicity in configuration, along with its automatic handling of HTTPS certificates. In this guide, we’ll walk through the entire process of deploying a Rust application on a VPS, cloud, or dedicated server, using Caddy to manage the web traffic, SSL, and reverse proxy.
Getting Started: Setting Up Your Server Environment
The first step is accessing the server you plan to deploy your Rust application on. Whether it’s a VPS from providers like DigitalOcean, Linode, or Vultr, or a cloud server from AWS, Google Cloud, or Azure, the process remains largely the same. Alternatively, if you have a dedicated physical server, the setup follows a similar process.
To access your server, you’ll need to SSH into it using the credentials provided when you provisioned the machine. If you're on Linux or macOS, you can use the built-in terminal, while Windows users can utilize tools like PuTTY or the native terminal in WSL (Windows Subsystem for Linux). Once you're ready, access your server by running:
ssh your_user@your_server_ip
After logging in, the first thing you should do is update your server's package manager to ensure that all installed software is up-to-date and the system is secure. On Ubuntu, you can do this by running:
sudo apt update
sudo apt upgrade
Once the server is updated, it’s good practice to create a non-root user with sudo privileges to handle the application deployment and server management. Working with a non-root user enhances security by reducing the risk of accidentally changing system-critical files or configurations.
Installing Rust on the Server
Next, you’ll need to install Rust on your server to build your application from the source. The Rust team provides an official installer, rustup
, which simplifies the installation process. To install Rust on the server, run the following command:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Follow the prompts during installation. After installation is complete, ensure that Rust is available in your shell environment by either reopening the terminal or running:
source $HOME/.cargo/env
You can verify that Rust is installed by running:
rustc --version
This should return the version of the Rust compiler you just installed. At this point, Rust is ready on your server, and you can move forward with deploying your application.
Transferring Your Rust Application to the Server
If you've been developing your Rust application locally, you’ll need to transfer the code to your server. You can do this using scp
(secure copy), or if you’re familiar with Git, by pushing your code to a Git repository and pulling it onto your server.
For instance, to transfer the files using scp
, run the following from your local machine:
scp -r /path/to/your/rust-app your_user@your_server_ip:/home/your_user/
Once the files are on your server, navigate to the directory where you uploaded your project and compile the application. Rust's compilation process is straightforward. Navigate to the directory and run:
cd /home/your_user/rust-app
cargo build --release
This command will compile your Rust application in release mode, optimizing it for production use. Once compiled, the binary file will be located in the target/release/
directory.
You can now test your Rust application by running the binary:
./target/release/your_app_name
This will start your Rust application, and it should be accessible at localhost:your_port
, assuming your app is configured to listen on a specific port. At this stage, the application is running, but it’s only accessible locally. The next step is to expose it to the internet, and that’s where Caddy comes in.
Installing Caddy as the Web Server
Caddy is a modern web server that simplifies handling web traffic and automates the process of obtaining SSL certificates from Let’s Encrypt. To install Caddy on your server, you can use the package manager appropriate for your operating system. For Ubuntu, the installation can be done by adding the official Caddy repository and then installing it with apt
:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo tee /etc/apt/trusted.gpg.d/caddy-stable.asc
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy
Once Caddy is installed, you can configure it to serve as a reverse proxy for your Rust application, directing traffic from the internet to the Rust app running on localhost
.
Configuring Caddy for Your Rust Application
The next step is to configure Caddy to handle requests and forward them to your Rust application. This is done using a simple configuration file known as the Caddyfile
. The Caddyfile defines how Caddy should route incoming requests, manage SSL, and handle reverse proxying.
Create a new Caddyfile in the /etc/caddy/
directory:
sudo nano /etc/caddy/Caddyfile
In this file, define your server’s domain name or IP address, and set up a reverse proxy to the port where your Rust application is running. Here’s an example configuration:
yourdomain.com {
reverse_proxy localhost:8080
}
Replace yourdomain.com
with your actual domain or the IP address of your server. The reverse_proxy
directive tells Caddy to forward all incoming requests to the Rust application running on localhost:8080
.
Caddy also automatically manages SSL certificates, which means once this configuration is in place, it will automatically obtain and renew HTTPS certificates for your domain using Let’s Encrypt.
After saving the file, restart Caddy to apply the configuration:
sudo systemctl restart caddy
Caddy will now listen for requests on port 80 (HTTP) and port 443 (HTTPS) and forward them to your Rust application running on port 8080. If you visit https://yourdomain.com
in your browser, your Rust application should be accessible securely over HTTPS.
Running the Rust Application as a Systemd Service
While your Rust application is now accessible via Caddy, it’s important to ensure that the application runs continuously and automatically restarts if the server reboots. To do this, you can configure your Rust application to run as a systemd service.
Create a new systemd service file for your Rust application:
sudo nano /etc/systemd/system/rustapp.service
In this file, define your service configuration:
[Unit]
Description=Rust Application
[Service]
ExecStart=/home/your_user/rust-app/target/release/your_app_name
Restart=always
User=your_user
WorkingDirectory=/home/your_user/rust-app
[Install]
WantedBy=multi-user.target
This configuration tells systemd to start your Rust application automatically, restart it if it crashes, and run it as the user you’ve specified. Once the file is created, enable and start the service with the following commands:
sudo systemctl enable rustapp
sudo systemctl start rustapp
Your Rust application is now configured to run in the background, and systemd will manage its lifecycle, ensuring that it starts automatically if the server reboots.
Configuring DNS for Your Domain
If you want to access your Rust application using a domain name rather than an IP address, you’ll need to configure the DNS settings for your domain. Log in to your domain registrar’s control panel (such as GoDaddy, Namecheap, or Google Domains), and create an “A” record that points your domain to the IP address of your server. For example, you would add a record like this:
- Host: @
- Type: A
- Value: Your server’s IP address
- TTL: 30 minutes
Once DNS propagation completes, which can take anywhere from a few minutes to a few hours, your domain will point to your Rust application, and you’ll be able to access it via HTTPS.
Monitoring and Securing Your Rust Application
Once your Rust application is deployed and live, it’s essential to monitor its performance and ensure that it remains secure. Caddy’s automatic HTTPS support provides encryption for your web traffic, but there are other steps you can take to secure your application.
Monitoring tools like Prometheus and Grafana can be integrated to collect metrics from your Rust application, providing insight into performance, resource usage, and potential issues. You can also use log monitoring services or set up alerts with UptimeRobot or similar tools to notify you if your application experiences downtime.
Ensuring that your application is regularly updated is critical