Deploy Your Rust Project to Any Hosting Provider in Minutes

In this post, I will show you the fastest way to deploy a Rust project to any hosting provider. We'll focus on Digital Ocean, but The principles discussed here can also be applied to other hosting providers. This guide assumes you already have a Rust project with a web server running on port 3000. Since Rust compiles to an executable binary, you don't need Docker or have to install Rust on the server, and you probably don't need Nginx either. Just compile, upload, and run.

Objective:

We aim to create a script that will update the server from a local environment. While it's possible to use something from a Continuous Integration (CI) pipeline, this post will focus on a quick deployment to allow you to get back to building your project.

Steps

  1. Create a Droplet with Debian and SSH access. Nothing too powerful is required; Rust can go a long way on modest resources.
  2. SSH into the box and clone your repository. If you don't need any files from your repository, you can skip this step. In this example, we'll call the app "my_app."
  3. Determine the architecture so we know the correct compile target
    $ dpkg --print-architecture
    amd64
    
  4. Install the Cross crate to compile our Rust app easily for any target.
  5. Compile the app with the correct architecture
    cross build --target x86_64-unknown-linux-gnu --release
    
  6. Upload the binary to the Droplet
    scp /your-path/my_app/target/x86_64-unknown-linux-gnu/release/my_app [email protected]:/root/my_app
    
  7. Make the binary execuatable
    ssh -f [email protected] 'chmod +x /root/my_app/my_app'
    
  8. Start the app
    ssh -f [email protected] './root/my_app'
    
  9. Expose port 3000 in the Droplet's firewall rulesAllow port 3000 in a Digital Ocean droplet firewall rule
  10. Visit the IP address or nameserver to verify it's working as expected.

Update Script

Here's a script to use for future updates to the Rust app.

cargo install cross
cross build --target x86_64-unknown-linux-gnu --release

ssh -f [email protected] 'sudo kill $(sudo lsof -t -i :3000)'
ssh -f [email protected] 'cd my_app && git pull'
scp /your-path/my_app/target/x86_64-unknown-linux-gnu/release/my_app [email protected]:/root/my_app
ssh -f [email protected] 'chmod +x /root/my_app/my_app'
ssh -f [email protected] './root/my_app'

Improvements

While this method is a quick way to deploy an app and return to building, it is worth noting that it misses some best practices for deploying a production application.