← All posts
7 min read

How I started self hosting my services

With the increasing cost of compute, self hosting is the best option for small projects. Virtually free and a lot of learning experiences.

freehostinghow-to

Hello everyone! From time to time I get asked how I pay for the hosting of my webs like writeinone.com or en2fe.com, but I simply don't. In this blog I will explain in a very short way how I host everything at home, it will not be an exhaustive guide as there would be too many things to cover, but I might create a follow up in the future to explain certain topics in detail.

Why I started

I started self hosting when I was at uni with a Raspberry Pi that I used to host some projects. Later I tried using cloud services like Oracle Cloud, which offered a really nice free tier, check this blog to see how to use it [How to host an API or Web Server in Oracle Cloud 100% Free]. But as soon as you need some real power, it becomes incredibly expensive.

In my case I wanted to have a Minecraft server with mods that required 15 GB of RAM idle and up to 25 when there were a couple of players. Then I decided to mount my own server. I got a new PC and using old parts I was able to have a really nice machine rocking 32 GB of DDR4 RAM, an Intel i5-4460 and a dream. (I still use it and it works great)

So how do you actually do it

First if you want to expose a server, you need an IP. These days internet providers often use CG-NAT, so it is most probable that you have to ask for a public IP, it might require an extra charge at the end of the month though. In my case it is 1€ per month so it is not a lot.

Note: You don't need a static IP. Some providers don't allow static IPs but all of them must support public IPs

Okay we have our IP secured, or maybe not. Public IPs are dynamic for the most part, which means that from time to time it will change. So we have to use DDNS (Dynamic DNS). We go to our trusted domain registrar, in my case it is actually Namecheap (I don't trust them a lot but it's cheap). Then we will see that there is an option to create a DNS record using dynamic DNS. In each provider the mechanism can be different. Here we just need the password and call an API, or use an already existing DDNS client. Here is the guide for Namecheap.

But I actually already do this with my API Gateway.

The actual service to expose

In my case I only expose an API Gateway that I created which redirect requests based on the domain used to my other services. This is the service I will expose. It has DDNS support for Cloudflare and Namecheap, but that is not the only thing it does:

  • Since we only have 1 IP, we cannot expose HTTPS port (443) multiple times right? That's why we need
  • HTTPS requires SSL certs, we need to update them each 30 days or so. So we integrate with **Let's En
  • We are vulnerable to attacks and bot scans. So we need a rate limiter and bot blocker.
  • And it does other things that are not related to this topic...

Check the other blogs related to the gateway to learn more: https://gonzalo-leon.site/blog/en/articlted-api-gateway-with-elixir

Obviously if you want to use nginx you are more than free to do it, or even if you only want to host a single page, you don't need an API gateway.

In any case, the machine that hosts that service should have an ip that doesnt change for the router. You can use the static ip config and set it in the machine, but if the network changes or something else takes that ip you migh have to manually edit the config in the machine. For that reason I recommend using a dhcp reservation in the router. It is a config in the dhcp server of the router where you select which ip is reserved for a mac address.

Exposing the ports and protection

To expose a port you first need a router that allows you to expose a port, if your ISP doesn't provide can try buying an independent one that can take the optic fiber directly. Once you have it, you will have to log in to the UI of the router, it is probably at http://192.168.0.1, but your router mightike 192.168.1.0/24, check your IP to know which network you are in and try putting the first address of that network in a browser. It will prompt you with the typical user and password login, it is usually s password unless you change it. Although lately the routers have a sticker on the bottom that showsthe login credentials. Btw, you should change that user and password...

Once we are in the menu, we will want to find a section Port forwarding or maybe it is called `virtue can create a new rule to forward ports 80 and 443 (HTTP and HTTPS) towards the IP of our serverrunning the gateway and the ports that the gateway exposes. This way when a user puts our domain on the browser, it will go directly to our API gateway and then into our services.

However, this is a bit dangerous, what if our API gateway has a vulnerability that lets a hacker into dangerous. This is usually solved with VLANs since it gives a literal separation of networks between our services and our home devices. But VLANs are usually only available in really good routers.

So what I do is instead of creating virtual networks, I created physical networks. I had 2 extra routi coverage and the other was an old one I used as a repeater some time ago. So I made the old onecreate a new network only for the external facing services, that could not access the home router, but in the home router I added a static route to the other one, this way I could SSH into machines and whatnot. So in total 3 routers with 3 networks.

With this and some firewall rules and not running the services in sudo mode, you have a relatively sec

100% Uptime

This is the hard part, power goes off, internet goes down, maybe you tripped on the ethernet cables... But there are some solutions.

In my case I bought a UPS that can power my servers for up to 20 min after a power shutdown, this is no prevent the most common power outages a house can have. On top of that it has surge protection, which is useful if you don't want a random lightning frying your motherboard.

Internet providers don't give SLAs for residential networks so you have to get a bit creative. In my c router with a SIM card on a data plan (of a different ISP). With 8 GB a month, it has been more thanenough for the outages I had.

Also remember to set up your server machines for automatic unattended updates, so you don't need to mathey can be on all the time.

Cost

I pay around 7€ a month for electricity of 3 beefy machines, with one Raspberry Pi or a mini PC you weuro from the public IP, I pay 8€ for what in AWS would cost me 300€ monthly (3 machines with more than 16 GB of RAM).

You could come up with the initial costs justification, and yes, I did have to buy some things like the UPS or parts for the old PCs I used for servers, but with this montly price difference you will make it worth in less than a year for sure.

Other topics

In another blog I will probably cover some of these things, but naming them might be useful in case yo

  • WireGuard to communicate with cloud machines.
  • Prometheus to scrape metrics.
  • Grafana to visualize metrics.
  • VictoriaLogs and Alloy to aggregate logs.
  • Postgres and Mongo backups at least daily.
  • Alerts to your email or Discord to know what is broken.

I'm probably missing some things, but with this, you have something to start, and it is where I started.