gem version

Using Passenger with Rust

To use Passenger with Rust, you'll need to use one of the many web frameworks available on Crates.io.

In this example we'll be using Rouille, because it is the simplest framework to use, and doesn't require a Nightly Rust compiler like Rocket. However you are free to choose your favourite framework, we simply made this choice for our docs.

Step 1: Install Rust

Rust is available from most OS' package managers. We recommend against using the rustup tool because it is less safe, more work to setup correctly, and requires you to perform updates outside your regular system updates via your package manager.

Debian:

No cargo package is available, though rustc is, please see this page for a manual download of the current stable release.

Ubuntu:

apt install -y cargo

CentOS/Rocky/Alma:

yum install -y rust-toolset

RHEL:

subscription-manager register --username $USERNAME_EMAIL --password $PASSWORD --auto-attach
subscription-manager repos --enable rhel-7-server-devtools-rpms
yum-config-manager --enable rhel-server-rhscl-7-rpms
yum install -y rust-toolset-7
scl enable rust-toolset-7 bash

macOS:

In order to use Homebrew to install Rust on macOS, you'll need to install the command line tools, as well as Homebrew itself.

If you are physically present at the mac, you can run the following simplified commands to install everything you need:

xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install rust

The following commands will install everything for you, entirely from the cli, so you can run them over ssh if need be.

touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
softwareupdate -i $(softwareupdate -l | grep "\*.*Command Line" | grep $(sw_vers -productVersion) | awk -F"*" '{print $2}' | sed -e 's/^ *//' | tr -d '\n') --verbose
rm /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install rust

Step 2: Create your project

Run the following command in a directory where you keep your projects. The cargo command will create a new directory called hello-rouille and populates it with the files you'll use to create your app.

cargo new hello-rouille --bin

Step 3: Hello world

Edit the Cargo.toml file to indicate that you are going to use the Rouille framework (called a crate in Rust lingo). You are going to add the following line in the [dependencies] section:

rouille = "^2.2.0"

Next edit the src/main.rs file to contain these contents:

extern crate rouille;
use rouille::Response;
use std::env::var;

fn main() {
    rouille::start_server(format!("0.0.0.0:{}", var("PORT").unwrap_or("80".to_string())), move |_request| {
        Response::text("Hello, world!")
    });
}

Step 4: Build and run

Run the following commands to run the web app, which will serve the web app on port 5000, then verify that your app is being served by sending a request to the web app from another terminal.

cd hello-rouille
env PORT=5000 cargo run
# in another terminal
curl http://localhost:5000/

Step 5: Add Passenger

You will need Passenger installed, for instructions on how to install Passenger click here.

Next we will want a release build of our web app, which you can make with cargo build --release, then you can move the executable into a more convenient location on your disk, such as ~/Sites/. Once you are happy with where things are, you can start your web app in Passenger by cding to the dir with the executable and running the following command: passenger start --app-start-command 'env PORT=$PORT ./hello-rouille'.

Passenger requires that applications listen on a particular port that Passenger has chosen, which it provides to the application by replacing the $PORT string in its startup command with the actual port number. Regardless of how your app accepts port arguments, the startup string must be able to convert from a cli argument to what works for your app. In our example we converted the port argument to an envvar to show how one may need to change how the port is passed to your app.

You can verify that everything is working with curl http://localhost:3000. To stop Passenger simply press Ctrl+C in the terminal where you started it.

The commands to run from this section are collected here:

cargo build --release
passenger start --app-start-command 'env PORT=$PORT ./hello-rouille'
# in another terminal
curl http://localhost:3000
light mode dark mode
Passenger 6 Passenger 6