Skip to content

Instantly share code, notes, and snippets.

@alkavan
Last active June 2, 2023 19:17
Show Gist options
  • Save alkavan/2e7050cf97f542268aac414232ebd85f to your computer and use it in GitHub Desktop.
Save alkavan/2e7050cf97f542268aac414232ebd85f to your computer and use it in GitHub Desktop.
Instructions how to bootstrap a Rocket (Rust) based web application server.

Rust Webserver Framework (Rocket)

Notice: These instructions are for Rocky Linux 9/8, but with small adjustments can be used on any.

Install Rust

Install the crab language (RHEL distributions):

sudo dnf install rust rust-src cargo

For non-RHEL distributions:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Initialize Rocket Framework Project

Create new project using cargo and give permission to webmaster user:

cd /var/www/
sudo cargo new hello-rocket --bin
sudo chown -R webmaster:webmaster hello-rocket
cd hello-rocket/

Add rocket framework:

echo -e 'rocket = { version = "=0.5.0-rc.3", features = ["secrets", "tls", "json", "http2", "uuid"] }\n' >> Cargo.toml

Add some basic website code nano src/main.rs:

#[macro_use] extern crate rocket;

#[get("/")]
fn index() -> &'static str {
    "Hello, world!"
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![index])
}

Add configuration file nano Rocket.toml:

## defaults for _all_ profiles
[default]
#address = "127.0.0.1"
limits = { form = "64 kB", json = "1 MiB" }

## set only when compiled in debug mode, i.e, `cargo build`
[debug]
port = 9000
## only the `json` key from `default` will be overridden; `form` will remain
limits = { json = "10MiB" }
secret_key = "czEyUm5iNkdNS1pid1RYOWEyTkc2Y0RJemZuV09oOFcK"

## set only when compiled in release mode, i.e, `cargo build --release`
[release]
port = 8000
ip_header = false
secret_key = "VnEzeGlVQXBRRXlmTnowMkNwaXFpY2ZveHZ3OG5jWWgK"

Build web application:

cargo build

To build for production: cargo build --release

Allow httpd to use the local server as proxy:

setsebool -P httpd_can_network_connect 1

Run rocket web-server (to view session type tmux at in the shell):

tmux new-session -d -s "rocket" cargo run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment