Rust Web Hosting

I'm planning on using Ubuntu 20.04 LTS because that's what I've been using locally, but if needed I can change it.

Using Ubuntu is fine by me.

I just cloned my github repo to my linode instance. What steps do I have to take to compile and run the server? Also is it fine if the actix server is listening on port 127.0.0.1:8088 (localhost:8088) or should I change it (perhaps to the ip address of the linode)?

You install Rust, then do cargo build --release inside the project. This produces an executable in the target/release folder, which you can run by typing

./target/release/[your_project_name]

You should bind to 0.0.0.0 instead of 127.0.0.1, otherwise it is not accessible from outside the machine itself.

If you want the program to be started on reboot of the server, google how to create a Linux service that runs it using the full path to the binary in the target folder.

It's a good idea to have ones web server secured with HTTPS.

To that end I find it easier to run an nginx web server and have it handle all the certs and such. Nginx then forwards requests to the Rust web server(s) and other services which can then bind to localhost and listen some weird port numbers.

So then would the port 0.0.0.0:8088 be valid? Should I change 8088 to something else as well or is it fine as is? From what I know so far, I think later on when you set up the firewall you close some ports leaving only a few open or is that something else? Also in this case, should I leave the database on port 127.0.0.1 so that only database commands coming locally from the machine through the server will be processed?

So I did what you said. I changed my port from 127.0.0.1:8088 to 0.0.0.0:8088, installed Rust, compiled the code, and ran it through the Linode instance. I tried to visit the site with my domain name but it said the server refused to connect. Then I tried visiting it directly with Linode's IP address and again I get the message saying the server refused to connect. I also tried it running cargo run --release and there was no difference. What step did I miss? I was under the assumption that I could visit the site afterward.

Edit: It might be the firewall, I'm going to check if it works on a different port number.
Edit: It worked! It's successfully hosting the website. Now I just have to download the database and activate my SSL.

Sounds like you figured it out. Yeah, you probably want the database to bind to 127.0.0.1 so its not accessible from the internet.

Would you happen to know how to install a SSL certificate to Actix Web? I have the certificate issued and ready to go from NameCheap (already verified that I own the domain and validated the SSL; just need to install it)

I would recommend putting your service behind something like nginx, which will handle all the SSL stuff for you and have it reverse proxy to your rust server that’s running using plain HTTP.

Also you can get SSL certificates for free from LetsEncrypt.

As @XAMPPRocky said

This is good advice👆.

If you already paid for a certificate you can use that until it expires. Let's encrypt is better though: renews automatically, and is free.

If you want to use the cert you have, use nginx. There are lots of guides, just search the internet. Otherwise check out nginx + let's encrypt.

Hi,
Is there any resources online explaining how to connect a server or dynamic website to nginx? Most guides just explain how to post a static website with nginx.

You can do a search for "nginx reverse proxy localhost".

The first result for me is this: nginx reverse proxy to backend running on localhost - Stack Overflow that's not a guide, just a config file.

This result seems to be a guide: https://gist.github.com/soheilhy/8b94347ff8336d971ad0

Check them out and do more searches, then if you have more questions please ask them.

I wouldn't really recommend it, but if you wanted to change your code to warp rather than actix you could skip the proxy and use warp-lets-encrypt.

I would not recommend it either. Because using Nginx in the front line means:

  1. You don't have to mess with TLS and certs in your Rust web server. Let alone changing Rust framework and rewriting the whole thing.

  2. If you happen to have more than one virtual machine in the cloud you can put multiple Rust web services behind the nginx proxy.

As far as I can tell bes' links above cover all that is required to get nginx to do this. It's not actually difficult to do.

Well, the point of warp-lets-encrypt is precisely to avoid having to mess with certs at all (at least in your code or configuration). But yes, it doesn't seem worth rewriting for that.

I have no idea what problem warp-lets-encrypt is solving. I just run the letsencrypt certbot and everything works!

I just setup nginx and the SSL certificate and now my website is secure! All that's left is to set up the database and I'm good to go!

Good for you!

Excellent.

All we need now is an nginx written in Rust :slight_smile: