Raspberry Pi 4 server at home


A Raspberry Pi 4 at home Link to heading

General Link to heading

I have installed a Raspberry Pi 4 at home with the goal of running it as a kind of server for different things. Here is a list of hardware, software and some points and facts:

  • Raspberry Pi 4 with 8Gb memory.
  • Raspberry Pi OS Lite 64-bit, Debian Bullseye with no desktop.
  • A USB 1GB SSD disk. No SD-card. Booting from the SSD.
  • Connected directly to my ISP modem, not to home network router.
  • Static site with homepages and blog set up with Hugo.
  • Caddy used for accessing services and routing everything.
  • Cloudflare used for handling DNS and mail routing.
  • A full Bitcoin node, setup as instructed in Raspibolt except for electrs and Electrum. Using Sparrow on Windows to access the node directly.
  • Lightning client with Ride The Lightning web interface, also setup with Raspibolt. More info: Lightning network | Wikipedia.
  • SSH into the Raspberry with PuTTY and/or VS Code.

Connections Link to heading

The Raspberry can be connected to the router and share the home network at a 192.168.n.n address. That will work fine, but connection will be lost when we turn off our router at night or when we go away for a longer period.

So I have it directly connected with a cable to the ISP modem, which we never turn off. Port 22 is open in the Raspberry and it is possible to SSH into it from anywhere in the world. If the Raspberry doesn’t want to connect, run the following:

sudo ethtool -s eth0 speed 100 duplex full autoneg off

SSH Link to heading

You can SSH into the Pi with PuTTY and/or VS Code at 83.177.182.nnn port 22 and private keys ~/.ssh/Windows_Private_Key.ppk for Putty and ~/.ssh/Windows_Private_Key - OpenSSH.ppk for VS Code. A description of this for VS Code โ€“ see this post.

The ip from my ISP is not static and changes from time to time (“nnn” above). This is solved automatically with Caddy and Cloudflare โ€“ see below โ€“ but Putty and VS Code must be updated manually.

Caddy Link to heading

Caddy is an open source web server of sorts. Does away with Apache, Nginx and databases.

  • Install Caddy

    echo "deb [trusted=yes] https://apt.fury.io/caddy/ /" | sudo tee -a /etc/apt/sources.list.d/caddy-fury.list
    sudo apt update
    sudo apt install caddy

  • Caddy uses ports 80 and 443. Open them and check status.

    sudo ufw allow 80/tcp
    sudo ufw allow 443/tcp
    sudo ufw enable
    sudo ufw status

  • Start Caddy service

    sudo systemctl start caddy

  • If running Caddy as a service, open Caddy config file /etc/caddy/Caddyfile and type the following.

    ahlstroem.net {
           root * /
           reverse_proxy localhost:1313
    }
    Open a terminal on the Raspberry Pi, cd to the folder from where you want to serve Hugo and run hugo serve. You can now reach localhost:1313 on the Raspberry Pi on the address it is assigned by your internet provider. In this case Comviq is the provider and the Raspberry address is 83.177.182.215(this has probably changed, see SSH above).

  • The hugo serve command is mostly for developing purposes. A more advanced way of building the site is to run the command hugo from the above mentioned folder. Hugo will then build the site under the public folder and the Caddy config file should contain the following:

    ahlstroem.net {
           root * /home/admin/hugo/blog/public
           file_server
    }

  • You can refer to another site by adding another post in the Caddy config file. Add as many sites you want this way.

    textochnot.se {
           root * /home/admin/hugo/blog/content/textochnot.se
           file_server
    }

  • Save and close the file and run sudo systemctl reload caddy.

Cloudflare Link to heading

If you set the DNS for each of your sites at your internet provider to point at the Raspberry Pi:s ip you can reach all of them. That is all well and good as long as your ISP doesn’t change the ip. But probably they will, so we let Cloudflare handle the DNS instead.

  • Sign up and set up a free account with Cloudflare.
  • Add your site. Must be a registered active domain.
  • Choose the Free option and click on Continue. Cloudflare will add your site.
  • Click Continue and change nameservers at your domain registrar. Your internet provider should point to Cloudflare’s nameservers jermaine.ns.cloudflare.com and sureena.ns.cloudflare.com. Click Done when ready. Log out from Cloudflare and wait for the changes to take place. Can take up to 24 hours.
  • Add an A record with the Raspberrys current ip-address.

