My journey to self-hosting

My journey to self-hosting
we gonna run on potatos

Host ur own infra at a cheap cost

I am an advocate for self-reliance and sufficiency. Everyone should be self-sufficient and reliant, unless it is not economically viable for them.
In most scenarios, a person might not be able to be self-sufficient because of situations such as not knowing how to farm, not knowing how to do taxes, or not knowing how computers work.
Well, I can help you with that. (Obviously, the tech/computer part.) (I don't know sh*t about farming or taxes.)

Let's start with a list of things you can self-host.

  • email
  • cloud storage
  • automations
  • notes
  • a personal portfolio
    Out of these, what would I recommend to self-host?
  • cloud storage
  • automations

...The tech-savvy people might ask, Uh, what about email? I will explain in another blog post.

What are the raw materials?

  • A cheap computer or server
  • working internet connection
  • power backup for your machine* (*please note power backup must be of the correct wattage or else you are only going to damage the machine)
  • a domain (any domain, not just .com; buy it from any one of the domain registrars)

How much would it cost?

My setup costs

  • 8k (INR) for the Raspberry Pi
  • 2K (INR) for the power backup
  • 800 (INR) for the domain
  • 500(INR) for snacks (jus jokin :D)
    Total: 10800K(INR)
    Note: You've got to renew your domain yearly.

How to calculate power backup rating

use this formula to calculate your backup rating

Screenshot 2024-12-08 at 16-35-18 website_content_posts_SelfHost.md at main · WithBites_website.png

For example

pi's rated wattage = 2.5A * 5V = 2.5*5
and i require an hour of backup

Screenshot 2024-12-08 at 16-35-46 website_content_posts_SelfHost.md at main · WithBites_website.png

for the ampere rating of pi I am referring to the Raspberry Pi's [technical sheet] (https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#power-supply) .

now Buy a UPS with the same or more mAh rating. (In the case of larger devices, required backups might be in Ah, then remove the 1000 from the formula.)

Setting up personal cloud

Utility cloud

Follow this gentleman's video.
He shows how to set up a Raspberry Pi and Casa OS (https://casaos.io/).
youtube guide

App hosting

Install any Linux-based OS on your hosting app. For Raspberry Pi, follow "Getting Started with a Pi".

Then, let's install Kubernetes for deploying our apps.
There are a lot of lightweight Kubernetes solutions like K3d, K3s, Kind, MicroK8s, and MiniKube. But we'll only look at k3s because they're simple to set up.
now ,
just run

curl -sfL https://get.k3s.io | sh -

That command should probably work, if not

curl -sfL https://get.k3s.io | sh -

Now that you have a working Kubernetes setup, all you have to do is add a few more things to make it completely functional.

  • Dashboard
  • Storage setup

Setting up the tunnel

Tunnel is software that allows us to expose our local device to the internet without a static IP.
e.g., ngrok, cloudflare or you may use one of the open-source alternatives here.

But in this blog, I am going to cover Cloudflare.
Let's get started.
Since I am using Debian Buster-based Rasbian, I follow these. For your variation, please refer to this.

# Add cloudflare gpg key
$ sudo mkdir -p --mode=0755 /usr/share/keyrings
$ curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null

# Add this repo to your apt repositories
$ echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared buster main' | sudo tee /etc/apt/sources.list.d/cloudflared.list

# install cloudflared
$ sudo apt-get update && sudo apt-get install cloudflared

Once done, you must be able to use Cloudflare on your machine.

$ cloudflared version
cloudflared version 2023.6.1 (built 2023-06-20-0928 UTC)

Now we register a tunnel (hopefully you've already set up a domain with Cloudflare).

Step 1 (Authenticate cloudflared)

$ cloudflared tunnel login
# A url would be provided open it in a browser window and 
# it will prompt you to log in to your Cloudflare account. 
# After logging in to your account, select your hostname.

Step 2 (create a tunnel):

$ cloudflared tunnel create <NAME>

Now the credential json file will most probably show in o/p but in case you missed it , it would be ~/.cloudflared/.json

Step 3 (initial config of tunnel)

Now lets create a config.yaml file in the ~/.cloudflared/ folder

url: http://localhost:80
tunnel: <Tunnel-UUID>
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json

Step 4 (configuring domain DNS for the tunnel)

$ cloudflared tunnel route dns <UUID or NAME> <hostname>

This will map the provided <hostname> to the tunnel in the DNS configuration.
And check it with this command.

cloudflared tunnel route ip show

Step 5 (Run the tunnel)

Now let's test our tunnel.

$ cloudflared tunnel run <UUID or NAME>

This should run a tunnel, and now you can test the tunnel by directly hitting the domain.

Most probably it should work, but if you skipped the k3s part, you would see a cloud flare error since you don't have an app running on port 80.

Step 6 (configure a daemon)

Let's configure a service (daemon) using cloudflared iself. Yeah, it's a very useful tool. No messy writing. service file or something

# press ctrl+c to stop tunnel run first then 
$ sudo cloudflared service install
$ sudo systemctl start cloudflared
$ systemctl status cloudflared
# This should initialize the cloudflared as a daemon.

Buttttttt... this means Cloudflare's service is running as root. ( Some of you might not like that, but hey, if you are knowledgeable enough to hate that, you probably can fix it yourself ;) .)
Sooooo yeah, since the root user has a different path to configure Cloudflared, you must move your config and tunnel credentials to that folder, /etc/cloudflared/config.yml

$ sudo cp ~./cloudflared/* /etc/cloudflared/
$ ls /etc/cloudflared/ #to confirm files
$ sudo systemctl restart cloudflared
Please note: If you did not create the k3s part, you are now setup with a working tunnel. All you have to do is create an app running on port 80 to use it to expose your app to the external internet.
If you want a simple tutorial, I would suggest you reevaluate your life choices of learning to create a tunnel first without knowing how to run an app on port 80. But hey, I am just a guy on the internet with some free time; who am I to judge?
Follow this page: https://raspberrytips.com/node-js-raspberry-pi/ and replace 8080 with 80.

Step 7 (add multiple domains or host multiple apps on the same machine)

Now let's edit the config file for this; by default, config.yml only supports one port mapping. For multi-port mapping, we must create an ingress mapping.

tunnel: <tunnel-id>
credentials-file: /etc/cloudflared/<tunnel-id>.json

ingress:
  - hostname: k3s.example.dev
    service: http://localhost:80
  - hostname: ssh.example.dev
    service: ssh://localhost:22
  - service: http_status:404

Now this will map

gitlab.example.dev --->  http://localhost:80
ssh.example.dev --->  ssh://localhost:22

Here I have forwarded my port 80, where my k3s ingress is running, and my port 22 to access my pi's ssh anywhere if I had a working internet connection.
Note: By exposing ssh to the internet, you basically invited everyone on the internet to hack you. I would suggest configuring a Cloudflare application. and rendering ssh terminal on browser following this

Well now, mate, you are now setup with a cheap self-hosted server. Now do some tinkering and get some apps created, and please let me know what you did by mailing me at [email protected] or [email protected] .