Raspberry Pi 4 server at home
A Raspberry Pi 4 at home
General
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.
-
Caddy used for accessing services and routing.
-
Cloudflare used for handling DNS and mail routing.
Connections
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
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
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
}
Hugo
- Install Hugo
This may install an older version. For the latest Hugo Extended:
```
Replace version with latest
HUGO_VER=0.126.1 wget https://github.com/gohugoio/hugo/releases/download/v$HUGO_VER/hugo_extended_${HUGO_VER}Linux-64bit.tar.gz tar -xvzf hugo_extended${HUGO_VER}_Linux-64bit.tar.gz sudo mv hugo /usr/local/bin/ ```
- Verify installation
hugo version
- Create a Hugo Site
hugo new site myblog
cd myblog
- Add a Theme and run local dev server
git init
git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke themes/ananke
echo 'theme = "ananke"' >> config.toml
Open a terminal on the Raspberry Pi, cd to the folder from where you want to serve Hugo and run hugo server -D
. 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
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
andsureena.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
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
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.
Update at a reboot of the Raspberry
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
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.