Setup auto update of Cloudflare when ip changes Link to heading

Cloudflare Dynamic DNS IP Updater is a BASH script that updates DDNS at Cloudflare.

  • Download or clone it to any directory.
  • Copy the cloudflare-template.sh script and name it any way you want.
  • Open your new script and fill in the data as in instructions in this clip: DDNS on a Raspberry Pi using the Cloudflare API (Dynamic DNS). (Watch the whole clip and you get the whole setup process).
  • Run the script and it will update the ip at Cloudflare if a change has taken place. Put the script in Crontab and it will check by any interval you specify. How to do that, watch this: Automate your script (crontab).
  • Check if it is working: Change the ip in the Cloudflare DNS A entry (to 8.8.8.8 or anything), run the script, refresh Cloudflare page and watch it change.
  • Currently I have cron checking every minute and the script sends an e-mail to thomas.ahlstroem@gmx.com if and only if the ip has changed.

What to update when ip changes Link to heading

Putty:

  • Change to the new IP-address in Putty

VSCode:

  • Start VSCode.
  • Press F1 and click Open SSH Configuration File.
  • Click C:\Users\Thomasa.ssh\config.
  • Change HostName to the new IP-address.

Cloudflare:

  • Run the [~/nameofsite]_cloudflare.sh script for each site. It will change to the new IP-address at Cloudflare.

Bitcoin config file:

  • In the file ~/.bitcoin/bitcoin.conf under section [main], change rpcbind= and rpcallowip= to the new IP-address

Sparrow:

  • Change File-Preferences-Server-Bitcoin Core-URL to the new IP-address.

Update at a reboot of the Raspberry Link to heading

A crontab file at user admin runs at reboot. Be sure to run export EDITOR=nano before crontab -e if you need to edit it.

Send e-mail when ip changes Link to heading

NeoMutt is used to automatically send an e-mail when the ip changes. The process of setting up NeoMutt can be viewed here: Email on the terminal with mutt. Go into the Cloudflare script mentioned above. Go to the section “Get existing IP” and insert the following lines:

  • After if: neomutt -s "Raspibolt has a change of IP" thomas.ahlstroem@gmx.com < ~/ip.txt
  • After fi: neomutt -s "Raspibolt has no change of IP" thomas.ahlstroem@gmx.com < ~/ip.txt

Replace the e-mail above with an address to where you want to recieve the message.

Using Sparrow directly with Bitcoin Core Link to heading

I do not use Raspibolt’s way of connecting to the node with electrs. Sparrow can instead communicate directly with the node. See Connect to Bitcoin Core for instructions.

Sparrow not syncing from Windows Link to heading

If Sparrow wallet is not syncing when interacting with the Raspibolt Bitcoin node from Windows, one or more of following actions might help:

  • Maybe just restarting bitcoin core helps, especially after a reboot of the Raspberry Pi. Run sudo systemctl restart bitcoind.service.
  • Ensure that the entries dbcache=2000 and blockson1y=1 are disabled in the file /home/bitcoin/.bitcoin/bitcoin.conf on the Raspberry Pi.
  • Erase the file /home/bitcoin/.bitcoin/peers.dat
  • Change ControlPort 9051 to ControlPort 9150 in the /etc/tor/torrc file.
  • Don’t forget to run sudo systemctl restart bitcoind.service and/or sudo systemctl restart tor.service after making changes.
  • To monitor restart process of bitcoind.service run tail -f /home/bitcoin/.bitcoin/debug.log

Setup and move to myNode? Link to heading

Read here how to use already synced node: Using Bitcoin Data from Other Node

Change RTL port Link to heading

Changed the port that Ride The Lightning runs on, from 3000 to 7000. I tried to install Cryptpad that runs on port 3000 and I let the port change stay even if Cryptpad never was installed.