A Rust-powered public web page in 5 minutes

Raphaël Huchet
2 min readJul 12, 2016

--

Let’s do an “hello world” website powered by rust in about 5 minutes with this quick and dirty tutorial. You will need a fresh ubuntu 16.04 server (about $5 on Digital Ocean, buy it, throw it). Obviously, after this 3 steps recipe, your server will not be ready for production (no logs, no security, etc.). It will just display “hello world”.

Step 1: Rust

First install Rust and gcc

$ curl -sSf https://static.rust-lang.org/rustup.sh | sh
$ apt-get install gcc

Start the www project with Cargo

$ cd /home
$ cargo new www --bin

Edit /home/www/Cargo.toml and add Iron to your project

[package]
name = "www"
version = "0.1.0"
authors = ["me"]
[dependencies]
iron = "0.4"

Edit /home/www/src/main.rs and paste the hello code from Iron

extern crate iron; use iron::prelude::*;
use iron::status;
fn main() {
Iron::new(|_: &mut Request| {
Ok(Response::with((status::Ok, "Hello world!")))
}).http("localhost:3000").unwrap();
}

Build www project and please wait

$ cargo build --release

Step 2: Nginx

Install Nginx

$ apt-get install nginx 

Check Nginx install by opening your browser and go to your server IP. It should display a welcome message from Nginx. It works! Then edit the default site in /etc/nginx/sites-enabled/default and replace the location zone with that proxy:

location / { 
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000;
proxy_redirect off;
}

Reload the service

$ service nginx reload

Step 3: Supervisor

Install Supervisor to run your hello world app persistently

$ apt-get install supervisor

Edit /etc/supervisor/conf.d/www.conf

[program:www]
command=/home/www/target/release/www
autostart=true
autorestart=true
stderr_logfile=/var/log/www.err.log
stdout_logfile=/var/log/www.out.log

Restart Supervisor

$ service supervisor restart

Epilogue: Your Browser

Open your favorite browser, go to your server IP. It works!

--

--