Why Self-Host?
Self-hosting is awesome! It gives you control over your data, helps you learn cool tech stuff like Linux and Docker, and can even save you money.
When I moved to Slovenia for my studies, I couldn't bring my whole PC setup. Self-hosting let me use my home computer remotely and learn a bunch of new things. It's been a fun and rewarding experience.
If you have a spare computer and want to try something new, give self-hosting a shot. Even if you don't know what you'll use your server for, it'll eventually come in handy and the experience you get will be valuable.
In this blogpost, i'll explain how to setup your machine to host Ubuntu Server, and containarise everything with Docker. We will also host a service called Memos and use Caddy to reverse proxy it to your domain.
Ubuntu server
As you know, a server is just like a regular old pc, with a few caveats. You'll first need to install some sort of operating system that's suitable for a server.
There's a lot of options when choosing your os, but some of the more popular ones include : Ubuntu Server, Debian, Fedora Server, Arch Linux, Windows Server. I personally recommend Ubuntu Server, since it has everything you need without too much setup
Simply get a USB and flash the Ubuntu ISO file onto it using Rufus. If you don't know how, just google it.
Don't forget to back up your data, on a seperate drive, or however you want. Triple check everything.
Now - plug the USB into your soon-to-be server and reboot into the Boot Menu (or BIOS). Select to boot from the USB. You'll be prompted with the Ubuntu server setup. Just follow that and you'll be fine. Make sure to install OpenSSH.
Setting up Docker
In order to run your services or whatever, it's good practice to containerize everything in it's own docker container. This will help with management down the line and helps with security as well.
You can install docker by running the commands below. They are taken from the official docker page
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
Setting Up Caddy
Once Docker is set up, the next step is to install Caddy in a Docker container. Caddy is an open-source web server with automatic HTTPS capabilities, and it's incredibly useful for managing reverse proxies.
Make a folder for your projects, i have mine at /home/elec/containers
mkdir containers
To install Caddy, we're gonna make a docker compose file. Go to the directory you created and make a folder caddy with a file compose.yml
cd containers
mkdir caddy
cd caddy
nano compose.yml
Put in the code below. If you don't understand what docker is and how it works, you should learn about it, it's awesome and a very important skill to have.
services:
caddy:
image: docker.io/caddy:latest
network_mode: host
restart: always
volumes:
- ./data/config:/config:z
- ./data/data:/data/caddy:z
- ./Caddyfile:/etc/caddy/Caddyfile:ro
Next, create a Caddyfile
in the same directory
nano Caddyfile
with the
following content:
memos.example.com {
reverse_proxy 127.0.0.1:5230
}
This tells Caddy to reverse proxy requests from memos.example.com to the Memos instance running on port 5230. Replace the example with your domain, and make sure to setup your DNS settings (Make an A record pointing to your ip). And also make sure ports 443 and 80 are forwarded on your router.
Now, you can bring up the Caddy container by running:
docker-compose up -d
Setting up Memos
With Caddy up and running, the final step is to set up the Memos instance. Make another folder in your containers and make a compose file
cd ..
mkdir memos
cd memos
nano compose.yml
And here's the compose :
version: '3'
services:
memos:
image: ghcr.io/usememos/memos:latest
volumes:
- ./data:/var/opt/memos
ports:
- 127.0.0.1:5230:5230
restart: always
Update the environment variables as necessary for your Memos setup. Then, bring up the entire stack by running:
docker-compose up -d
Now, your Memos instance should be accessible at your domain with Caddy handling the reverse proxy.
Some useful commands
To restart caddy when you change something without downtime, (make sure to run this in the caddy folder) you can use :
docker compose exec -w /etc/caddy caddy caddy reload
I also highly recommend you to use fish, a very neat and based shell. But don't take it from me, check out this article that my friend made about it. He's one of the people i've learned from the most when it comes to technology.
Thank you
That's it! You've successfully set up a server with Docker, Caddy, and a Memos instance. This setup can be extended to include other services as well, making it a powerful base for your self-hosting journey.
Thanks for taking the time to read this blogpost, hopefully it helped you with something.
For any occuring issues, i recommend you to google it or ask chatgpt, but if you really can't figure it out, you can contact me i'll try to help you out.