Post

My Antisocial Home Server Finally Made a Friend (a VPS, via WireGuard)

Setting up my home server has become a little hobby of mine, and honestly I really enjoy it. It started as a way to host a few things for myself, but along the way it turned into one of the best learning playgrounds I could ask for. I picked up a ton: running and managing containers with Docker, creating and managing users and their permissions, and securing networks so the wrong people can’t wander in. Every problem I ran into taught me something new, and this post is one of those adventures.

This one started with a simple wish: I had a home server happily running a few containers, and I wanted to reach it from anywhere 😎!

My ISP back then (Orange) was blocking traffic from the outside world. Public access was restricted! One day, an idea strikes me: What if I use a VPN to create a direct tunnel between my home server and a VPS? The latter will act as a public gateway. I parked the idea in my mind for later.

Well, today is the day I finally act on it. What follows is a step-by-step log of everything I did to turn that idea into reality.

First, I bought a droplet (VPS) from DigitalOcean.

I set it up and added SSH keys so I can control the server from my main machine.

To make the SSH connection easy every time, I added the host, user and key file to the config, so I don’t have to type the long user@ip each time.

1
2
3
4
5
Host my-vps-server
HostName xxx.xxx.xxx.xxx
User root
IdentityFile ~/.ssh/id_ed25519_xxx
IdentitiesOnly yes

I installed WireGuard on the VPS:

1
2
sudo apt update
sudo apt install wireguard

Then I generated the keys:

1
wg genkey | tee server_private.key | wg pubkey > server_public.key

Next, I created a WireGuard config file and set the interface and the peer information:

1
/etc/wireguard/wg0.conf

Now I started WireGuard:

1
2
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0

I logged in to my home server and installed WireGuard. I created SSH keys to be used later and a config file for WireGuard.

There was no handshake, the tunnel wasn’t up. I had to enable and open a port on the VPS, so my home server could talk to it.

Now they were connected, Wohooo!!

On my home server I already had Nginx installed and configured, and Docker running some containers. Now I installed Nginx on the VPS and routed all traffic to my home server with the correct ports corresponding to each container.

I wanted to use a domain I have in Namecheap and create some subdomains to access various containers/services in my home server, but what a coincidence, Cloudflare had a worldwide outage that very day! So I had to wait a bit until it was fixed.

It got fixed.

I added SSL on the server to enable HTTPS:

1
2
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d **.******.***

I created all the subdomains and added the VPS IP to the DNS records.

I created a config file for Nginx on the VPS and added the corresponding routes. I used to create a file for each subdomain, but this time I used a single file for all of them.

Now I can access my services from everywhere 😁!

Some services were still not accessible from the outside world.

To fix that, I had to forward some ports from my VPS directly to my home server.

I edited the iptables on the VPS and added the following rule:

1
2
3
4
5
6
7
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]

-A PREROUTING -p tcp --dport yyyy -j DNAT --to-destination xx.xx.x.x:yyyy

COMMIT

Hmm… There is something wrong with my config file!!

Ah! Packets know where to go, but returned packets don’t know how to get back! My poor home server is yelling into the void!

I should add a return rule:

1
-A POSTROUTING -p tcp -d xx.xx.x.x --dport yyyy -j MASQUERADE

Well, that’s it! Thanks WireGuard!

This post is licensed under CC BY 4.0 by the author.