Raspberry pi as a web server

Has anyone used a pi to use as a web server? For example if I want to put a crystal web app on it and access it via the internet for personal projects.

If not, are there any free options available. Or what is the lowest cost option available?

I just want to do proof of concept web apps for myself

You can definitely do this yes, but self hosting will require some special considerations:

  1. Will need access to your router (assuming its running at home) to port forward traffic to your pi. Not really needed if you just want it available on your local network, i.e. not public
  2. Will need to ensure your pi is secure given you want it accessible via the public internet (if public)
  3. Ideally you have a static IP so it doesn’t change all the time breaking how people access the site (if public)
  4. Having some sort of domain name is also a nice plus (if public)
  5. You then need a way to route the traffic coming to the pi (on port 80/443 most likely) to the actual webserver running on the pi. It’s pretty common to do this via nginx or some other reverse proxy
  6. Will of course need to keep the pi on all the time
  7. Will need to come up with some way to deploy new versions of the application, and how you run the application. E.g. docker, systemd, etc

I’m sure there are plenty of online resources for this topic. Overall I think its a very good way to learn a lot about networking and can provide some real-world hands on experience with what’s all involved. So I definitely think it’s worth giving it a shot.

In addition to what Blacksmoke16 has already advised, keep in mind that the Pi’s architecture is ARM, so any apps that you need to run on the Pi will need to be compatible with that.

And along with port forwarding … If you have a home network, you likely have a dynamic IP, and while this normally won’t change as long as the router is on and connected, any time it re-boots for any reason, it’s likely to change the IP. (not your internal ones) There are services, noip.com is the one I use, but there are others, that will keep your IP updated and provide you with a domain name. Noip.com will do this for free up to (I think) four domain names, the way it works is you have your computer communicate the current IP peridically to noip, I run a web server and an IRC server on an Orange Pi and Raspberry Pi respectively by doing this in addition to having a regular box available to accept SSH sessions, and it works surprisingly well.

One more concern.

You probably don’t want pull code on Pi, and build your binary directly on there, this is not a high efficiency way.

Check Use zig cc as a alternative linker (draft version) for how to build an ARM static binary on local host, then copy it into Pi.

I use this process for my lucky web project, it work quite well!

Building on a machine other than the Pi sounds like a great idea. Maybe we should add a Raspberry Pi to the compile speed benchmarks thread so we can see the difference.

Would it also be feasible to build with Docker and then copy the file from the container to the Pi? I’m not very familiar with Raspberry Pi other than “it’s small and cheap”.

1 Like

It should works, but for my own case, docker data-dir use memory disk, so, after reboot system, i have to build container from scratch.

and i consider performance is better build on local then container too.

IMO the toughest thing to do is compiling on the aarch64 GNU/Linux target. I’d love to know if anyone has a Crystal guide/blog post/docs/etc for this. I couldn’t personally find a tag that would work on the official docker hub image.

I use my rPi as a server to host an OSS web server but it’s Python (easy to run in this architecture). I own a domain and manage DNS with Cloudflare, which means you can setup tunnels to access your server on a subdomain :sparkles: for free :sparkles: (cloudflared & web server managed by systemd). I wrote an article about this setup here if interested in that option.

2 Likes

Crystal multi-arch docker images available should help with that, as provided by 84codes. The official images do not currently include arm images.

Oh sweet, didn’t realized those existed. Thanks!

Interesting cloudflare give us this subdomain service for free.