Deploying a Rust web application involves building an optimized binary and running it reliably behind a reverse proxy — this guide covers the complete deployment pattern.
Prerequisites
- Rust installed (see How to Install Rust on Ubuntu & Debian)
- Nginx installed for reverse proxying
Step 1 — Build Your Application in Release Mode
cargo build --release
Produces the optimized binary at target/release/your-app-name.
Step 2 — Deploy the Binary to Your Server
scp target/release/myapp user@YOUR_SERVER_IP:/opt/myapp/
Since Rust compiles to a native binary, you typically only need to transfer this single compiled file (plus any static assets your app serves), not the entire source tree or dependencies.
Step 3 — Create a Dedicated System User
sudo useradd -r -s /bin/false myapp
Step 4 — Create a systemd Service
sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Rust Web Application
After=network.target
[Service]
Type=simple
User=myapp
ExecStart=/opt/myapp/myapp
Restart=on-failure
Environment=PORT=8080
Environment=RUST_LOG=info
[Install]
WantedBy=multi-user.target
Step 5 — Start and Enable the Service
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
Step 6 — Verify It's Running
sudo systemctl status myapp
curl http://localhost:8080
Step 7 — Configure Nginx as a Reverse Proxy
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
Step 8 — Add HTTPS
sudo certbot --nginx -d yourdomain.com
Setting Up Structured Logging
Environment=RUST_LOG=info
Most Rust web frameworks integrate with the tracing or log crates — configure the RUST_LOG environment variable to control log verbosity, and ensure output flows to journald for review via journalctl -u myapp.
Building for Production Elsewhere and Deploying the Binary
For faster deployment, consider building in CI/CD (see How to Build a Simple CI/CD Pipeline with GitHub Actions) rather than building directly on the production server, keeping the server focused purely on running the already-compiled binary.
Zero-Downtime Deployment Considerations
Since Rust produces a single binary, deployment involves replacing the binary and restarting the service — for genuinely zero-downtime updates, consider a blue-green or rolling deployment pattern with a load balancer, similar to approaches covered for other language runtimes.
Common Errors
"cannot execute binary file: Exec format error" — the binary was compiled for a different CPU architecture than your server; verify you built for the correct target architecture matching your VPS.
Continue Reading
- How to Install Rust on Ubuntu & Debian
- How to Configure Nginx as a Reverse Proxy for Node.js/Docker Apps
- How to Build a Simple CI/CD Pipeline with GitHub Actions
Browse more articles in Programming Languages & Runtimes.