Bradford Morgan White: Zero to Hosted, Part 2

20251126

In Part 1, we looked at just getting a site up on a local network. How do you get the site online? First, you need a domain name. There are plenty of registrars from whom to purchase a domain, and I typically recommend Cloudflare to those aiming to host at home. The advantage to Cloudflare is the ability to use tunnels. A Cloudflare tunnel, in our use case, allows us to host at home without exposing our home networks to the internet. This also keeps common ISPs from getting angry about the situation. If you already have your domain name, you can just set your NS records to Cloudflare and then use Clouflare as a DNS provider and not your registrar.

Once the domain is either registered or hosted by Cloudflare, we can setup the tunnel. If you are using a Raspberry Pi (if you're using an Intel/AMD machine change arm64 to amd64):

wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm64.deb
sudo dpkg -i cloudflared-linux-arm64.deb
cloudflared tunnel run
cloudflared tunnel login
cloudflared tunnel create mywebsite

We now have to create a config file for the tunnel. In $HOME/.cloudflared/ you should now have a file with the ending of .json and a name that is a rather long UUID. You need that filename. Put the following in /etc/cloudflared/config.yml:

tunnel: the-super-long-id-number
credentials-file: /home/user/.cloudflared/the-super-long-id-number.json
origincertpath: /home/user/.cloudflared/cert.pem
ingress:
  - hostname: mywebsite.com
    service: http://localhost:80
  - hostname: www.mywebsite.com
    service: http://localhost:80
  - service: http_status:404

To setup the DNS entries, we now need to inform cloudflare to point our domain name to this tunnel.

cloudflared tunnel route dns mywebsite mywebsite.com
cloudflared tunnel route dns mywebsite www.mywebsite.com

It's now a good idea to set the tunnel to run at system boot. Create a file /etc/systemd/system/cloudflared.service with the contents:

[Unit]
Description=Start Cloudflared
After=network.target

[Service]
Type=oneshot
ExecStart=/usr/bin/cloudflared --no-autoupdate --config /etc/cloudflared/config.yml tunnel run mywebsite

[Install]
WantedBy=default.target

You now need to reload systemd, and enable and launch the service.

sudo systemctl daemon-reload
sudo systemctl enable --now cloudflared

If everything went well, you should now be able to see your site from any internet connected machine :-)

More complex setups will be handled in the next article.

⇠ back

© MMIX - MMXXVI, Bradford Morgan White
Licentiam Absurdum