Deploy Rust Apps
Rust installation is powered by rustup. To install latest version with deployment script:
features:
- rust
Without this, rustc
won't be available since no globally installed Rust compiler available.
Install other version using rust latest
, rust 1.73
, etc. To uninstall use rust off
. Check the current version using rustc --version
from SSH.
Example
The deployment script below installs the latest rust
compiler, compiles main.rs
and runs the main
binary serving HTTP from PORT
envar.
source: clear
features:
- rust
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT ./main
commands:
- filename: main.rs
content: |
use std::{env,str};
use std::io::Write;
use std::net::TcpListener;
use std::process::Command;
const HTTP_HEADER: &str = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\r\n";
const HTML_TEXT: &str = r#"
<!DOCTYPE html>
<html>
<head>
<title>Rust App</title>
<link rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css">
</head>
<body class="p-5 text-center">
<p><img src="//images.unsplash.com/photo-1465153690352-10c1b29577f8?fit=crop&w=200&h=200"
class="img-fluid rounded-circle"></p>
<h1 class="mb-3">Hello, world!</h1>
<p>Serving from Rust version {}</p>
<p class="text-muted">DOM Cloud</p>
</body>
</html>
"#;
fn get_version() -> String {
let stdout = Command::new("rustc")
.arg("-V")
.output()
.expect("failed to execute process")
.stdout;
let text = str::from_utf8(&stdout).unwrap().to_string();
let chunks: Vec<&str> = text.split(" ").collect();
chunks[1].to_string()
}
fn main() {
let port = env::var("PORT").expect("$PORT is not set");
let socket_addr = "127.0.0.1".to_string() + ":" + &port;
let listener = TcpListener::bind(socket_addr).unwrap();
let version = get_version();
println!("Listening for connections on port {}", port);
for stream in listener.incoming() {
match stream {
Ok(mut stream) => {
let response = HTTP_HEADER.to_string() + &HTML_TEXT.replace("{}", &version);
stream.write(response.as_bytes()).unwrap();
}
Err(e) => {
println!("Unable to connect: {}", e);
}
}
}
}
- rustc main.rs
Existing Rust projects
For existing rust apps, use this deployment script. This project must have a Cargo.toml
initialized.
features:
- rust
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT ./target/release/app
commands:
- cargo build --release -o target/release/app
It's possible to run the app via cargo run
. This eliminates the need to compile the code into a binary, although startup time may be a little bit slower.
features:
- rust
nginx:
root: public_html/public
passenger:
enabled: "on"
app_start_command: env PORT=$PORT cargo run
commands:
- cargo build
App Management
Your app do not restarted automatically after file changes. To restart, run restart
via SSH.
Environment variables can be set either using NGINX's env_var_list
or ~/.bashrc
. Usually your language framework also reads .env
files.
See NGINX and App Daemon for more information about NGINX and App managements including restarting, environment variables, and other global limitations.
App Logging
You can see app log from Check -> Check Process Logs tab. Only startup problems displayed in the browser.
You might want to use a proper logging mechanism such as the standard logging library then write it to a log file, or any other solution that suits you.
NGINX errors and traffic logs can be examined via Webmin or Check -> Check Process Logs tab.
Troubleshooting
Execution took more than 600s, exiting gracefully
Please run cargo build --release
via SSH instead. Usually it will be very long for first time compilation.