POSTS

Rust, Dreamhost shared hosting, and FastCGI

I’ve been playing with Rust recently, and one of the things I wanted to do with it is get it running on Dreamhost’s shared hosting. After a bit of monkeying around, I got a little demo working with FastCGI.

Why shared hosting?

If you’re wondering why I sometimes use Dreamhost shared hosting instead of VPS/Linode/AWS, and the answer is that it’s incredibly cheap for throwaway projects and websites that need a level of availability a Raspberry Pi at home might not be able to deliver.

Start cheap, and move once the project has outgrown what’s available. Use the money you save on coffee and a nicer keyboard ;-)

FastCGI Go Go Go!

As of this writing the shared hosting Passenger support doesn’t have Rust support, but it does have FastCGI, and Rust has FastCGI support in a crate already.

[package]
name = "fcgi-test"
version = "0.1.0"
authors = ["You <you@example.com>"]
edition = "2018"

[dependencies]
fastcgi = "1.0.0"

You can whip up a quick program easily enough, too:

extern crate fastcgi;

use std::io::Write;
use std::time::SystemTime;

fn main() {
    fastcgi::run(|mut req| {
        write!(
            &mut req.stdout(),
            "Content-Type: text/plain\n\n{:?}",
            SystemTime::now()
        )
        .unwrap_or(());
    });
}

Once you’ve built your project’s executable, drop it in a public-facing folder, give it an .fcgi suffix (or do the appropriate Apache configuration if this needs to look nice), and make sure that the file is executable.

chmod +x fcgi-test.fcgi

If you aren’t using Linux

Dreamhost shared hosting uses Debian, so if you aren’t using Linux already you’ll need to set up a cross-compilation toolchain. On my Mac, setting up the toolchain was fairly straight-forward with Homebrew.

# This installs the Linux target to your toolchain
rustup target add x86_64-unknown-linux-musl
# Install the binary utilities to make ELF stuff on Mac
brew install x86_64-elf-binutils

Then I added the following to my project’s .cargo/config file

[build]
target = "x86_64-unknown-linux-musl"

[target.x86_64-unknown-linux-musl]
linker = "x86_64-elf-ld"